Skip to content

Commit

Permalink
♻ Invert if to reduce nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawidpol committed Sep 26, 2023
1 parent b7d0965 commit 44f9b71
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 44f9b71

Please sign in to comment.