From d5a7bf7a8f6eb67701a550f0944f8b621371e1c4 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Tue, 23 Nov 2021 01:51:23 -0500 Subject: [PATCH] Changeset: Implement `OpAssembler` with a new `serializeOps()` function --- src/static/js/Changeset.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/static/js/Changeset.js b/src/static/js/Changeset.js index 07fdee78f..6761efef2 100644 --- a/src/static/js/Changeset.js +++ b/src/static/js/Changeset.js @@ -291,6 +291,18 @@ exports.newOp = (optOpcode) => { */ const copyOp = (op1, op2 = new Op()) => Object.assign(op2, op1); +/** + * Converts an iterable of operation objects to wire format. + * + * @param {Iterable} ops - Iterable of operations to serialize. + * @returns {string} A string containing the encoded op data (example: '|5=2p=v*4*5+1'). + */ +const serializeOps = (ops) => { + let res = ''; + for (const op of ops) res += op.toString(); + return res; +}; + /** * Serializes a sequence of Ops. */ @@ -300,7 +312,7 @@ class OpAssembler { } clear() { - this._serialized = ''; + this._ops = []; } /** @@ -308,11 +320,11 @@ class OpAssembler { */ append(op) { assert(op instanceof Op, 'argument must be an instance of Op'); - this._serialized += op.toString(); + this._ops.push(copyOp(op)); } toString() { - return this._serialized; + return serializeOps(this._ops); } }