From 60f2a059ec6f34f9d5c13dc82575f14b2d1f20fd Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Tue, 19 Oct 2021 04:36:13 -0400 Subject: [PATCH] Changeset: Deprecate `oldLen()` and `newLen()` functions --- src/node/handler/PadMessageHandler.js | 4 ++-- src/static/js/Changeset.js | 14 ++++++++++++-- src/static/js/ace2_inner.js | 7 +++---- src/static/js/changesettracker.js | 2 +- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/node/handler/PadMessageHandler.js b/src/node/handler/PadMessageHandler.js index 8dcb6120c..03db4ad44 100644 --- a/src/node/handler/PadMessageHandler.js +++ b/src/node/handler/PadMessageHandler.js @@ -628,10 +628,10 @@ const handleUserChanges = async (socket, message) => { const prevText = pad.text(); - if (Changeset.oldLen(rebasedChangeset) !== prevText.length) { + if (Changeset.unpack(rebasedChangeset).oldLen !== prevText.length) { throw new Error( `Can't apply changeset ${rebasedChangeset} with oldLen ` + - `${Changeset.oldLen(rebasedChangeset)} to document of length ${prevText.length}`); + `${Changeset.unpack(rebasedChangeset).oldLen} to document of length ${prevText.length}`); } const newRev = await pad.appendRevision(rebasedChangeset, thisSession.author); diff --git a/src/static/js/Changeset.js b/src/static/js/Changeset.js index 2393c8c35..aef04c1b4 100644 --- a/src/static/js/Changeset.js +++ b/src/static/js/Changeset.js @@ -286,18 +286,28 @@ class Changeset { /** * Returns the required length of the text before changeset can be applied. * + * @deprecated Use `Changeset.unpack(cs).oldLen` instead. * @param {string} cs - String representation of the Changeset * @returns {number} oldLen property */ -exports.oldLen = (cs) => Changeset.unpack(cs).oldLen; +exports.oldLen = (cs) => { + padutils.warnDeprecated( + 'Changeset.oldLen(cs) is deprecated; use Changeset.unpack(cs).oldLen instead'); + return Changeset.unpack(cs).oldLen; +}; /** * Returns the length of the text after changeset is applied. * + * @deprecated Use `Changeset.unpack(cs).newLen` instead. * @param {string} cs - String representation of the Changeset * @returns {number} newLen property */ -exports.newLen = (cs) => Changeset.unpack(cs).newLen; +exports.newLen = (cs) => { + padutils.warnDeprecated( + 'Changeset.newLen(cs) is deprecated; use Changeset.unpack(cs).newLen instead'); + return Changeset.unpack(cs).newLen; +}; /** * Parses a string of serialized changeset operations. diff --git a/src/static/js/ace2_inner.js b/src/static/js/ace2_inner.js index 6f16c7f5b..9549e5761 100644 --- a/src/static/js/ace2_inner.js +++ b/src/static/js/ace2_inner.js @@ -1447,11 +1447,10 @@ function Ace2Inner(editorInfo, cssManagers) { }; const doRepApplyChangeset = (changes, insertsAfterSelection) => { - Changeset.unpack(changes).validate(); + const cs = Changeset.unpack(changes).validate(); - if (Changeset.oldLen(changes) !== rep.alltext.length) { - const errMsg = `${Changeset.oldLen(changes)}/${rep.alltext.length}`; - throw new Error(`doRepApplyChangeset length mismatch: ${errMsg}`); + if (cs.oldLen !== rep.alltext.length) { + throw new Error(`doRepApplyChangeset length mismatch: ${cs.oldLen}/${rep.alltext.length}`); } const editEvent = currentCallStack.editEvent; diff --git a/src/static/js/changesettracker.js b/src/static/js/changesettracker.js index 04abe7391..cfe82f4a3 100644 --- a/src/static/js/changesettracker.js +++ b/src/static/js/changesettracker.js @@ -167,7 +167,7 @@ const makeChangesetTracker = (scheduler, apool, aceCallbacksProvider) => { let cs = null; if (toSubmit) { submittedChangeset = toSubmit; - userChangeset = Changeset.identity(Changeset.newLen(toSubmit)); + userChangeset = Changeset.identity(Changeset.unpack(toSubmit).newLen); cs = toSubmit; }