From 9ea424c8f954c33d0b95aa3575e4db7ee1eba9ff Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Tue, 5 Oct 2021 13:10:06 -0400 Subject: [PATCH] Changeset: Turn `opAssembler()` into a real class --- src/static/js/Changeset.js | 49 +++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/src/static/js/Changeset.js b/src/static/js/Changeset.js index e3ae9d286..47c920e95 100644 --- a/src/static/js/Changeset.js +++ b/src/static/js/Changeset.js @@ -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