Skip to content

Commit d7ac033

Browse files
committed
Changeset: Deprecate oldLen() and newLen() functions
1 parent fc958f8 commit d7ac033

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

src/node/handler/PadMessageHandler.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,11 +652,12 @@ const handleUserChanges = async (socket, message) => {
652652

653653
const prevText = pad.text();
654654

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

662663
try {

src/static/js/Changeset.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,18 +265,28 @@ class Changeset {
265265
/**
266266
* Returns the required length of the text before changeset can be applied.
267267
*
268+
* @deprecated Use `Changeset.unpack(cs).oldLen` instead.
268269
* @param {string} cs - String representation of the Changeset
269270
* @returns {number} oldLen property
270271
*/
271-
exports.oldLen = (cs) => Changeset.unpack(cs).oldLen;
272+
exports.oldLen = (cs) => {
273+
padutils.warnWithStack(
274+
'Changeset.oldLen(cs) is deprecated; use Changeset.unpack(cs).oldLen instead');
275+
return Changeset.unpack(cs).oldLen;
276+
};
272277

273278
/**
274279
* Returns the length of the text after changeset is applied.
275280
*
281+
* @deprecated Use `Changeset.unpack(cs).newLen` instead.
276282
* @param {string} cs - String representation of the Changeset
277283
* @returns {number} newLen property
278284
*/
279-
exports.newLen = (cs) => Changeset.unpack(cs).newLen;
285+
exports.newLen = (cs) => {
286+
padutils.warnWithStack(
287+
'Changeset.newLen(cs) is deprecated; use Changeset.unpack(cs).newLen instead');
288+
return Changeset.unpack(cs).newLen;
289+
};
280290

281291
/**
282292
* Parses a string of serialized changeset operations.

src/static/js/ace2_inner.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,11 +1446,10 @@ function Ace2Inner(editorInfo, cssManagers) {
14461446
};
14471447

14481448
const doRepApplyChangeset = (changes, insertsAfterSelection) => {
1449-
Changeset.unpack(changes).validate();
1449+
const cs = Changeset.unpack(changes).validate();
14501450

1451-
if (Changeset.oldLen(changes) !== rep.alltext.length) {
1452-
const errMsg = `${Changeset.oldLen(changes)}/${rep.alltext.length}`;
1453-
throw new Error(`doRepApplyChangeset length mismatch: ${errMsg}`);
1451+
if (cs.oldLen !== rep.alltext.length) {
1452+
throw new Error(`doRepApplyChangeset length mismatch: ${cs.oldLen}/${rep.alltext.length}`);
14541453
}
14551454

14561455
const editEvent = currentCallStack.editEvent;

src/static/js/changesettracker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ const makeChangesetTracker = (scheduler, apool, aceCallbacksProvider) => {
183183
let cs = null;
184184
if (toSubmit) {
185185
submittedChangeset = toSubmit;
186-
userChangeset = Changeset.identity(Changeset.newLen(toSubmit));
186+
userChangeset = Changeset.identity(Changeset.unpack(toSubmit).newLen);
187187

188188
cs = toSubmit;
189189
}

0 commit comments

Comments
 (0)