From a02e45499deea04af0481fc4f51fb2f80b7bb78a Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sun, 28 Nov 2021 23:39:15 -0500 Subject: [PATCH] Use the new AttributeMap and Changeset APIs --- src/node/db/Pad.js | 25 +++------------------ src/static/js/broadcast.js | 4 +--- src/tests/backend/specs/contentcollector.js | 8 ++----- 3 files changed, 6 insertions(+), 31 deletions(-) diff --git a/src/node/db/Pad.js b/src/node/db/Pad.js index 80ac5369d..0379f512a 100644 --- a/src/node/db/Pad.js +++ b/src/node/db/Pad.js @@ -3,7 +3,7 @@ * The pad object, defined with joose */ - +const AttributeMap = require('../../static/js/AttributeMap'); const Changeset = require('../../static/js/Changeset'); const ChatMessage = require('../../static/js/ChatMessage'); const AttributePool = require('../../static/js/AttributePool'); @@ -647,16 +647,6 @@ Pad.prototype.check = async function () { assert(pool instanceof AttributePool); await pool.check(); - const decodeAttribString = function* (str) { - const re = /\*([0-9a-z]+)|./gy; - let match; - while ((match = re.exec(str)) != null) { - const [m, n] = match; - if (n == null) throw new Error(`invalid character in attribute string: ${m}`); - yield Number.parseInt(n, 36); - } - }; - const authors = new Set(); pool.eachAttrib((k, v) => { if (k === 'author' && v) authors.add(v); @@ -681,9 +671,7 @@ Pad.prototype.check = async function () { Changeset.checkRep(changeset); const unpacked = Changeset.unpack(changeset); let text = atext.text; - const iter = Changeset.opIterator(unpacked.ops); - while (iter.hasNext()) { - const op = iter.next(); + for (const op of Changeset.deserializeOps(unpacked.ops)) { if (['=', '-'].includes(op.opcode)) { assert(text.length >= op.chars); const consumed = text.slice(0, op.chars); @@ -692,14 +680,7 @@ Pad.prototype.check = async function () { if (op.lines > 0) assert(consumed.endsWith('\n')); text = text.slice(op.chars); } - let prevK = null; - for (const n of decodeAttribString(op.attribs)) { - const attrib = pool.getAttrib(n); - assert(attrib != null); - const [k] = attrib; - assert(prevK == null || prevK < k); - prevK = k; - } + assert.equal(op.attribs, AttributeMap.fromString(op.attribs, pool).toString()); } atext = Changeset.applyToAText(changeset, atext, pool); assert.deepEqual(await this.getInternalRevisionAText(r), atext); diff --git a/src/static/js/broadcast.js b/src/static/js/broadcast.js index 4014d5829..cd2211ae1 100644 --- a/src/static/js/broadcast.js +++ b/src/static/js/broadcast.js @@ -117,9 +117,7 @@ const loadBroadcastJS = (socket, sendSocketMsg, fireWhenAllScriptsAreLoaded, Bro getActiveAuthors() { const authorIds = new Set(); for (const aline of this.alines) { - const opIter = Changeset.opIterator(aline); - while (opIter.hasNext()) { - const op = opIter.next(); + for (const op of Changeset.deserializeOps(aline)) { for (const [k, v] of attributes.attribsFromString(op.attribs, this.apool)) { if (k !== 'author') continue; if (v) authorIds.add(v); diff --git a/src/tests/backend/specs/contentcollector.js b/src/tests/backend/specs/contentcollector.js index c2cee5338..a4696307e 100644 --- a/src/tests/backend/specs/contentcollector.js +++ b/src/tests/backend/specs/contentcollector.js @@ -346,9 +346,7 @@ describe(__filename, function () { // numbers do not change if the attribute processing code changes.) for (const attrib of knownAttribs) apool.putAttrib(attrib); for (const aline of tc.wantAlines) { - const opIter = Changeset.opIterator(aline); - while (opIter.hasNext()) { - const op = opIter.next(); + for (const op of Changeset.deserializeOps(aline)) { for (const n of attributes.decodeAttribString(op.attribs)) { assert(n < knownAttribs.length); } @@ -375,9 +373,7 @@ describe(__filename, function () { gotAttribs.push(gotAlineAttribs); const wantAlineAttribs = []; wantAttribs.push(wantAlineAttribs); - const opIter = Changeset.opIterator(aline); - while (opIter.hasNext()) { - const op = opIter.next(); + for (const op of Changeset.deserializeOps(aline)) { const gotOpAttribs = [...attributes.attribsFromString(op.attribs, apool)]; gotAlineAttribs.push(gotOpAttribs); wantAlineAttribs.push(attributes.sort([...gotOpAttribs]));