Use the new AttributeMap and Changeset APIs

pull/5308/head
Richard Hansen 2021-11-28 23:39:15 -05:00
parent f00b1ae89b
commit a02e45499d
3 changed files with 6 additions and 31 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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]));