Skip to content

Commit 66cd596

Browse files
committed
Changeset: Migrate from opAssembler() to serializeOps()
1 parent 623dc96 commit 66cd596

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* `opAttributeValue()`
2626
* `opIterator()`: Deprecated in favor of the new `deserializeOps()` generator
2727
function.
28+
* `opAssembler()`: Deprecated in favor of the new `serializeOps()` function.
2829
* `appendATextToAssembler()`: Deprecated in favor of the new `opsFromAText()`
2930
generator function.
3031
* `newOp()`: Deprecated in favor of the new `Op` class.

src/node/utils/padDiff.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -206,28 +206,26 @@ PadDiff.prototype._extendChangesetWithAuthor = (changeset, author, apool) => {
206206
// unpack
207207
const unpacked = Changeset.unpack(changeset);
208208

209-
const assem = Changeset.opAssembler();
210-
211209
// create deleted attribs
212210
const authorAttrib = apool.putAttrib(['author', author || '']);
213211
const deletedAttrib = apool.putAttrib(['removed', true]);
214212
const attribs = `*${Changeset.numToString(authorAttrib)}*${Changeset.numToString(deletedAttrib)}`;
215213

216-
for (const operator of Changeset.deserializeOps(unpacked.ops)) {
217-
if (operator.opcode === '-') {
218-
// this is a delete operator, extend it with the author
219-
operator.attribs = attribs;
220-
} else if (operator.opcode === '=' && operator.attribs) {
221-
// this is operator changes only attributes, let's mark which author did that
222-
operator.attribs += `*${Changeset.numToString(authorAttrib)}`;
214+
const serializedOps = Changeset.serializeOps((function* () {
215+
for (const operator of Changeset.deserializeOps(unpacked.ops)) {
216+
if (operator.opcode === '-') {
217+
// this is a delete operator, extend it with the author
218+
operator.attribs = attribs;
219+
} else if (operator.opcode === '=' && operator.attribs) {
220+
// this is operator changes only attributes, let's mark which author did that
221+
operator.attribs += `*${Changeset.numToString(authorAttrib)}`;
222+
}
223+
yield operator;
223224
}
224-
225-
// append the new operator to our assembler
226-
assem.append(operator);
227-
}
225+
})());
228226

229227
// return the modified changeset
230-
return Changeset.pack(unpacked.oldLen, unpacked.newLen, assem.toString(), unpacked.charBank);
228+
return Changeset.pack(unpacked.oldLen, unpacked.newLen, serializedOps, unpacked.charBank);
231229
};
232230

233231
// this method is 80% like Changeset.inverse. I just changed so instead of reverting,

src/static/js/Changeset.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,16 @@ const copyOp = (op1, op2 = new Op()) => Object.assign(op2, op1);
297297
* @param {Iterable<Op>} ops - Iterable of operations to serialize.
298298
* @returns {string} A string containing the encoded op data (example: '|5=2p=v*4*5+1').
299299
*/
300-
const serializeOps = (ops) => {
300+
exports.serializeOps = (ops) => {
301301
let res = '';
302302
for (const op of ops) res += op.toString();
303303
return res;
304304
};
305305

306306
/**
307307
* Serializes a sequence of Ops.
308+
*
309+
* @deprecated Use `serializeOps` instead.
308310
*/
309311
class OpAssembler {
310312
constructor() {
@@ -324,7 +326,7 @@ class OpAssembler {
324326
}
325327

326328
toString() {
327-
return serializeOps(this._ops);
329+
return exports.serializeOps(this._ops);
328330
}
329331
}
330332

@@ -334,12 +336,11 @@ class OpAssembler {
334336
*/
335337
class MergingOpAssembler {
336338
constructor() {
337-
this._assem = new OpAssembler();
338339
this.clear();
339340
}
340341

341342
clear() {
342-
this._assem.clear();
343+
this._assem = [];
343344
this._bufOp = new Op();
344345
// If we get, for example, insertions [xxx\n,yyy], those don't merge, but if we get
345346
// [xxx\n,yyy,zzz\n], that merges to [xxx\nyyyzzz\n]. This variable stores the length of yyy and
@@ -352,11 +353,11 @@ class MergingOpAssembler {
352353
if (isEndDocument && this._bufOp.opcode === '=' && !this._bufOp.attribs) {
353354
// final merged keep, leave it implicit
354355
} else {
355-
this._assem.append(this._bufOp);
356+
this._assem.push(copyOp(this._bufOp));
356357
if (this._bufOpAdditionalCharsAfterNewline) {
357358
this._bufOp.chars = this._bufOpAdditionalCharsAfterNewline;
358359
this._bufOp.lines = 0;
359-
this._assem.append(this._bufOp);
360+
this._assem.push(copyOp(this._bufOp));
360361
this._bufOpAdditionalCharsAfterNewline = 0;
361362
}
362363
}
@@ -390,7 +391,7 @@ class MergingOpAssembler {
390391

391392
toString() {
392393
this._flush();
393-
return this._assem.toString();
394+
return exports.serializeOps(this._assem);
394395
}
395396
}
396397

@@ -590,9 +591,14 @@ exports.smartOpAssembler = () => new SmartOpAssembler();
590591
exports.mergingOpAssembler = () => new MergingOpAssembler();
591592

592593
/**
594+
* @deprecated Use `serializeOps` instead.
593595
* @returns {OpAssembler}
594596
*/
595-
exports.opAssembler = () => new OpAssembler();
597+
exports.opAssembler = () => {
598+
padutils.warnWithStack(
599+
'Changeset.opAssembler() is deprecated; use Changeset.serializeOps() instead');
600+
return new OpAssembler();
601+
};
596602

597603
/**
598604
* A custom made String Iterator

src/tests/frontend/specs/easysync-assembler.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
const Changeset = require('../../../static/js/Changeset');
44

55
describe('easysync-assembler', function () {
6-
it('opAssembler', async function () {
6+
it('deserialize and serialize', async function () {
77
const x = '-c*3*4+6|3=az*asdf0*1*2*3+1=1-1+1*0+1=1-1+1|c=c-1';
8-
const assem = Changeset.opAssembler();
9-
for (const op of Changeset.deserializeOps(x)) assem.append(op);
10-
expect(assem.toString()).to.equal(x);
8+
expect(Changeset.serializeOps(Changeset.deserializeOps(x))).to.equal(x);
119
});
1210

1311
it('smartOpAssembler', async function () {

0 commit comments

Comments
 (0)