Skip to content

Commit 623dc96

Browse files
committed
Changeset: Implement OpAssembler with a new serializeOps() function
1 parent f606569 commit 623dc96

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/static/js/Changeset.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,18 @@ exports.newOp = (optOpcode) => {
291291
*/
292292
const copyOp = (op1, op2 = new Op()) => Object.assign(op2, op1);
293293

294+
/**
295+
* Converts an iterable of operation objects to wire format.
296+
*
297+
* @param {Iterable<Op>} ops - Iterable of operations to serialize.
298+
* @returns {string} A string containing the encoded op data (example: '|5=2p=v*4*5+1').
299+
*/
300+
const serializeOps = (ops) => {
301+
let res = '';
302+
for (const op of ops) res += op.toString();
303+
return res;
304+
};
305+
294306
/**
295307
* Serializes a sequence of Ops.
296308
*/
@@ -300,19 +312,19 @@ class OpAssembler {
300312
}
301313

302314
clear() {
303-
this._serialized = '';
315+
this._ops = [];
304316
}
305317

306318
/**
307319
* @param {Op} op - Operation to add. Ownership remains with the caller.
308320
*/
309321
append(op) {
310322
assert(op instanceof Op, 'argument must be an instance of Op');
311-
this._serialized += op.toString();
323+
this._ops.push(copyOp(op));
312324
}
313325

314326
toString() {
315-
return this._serialized;
327+
return serializeOps(this._ops);
316328
}
317329
}
318330

0 commit comments

Comments
 (0)