Skip to content

Commit

Permalink
Changeset: Deprecate oldLen() and newLen() functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rhansen committed Nov 22, 2021
1 parent 410d02e commit 6b7d14a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/node/handler/PadMessageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,10 +632,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}`);
}

await pad.appendRevision(rebasedChangeset, thisSession.author);
Expand Down
14 changes: 12 additions & 2 deletions src/static/js/Changeset.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,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.warnWithStack(
'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.warnWithStack(
'Changeset.newLen(cs) is deprecated; use Changeset.unpack(cs).newLen instead');
return Changeset.unpack(cs).newLen;
};

/**
* Parses a string of serialized changeset operations.
Expand Down
7 changes: 3 additions & 4 deletions src/static/js/ace2_inner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/static/js/changesettracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 6b7d14a

Please sign in to comment.