Skip to content

Commit

Permalink
Merge pull request #625 from share/refactor-code-invert-if
Browse files Browse the repository at this point in the history
♻  Invert if to reduce nesting
  • Loading branch information
dawidreedsy authored Sep 26, 2023
2 parents b7d0965 + 44f9b71 commit 62e4ec5
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions lib/client/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -984,38 +984,37 @@ Doc.prototype._rollback = function(err) {
// working state, then call back
var op = this.inflightOp;

if ('op' in op && op.type.invert) {
try {
op.op = op.type.invert(op.op);
} catch (error) {
// If the op doesn't support `.invert()`, we just reload the doc
// instead of trying to locally revert it.
return this._hardRollback(err);
}
if (!('op' in op && op.type.invert)) {
return this._hardRollback(err);
}

// Transform the undo operation by any pending ops.
for (var i = 0; i < this.pendingOps.length; i++) {
var transformErr = transformX(this.pendingOps[i], op);
if (transformErr) return this._hardRollback(transformErr);
}
try {
op.op = op.type.invert(op.op);
} catch (error) {
// If the op doesn't support `.invert()`, we just reload the doc
// instead of trying to locally revert it.
return this._hardRollback(err);
}

// ... and apply it locally, reverting the changes.
//
// This operation is applied to look like it comes from a remote source.
// I'm still not 100% sure about this functionality, because its really a
// local op. Basically, the problem is that if the client's op is rejected
// by the server, the editor window should update to reflect the undo.
try {
this._otApply(op, false);
} catch (error) {
return this._hardRollback(error);
}
// Transform the undo operation by any pending ops.
for (var i = 0; i < this.pendingOps.length; i++) {
var transformErr = transformX(this.pendingOps[i], op);
if (transformErr) return this._hardRollback(transformErr);
}

this._clearInflightOp(err);
return;
// ... and apply it locally, reverting the changes.
//
// This operation is applied to look like it comes from a remote source.
// I'm still not 100% sure about this functionality, because its really a
// local op. Basically, the problem is that if the client's op is rejected
// by the server, the editor window should update to reflect the undo.
try {
this._otApply(op, false);
} catch (error) {
return this._hardRollback(error);
}

this._hardRollback(err);
this._clearInflightOp(err);
};

Doc.prototype._hardRollback = function(err) {
Expand Down

0 comments on commit 62e4ec5

Please sign in to comment.