Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | 13x 13x 13x 13x 13x 1914x 1914x 1914x 1914x 1588x 1588x 1588x 1588x 28773x 28773x 28773x 1588x 234943x 234943x 1914x 1914x 1914x 1914x 79476x 24778x 8368x 17722x 1142x 23924x 3x 3539x 24778x 24778x 24778x 24778x 24778x 66739x 66739x 66739x 66739x 24778x 24778x 3x 3x 3x 3x 3x 3x 9x 9x 8x 8x 8x 3x 3x 8368x 8368x 8368x 8368x 8368x 5473x 5473x 7217x 7217x 7217x 290x 6927x 9673x 9673x 9673x 6927x 8368x 8368x 17722x 17722x 17722x 17722x 17722x 17666x 17666x 95970x 95970x 95970x 48567x 47403x 47403x 17722x 17722x 3539x 3539x 3539x 3539x 3539x 3490x 3490x 52279x 52279x 52279x 39246x 13033x 13033x 13033x 3539x 3539x 1142x 1142x 1142x 1142x 1142x 1142x 23924x 23924x 23924x 23924x 23924x | import * as nodes from '../../../nodes';
import {fromBase64} from '@jsonjoy.com/base64/lib/fromBase64';
import {type ITimestampStruct, ts, ClockVector} from '../../../../json-crdt-patch/clock';
import {Model} from '../../../model';
import {SESSION} from '../../../../json-crdt-patch/constants';
import type * as types from './types';
export class Decoder {
public decode({time, root}: types.JsonCrdtVerboseDocument): Model {
const isServerClock = typeof time === 'number';
const doc = isServerClock ? Model.withServerClock(void 0, time) : Model.create(void 0, this.cClock(time));
this.cRoot(doc, root);
return doc;
}
protected cClock(timestamps: types.JsonCrdtVerboseLogicalTimestamp[]): ClockVector {
const [stamp] = timestamps;
const vectorClock = new ClockVector(stamp[0], stamp[1]);
const length = timestamps.length;
for (let i = 1; i < length; i++) {
const stamp = timestamps[i];
const [sessionId, time] = stamp;
vectorClock.observe(ts(sessionId, time), 1);
}
return vectorClock;
}
protected cTs(stamp: types.JsonCrdtVerboseTimestamp): ITimestampStruct {
const isServerClock = typeof stamp === 'number';
return isServerClock ? ts(SESSION.SERVER, stamp) : ts(stamp[0], stamp[1]);
}
protected cRoot(doc: Model, {value}: types.JsonCrdtVerboseVal): void {
const val = value ? this.cNode(doc, value) : new nodes.ConNode(doc.clock.tick(0), null);
const root = new nodes.RootNode(doc, val.id);
val.parent = root;
doc.root = root;
}
protected cNode(doc: Model, node: types.JsonCrdtNode): nodes.JsonNode {
switch (node.type) {
case 'obj':
return this.cObj(doc, node);
case 'arr':
return this.cArr(doc, node);
case 'str':
return this.cStr(doc, node);
case 'val':
return this.cVal(doc, node);
case 'con':
return this.cCon(doc, node);
case 'vec':
return this.cVec(doc, node);
case 'bin':
return this.cBin(doc, node);
}
throw new Error('UNKNOWN_NODE');
}
protected cObj(doc: Model, node: types.JsonCrdtVerboseObj): nodes.ObjNode {
const id = this.cTs(node.id);
const obj = new nodes.ObjNode(doc, id);
const map = node.map;
const keys = Object.keys(map);
for (const key of keys) {
const keyNode = map[key];
const child = this.cNode(doc, keyNode);
child.parent = obj;
obj.put(key, child.id);
}
doc.index.set(id, obj);
return obj;
}
protected cVec(doc: Model, node: types.JsonCrdtVerboseVec): nodes.VecNode {
const id = this.cTs(node.id);
const obj = new nodes.VecNode(doc, id);
const elements = obj.elements;
const map = node.map;
const length = map.length;
for (let i = 0; i < length; i++) {
const component = map[i];
if (!component) elements.push(undefined);
else {
const child = this.cNode(doc, component);
child.parent = obj;
elements.push(child.id);
}
}
doc.index.set(id, obj);
return obj;
}
protected cArr(doc: Model, node: types.JsonCrdtVerboseArr): nodes.ArrNode {
const id = this.cTs(node.id);
const rga = new nodes.ArrNode(doc, id);
const chunks = node.chunks;
const length = chunks.length;
if (length) {
let i = 0;
rga.ingest(length, () => {
const c = chunks[i++];
const id = this.cTs(c.id);
if (typeof (c as types.JsonCrdtVerboseTombstone).span === 'number')
return new nodes.ArrChunk(id, (c as types.JsonCrdtVerboseTombstone).span, undefined);
else {
const ids = (c as types.JsonCrdtVerboseArrChunk).value.map((n) => {
const child = this.cNode(doc, n);
child.parent = rga;
return child.id;
});
return new nodes.ArrChunk(id, ids.length, ids);
}
});
}
doc.index.set(id, rga);
return rga;
}
protected cStr(doc: Model, node: types.JsonCrdtVerboseStr): nodes.StrNode {
const id = this.cTs(node.id);
const rga = new nodes.StrNode(id);
const chunks = node.chunks;
const length = chunks.length;
if (length) {
let i = 0;
rga.ingest(length, () => {
const c = chunks[i++];
const id = this.cTs(c.id);
if (typeof (c as types.JsonCrdtVerboseTombstone).span === 'number')
return new nodes.StrChunk(id, (c as types.JsonCrdtVerboseTombstone).span, '');
else {
const value = (c as types.JsonCrdtVerboseStrChunk).value;
return new nodes.StrChunk(id, value.length, value);
}
});
}
doc.index.set(id, rga);
return rga;
}
protected cBin(doc: Model, node: types.JsonCrdtVerboseBin): nodes.BinNode {
const id = this.cTs(node.id);
const rga = new nodes.BinNode(id);
const chunks = node.chunks;
const length = chunks.length;
if (length) {
let i = 0;
rga.ingest(length, () => {
const c = chunks[i++];
const id = this.cTs(c.id);
if (typeof (c as types.JsonCrdtVerboseTombstone).span === 'number')
return new nodes.BinChunk(id, (c as types.JsonCrdtVerboseTombstone).span, undefined);
else {
const value = (c as types.JsonCrdtVerboseBinChunk).value;
const buf = fromBase64(value);
return new nodes.BinChunk(id, buf.length, buf);
}
});
}
doc.index.set(id, rga);
return rga;
}
protected cVal(doc: Model, node: types.JsonCrdtVerboseVal): nodes.ValNode {
const id = this.cTs(node.id);
const child = this.cNode(doc, node.value);
const obj = new nodes.ValNode(doc, id, child.id);
child.parent = obj;
doc.index.set(id, obj);
return obj;
}
protected cCon(doc: Model, node: types.JsonCrdtVerboseCon): nodes.ConNode {
const id = this.cTs(node.id);
const val = node.timestamp ? this.cTs(node.value as types.JsonCrdtVerboseLogicalTimestamp) : node.value;
const obj = new nodes.ConNode(id, val);
doc.index.set(id, obj);
return obj;
}
}
|