Skip to content

Commit 8bb7e96

Browse files
committed
Changeset: Migrate from opAssembler() to serializeOps()
1 parent 2a78791 commit 8bb7e96

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* `opIterator()`: Deprecated in favor of the new `deserializeOps()`
4343
generator function. Also, the unused start index parameter has been
4444
removed, as has the unused `lastIndex()` method on the returned object.
45+
* `opAssembler()`: Deprecated in favor of the new `serializeOps()` function.
4546
* `smartOpAssembler()`: The returned object's `appendOpWithText()` method is
4647
deprecated without a replacement available to plugins (if you need one,
4748
let us know and we can make the private `opsFromText()` function public).

src/node/utils/padDiff.js

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

212-
const assem = Changeset.opAssembler();
213-
214212
// create deleted attribs
215213
const authorAttrib = apool.putAttrib(['author', author || '']);
216214
const deletedAttrib = apool.putAttrib(['removed', true]);
217215
const attribs = `*${Changeset.numToString(authorAttrib)}*${Changeset.numToString(deletedAttrib)}`;
218216

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

232230
// return the modified changeset
233-
return Changeset.pack(unpacked.oldLen, unpacked.newLen, assem.toString(), unpacked.charBank);
231+
return Changeset.pack(unpacked.oldLen, unpacked.newLen, serializedOps, unpacked.charBank);
234232
};
235233

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

src/static/js/Changeset.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,16 @@ const copyOp = (op1, op2 = new Op()) => Object.assign(op2, op1);
315315
* @param {Iterable<Op>} ops - Iterable of operations to serialize.
316316
* @returns {string} A string containing the encoded op data (example: '|5=2p=v*4*5+1').
317317
*/
318-
const serializeOps = (ops) => {
318+
exports.serializeOps = (ops) => {
319319
let res = '';
320320
for (const op of ops) res += op.toString();
321321
return res;
322322
};
323323

324324
/**
325325
* Serializes a sequence of Ops.
326+
*
327+
* @deprecated Use `serializeOps` instead.
326328
*/
327329
class OpAssembler {
328330
constructor() {
@@ -413,7 +415,7 @@ class MergingOpAssembler {
413415
}
414416

415417
_serialize(finalize) {
416-
this._serialized = serializeOps(squashOps(this._ops, finalize));
418+
this._serialized = exports.serializeOps(squashOps(this._ops, finalize));
417419
}
418420

419421
clear() {
@@ -538,7 +540,7 @@ class SmartOpAssembler {
538540
}
539541

540542
_serialize(finalize) {
541-
this._serialized = serializeOps((function* () {
543+
this._serialized = exports.serializeOps((function* () {
542544
this._lengthChange = yield* canonicalizeOps(this._ops, finalize);
543545
}).call(this));
544546
}
@@ -649,9 +651,13 @@ exports.smartOpAssembler = () => new SmartOpAssembler();
649651
exports.mergingOpAssembler = () => new MergingOpAssembler();
650652

651653
/**
654+
* @deprecated Use `serializeOps` instead.
652655
* @returns {OpAssembler}
653656
*/
654-
exports.opAssembler = () => new OpAssembler();
657+
exports.opAssembler = () => {
658+
warnDeprecated('Changeset.opAssembler() is deprecated; use Changeset.serializeOps() instead');
659+
return new OpAssembler();
660+
};
655661

656662
/**
657663
* A custom made String Iterator

src/tests/frontend/specs/easysync.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ const randInt = (maxValue) => Math.floor(Math.random() * maxValue);
3131
describe('easysync', function () {
3232
it('throughIterator', async function () {
3333
const x = '-c*3*4+6|3=az*asdf0*1*2*3+1=1-1+1*0+1=1-1+1|c=c-1';
34-
const assem = Changeset.opAssembler();
35-
for (const op of Changeset.deserializeOps(x)) assem.append(op);
36-
expect(assem.toString()).to.equal(x);
34+
expect(Changeset.serializeOps(Changeset.deserializeOps(x))).to.equal(x);
3735
});
3836

3937
it('throughSmartAssembler', async function () {

0 commit comments

Comments
 (0)