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 Oct 19, 2021
1 parent 6d7a54e commit 68d5830
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/node/handler/PadMessageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,11 +654,12 @@ const handleUserChanges = async (socket, message) => {

const prevText = pad.text();

if (Changeset.oldLen(changeset) !== prevText.length) {
if (Changeset.unpack(changeset).oldLen !== prevText.length) {
socket.json.send({disconnect: 'badChangeset'});
stats.meter('failedChangesets').mark();
throw new Error(`Can't apply USER_CHANGES ${changeset} with oldLen ` +
`${Changeset.oldLen(changeset)} to document of length ${prevText.length}`);
throw new Error(
`Can't apply USER_CHANGES ${changeset} with oldLen ` +
`${Changeset.unpack(changeset).oldLen} to document of length ${prevText.length}`);
}

try {
Expand Down
12 changes: 10 additions & 2 deletions src/static/js/Changeset.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,18 +246,26 @@ 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) => {
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) => {
warnDeprecated('Changeset.newLen(cs) is deprecated; use Changeset.unpack(cs).newLen instead');
return Changeset.unpack(cs).newLen;
};

/**
* Iterator over a changeset's 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 @@ -1474,11 +1474,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 @@ -192,7 +192,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 68d5830

Please sign in to comment.