From 44f9b71c2df5190709bcda11703b989e8515fb73 Mon Sep 17 00:00:00 2001 From: Dawid Kisielewski Date: Tue, 26 Sep 2023 18:17:03 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20=20Invert=20if=20to=20reduce=20nest?= =?UTF-8?q?ing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/client/doc.js | 53 +++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/lib/client/doc.js b/lib/client/doc.js index 7bb47c33f..2472839e7 100644 --- a/lib/client/doc.js +++ b/lib/client/doc.js @@ -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) {