Skip to content

Commit 973e50d

Browse files
committed
Changeset: Migrate from mergingOpAssembler() to squashOps()
1 parent b486934 commit 973e50d

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
instead.
3838
* The `opAssembler()` function is deprecated; use the new `serializeOps()`
3939
function instead.
40+
* The `mergingOpAssembler()` function is deprecated; use the new
41+
`squashOps()` generator function with `serializeOps()` instead.
4042

4143
### Notable enhancements
4244

src/static/js/Changeset.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ class OpAssembler {
358358
* @param {boolean} finalize - If truthy, omits the final op if it is an attributeless keep op.
359359
* @yields {Op} The squashed operations.
360360
*/
361-
const squashOps = function* (ops, finalize) {
361+
exports.squashOps = function* (ops, finalize) {
362362
let prevOp = new exports.Op();
363363
// If we get, for example, insertions [xxx\n,yyy], those don't merge, but if we get
364364
// [xxx\n,yyy,zzz\n], that merges to [xxx\nyyyzzz\n]. This variable stores the length of yyy and
@@ -417,7 +417,7 @@ class MergingOpAssembler {
417417
}
418418

419419
_serialize(finalize) {
420-
this._serialized = exports.serializeOps(squashOps(this._ops, finalize));
420+
this._serialized = exports.serializeOps(exports.squashOps(this._ops, finalize));
421421
}
422422

423423
clear() {
@@ -459,14 +459,14 @@ const canonicalizeOps = function* (ops, finalize) {
459459
let lengthChange = 0;
460460

461461
const flushPlusMinus = function* () {
462-
yield* squashOps(minusOps, false);
462+
yield* exports.squashOps(minusOps, false);
463463
minusOps = [];
464-
yield* squashOps(plusOps, false);
464+
yield* exports.squashOps(plusOps, false);
465465
plusOps = [];
466466
};
467467

468468
const flushKeeps = function* (finalize) {
469-
yield* squashOps(keepOps, finalize);
469+
yield* exports.squashOps(keepOps, finalize);
470470
keepOps = [];
471471
};
472472

@@ -619,9 +619,13 @@ exports.checkRep = (cs) => {
619619
exports.smartOpAssembler = () => new SmartOpAssembler();
620620

621621
/**
622+
* @deprecated Use `squashOps` with `serializeOps` instead.
622623
* @returns {MergingOpAssembler}
623624
*/
624-
exports.mergingOpAssembler = () => new MergingOpAssembler();
625+
exports.mergingOpAssembler = () => {
626+
warnDeprecated('Changeset.mergingOpAssembler() is deprecated; use Changeset.squashOps() instead');
627+
return new MergingOpAssembler();
628+
};
625629

626630
/**
627631
* @deprecated Use `serializeOps` instead.
@@ -1332,28 +1336,26 @@ exports.mutateAttributionLines = (cs, lines, pool) => {
13321336
if (!lineIter || !lineIter.hasNext()) return new exports.Op();
13331337
return lineIter.next();
13341338
};
1335-
let lineAssem = null;
1339+
let lineOps = null;
13361340

13371341
const outputMutOp = (op) => {
1338-
if (!lineAssem) {
1339-
lineAssem = new MergingOpAssembler();
1340-
}
1341-
lineAssem.append(op);
1342+
if (lineOps == null) lineOps = [];
1343+
lineOps.push(op);
13421344
if (op.lines <= 0) return;
13431345
assert(op.lines === 1, "Can't have op.lines of ", op.lines, ' in attribution lines');
13441346
// ship it to the mut
1345-
mut.insert(lineAssem.toString(), 1);
1346-
lineAssem = null;
1347+
mut.insert(exports.serializeOps(exports.squashOps(lineOps, false)), 1);
1348+
lineOps = null;
13471349
};
13481350

13491351
let csOp = new exports.Op();
13501352
let attOp = new exports.Op();
13511353
while (csOp.opcode || csIter.hasNext() || attOp.opcode || isNextMutOp()) {
13521354
if (!csOp.opcode && csIter.hasNext()) csOp = csIter.next();
1353-
if ((!csOp.opcode) && (!attOp.opcode) && (!lineAssem) && (!(lineIter && lineIter.hasNext()))) {
1355+
if (!csOp.opcode && !attOp.opcode && lineOps == null && !(lineIter && lineIter.hasNext())) {
13541356
break; // done
13551357
} else if (csOp.opcode === '=' && csOp.lines > 0 && (!csOp.attribs) &&
1356-
(!attOp.opcode) && (!lineAssem) && (!(lineIter && lineIter.hasNext()))) {
1358+
!attOp.opcode && lineOps == null && !(lineIter && lineIter.hasNext())) {
13571359
// skip multiple lines; this is what makes small changes not order of the document size
13581360
mut.skipLines(csOp.lines);
13591361
csOp.opcode = '';
@@ -1377,7 +1379,7 @@ exports.mutateAttributionLines = (cs, lines, pool) => {
13771379
}
13781380
}
13791381

1380-
assert(!lineAssem, `line assembler not finished:${cs}`);
1382+
assert(lineOps == null, `line assembler not finished:${cs}`);
13811383
mut.close();
13821384
};
13831385

@@ -1387,24 +1389,20 @@ exports.mutateAttributionLines = (cs, lines, pool) => {
13871389
* @param {string[]} theAlines - collection of Attribution lines
13881390
* @returns {string} joined Attribution lines
13891391
*/
1390-
exports.joinAttributionLines = (theAlines) => {
1391-
const assem = new MergingOpAssembler();
1392-
for (const aline of theAlines) {
1393-
for (const op of new exports.OpIter(aline)) assem.append(op);
1394-
}
1395-
return assem.toString();
1396-
};
1392+
exports.joinAttributionLines = (theAlines) => exports.serializeOps(exports.squashOps((function* () {
1393+
for (const aline of theAlines) yield* new exports.OpIter(aline);
1394+
})(), false));
13971395

13981396
exports.splitAttributionLines = (attrOps, text) => {
1399-
const assem = new MergingOpAssembler();
1397+
let ops = [];
14001398
const lines = [];
14011399
let pos = 0;
14021400

14031401
const appendOp = (op) => {
1404-
assem.append(op);
1402+
ops.push(op);
14051403
if (op.lines > 0) {
1406-
lines.push(assem.toString());
1407-
assem.clear();
1404+
lines.push(exports.serializeOps(exports.squashOps(ops, false)));
1405+
ops = [];
14081406
}
14091407
pos += op.chars;
14101408
};

src/static/js/changesettracker.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ const makeChangesetTracker = (scheduler, apool, aceCallbacksProvider) => {
162162

163163
// Replace all added 'author' attribs with the value of the current user
164164
const cs = Changeset.unpack(userChangeset);
165-
const assem = Changeset.mergingOpAssembler();
165+
const ops = [];
166166

167167
for (const op of new Changeset.OpIter(cs.ops)) {
168168
if (op.opcode === '+') {
@@ -179,10 +179,10 @@ const makeChangesetTracker = (scheduler, apool, aceCallbacksProvider) => {
179179
});
180180
op.attribs = newAttrs;
181181
}
182-
assem.append(op);
182+
ops.push(op);
183183
}
184-
assem.endDocument();
185-
userChangeset = Changeset.pack(cs.oldLen, cs.newLen, assem.toString(), cs.charBank);
184+
const serializedOps = Changeset.serializeOps(Changeset.squashOps(ops, true));
185+
userChangeset = Changeset.pack(cs.oldLen, cs.newLen, serializedOps, cs.charBank);
186186
Changeset.checkRep(userChangeset);
187187
}
188188
if (Changeset.isIdentity(userChangeset)) toSubmit = null;

src/tests/frontend/specs/easysync.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,16 +646,16 @@ describe('easysync', function () {
646646

647647
const testSplitJoinAttributionLines = (randomSeed) => {
648648
const stringToOps = (str) => {
649-
const assem = Changeset.mergingOpAssembler();
650-
const o = new Changeset.Op('+');
651-
o.chars = 1;
649+
const ops = [];
652650
for (let i = 0; i < str.length; i++) {
653651
const c = str.charAt(i);
652+
const o = new Changeset.Op('+');
653+
o.chars = 1;
654654
o.lines = (c === '\n' ? 1 : 0);
655655
o.attribs = (c === 'a' || c === 'b' ? `*${c}` : '');
656-
assem.append(o);
656+
ops.push(o);
657657
}
658-
return assem.toString();
658+
return Changeset.serializeOps(Changeset.squashOps(ops, false));
659659
};
660660

661661
it(`testSplitJoinAttributionLines#${randomSeed}`, async function () {

0 commit comments

Comments
 (0)