Changeset: Turn opAssembler() into a real class

This commit is contained in:
Richard Hansen 2021-10-05 13:10:06 -04:00
parent 748d661495
commit 9ea424c8f9

View file

@ -293,12 +293,28 @@ const copyOp = (op1, op2 = new Op()) => Object.assign(op2, op1);
/**
* Serializes a sequence of Ops.
*
* @typedef {object} OpAssembler
* @property {Function} append -
* @property {Function} clear -
* @property {Function} toString -
*/
class OpAssembler {
constructor() {
this.clear();
}
clear() {
this._serialized = '';
}
/**
* @param {Op} op - Operation to add. Ownership remains with the caller.
*/
append(op) {
assert(op instanceof Op, 'argument must be an instance of Op');
this._serialized += op.toString();
}
toString() {
return this._serialized;
}
}
/**
* Efficiently merges consecutive operations that are mergeable, ignores no-ops, and drops final
@ -586,28 +602,7 @@ exports.mergingOpAssembler = () => {
/**
* @returns {OpAssembler}
*/
exports.opAssembler = () => {
let serialized = '';
/**
* @param {Op} op - Operation to add. Ownership remains with the caller.
*/
const append = (op) => {
assert(op instanceof Op, 'argument must be an instance of Op');
serialized += op.toString();
};
const toString = () => serialized;
const clear = () => {
serialized = '';
};
return {
append,
toString,
clear,
};
};
exports.opAssembler = () => new OpAssembler();
/**
* A custom made String Iterator