Skip to content

Commit 16e946b

Browse files
committed
feat(json-crdt): 🎸 make view decoder support latest binary codec
1 parent 1105357 commit 16e946b

File tree

2 files changed

+40
-60
lines changed

2 files changed

+40
-60
lines changed

src/json-crdt/codec/structural/binary/Encoder.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ export class Encoder extends CborEncoder<CrdtWriter> {
9696
else if (node instanceof BinNode) this.cBin(node);
9797
}
9898

99-
protected cObj(obj: ObjNode): void {
100-
this.ts(obj.id);
101-
const keys = obj.keys;
99+
protected cObj(node: ObjNode): void {
100+
this.ts(node.id);
101+
const keys = node.keys;
102102
this.writeTL(CRDT_MAJOR_OVERLAY.OBJ, keys.size);
103103
keys.forEach(this.cKey);
104104
}
@@ -108,10 +108,10 @@ export class Encoder extends CborEncoder<CrdtWriter> {
108108
this.cNode(this.doc.index.get(val)!);
109109
};
110110

111-
protected cVec(obj: VecNode): void {
112-
const elements = obj.elements;
111+
protected cVec(node: VecNode): void {
112+
const elements = node.elements;
113113
const length = elements.length;
114-
this.ts(obj.id);
114+
this.ts(node.id);
115115
this.writeTL(CRDT_MAJOR_OVERLAY.VEC, length);
116116
const index = this.doc.index;
117117
for (let i = 0; i < length; i++) {
@@ -121,13 +121,13 @@ export class Encoder extends CborEncoder<CrdtWriter> {
121121
}
122122
}
123123

124-
protected cArr(obj: ArrNode): void {
124+
protected cArr(node: ArrNode): void {
125125
const ts = this.ts;
126126
const writer = this.writer;
127-
ts(obj.id);
128-
this.writeTL(CRDT_MAJOR_OVERLAY.ARR, obj.size());
127+
ts(node.id);
128+
this.writeTL(CRDT_MAJOR_OVERLAY.ARR, node.size());
129129
const index = this.doc.index;
130-
for (let chunk = obj.first(); chunk; chunk = obj.next(chunk)) {
130+
for (let chunk = node.first(); chunk; chunk = node.next(chunk)) {
131131
const span = chunk.span;
132132
const deleted = chunk.del;
133133
writer.b1vu28(deleted, span);
@@ -138,12 +138,12 @@ export class Encoder extends CborEncoder<CrdtWriter> {
138138
}
139139
}
140140

141-
protected cStr(obj: StrNode): void {
141+
protected cStr(node: StrNode): void {
142142
const ts = this.ts;
143143
const writer = this.writer;
144-
ts(obj.id);
145-
this.writeTL(CRDT_MAJOR_OVERLAY.STR, obj.size());
146-
for (let chunk = obj.first(); chunk; chunk = obj.next(chunk)) {
144+
ts(node.id);
145+
this.writeTL(CRDT_MAJOR_OVERLAY.STR, node.size());
146+
for (let chunk = node.first(); chunk; chunk = node.next(chunk)) {
147147
ts(chunk.id);
148148
if (chunk.del) {
149149
writer.u8(0);

src/json-crdt/codec/structural/binary/ViewDecoder.ts

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {CrdtReader} from '../../../../json-crdt-patch/util/binary/CrdtDecoder';
2-
import {MsgPackDecoderFast} from '../../../../json-pack/msgpack';
2+
import {CborDecoderBase} from '../../../../json-pack/cbor/CborDecoderBase';
3+
import {CRDT_MAJOR} from './constants';
34

4-
export class ViewDecoder extends MsgPackDecoderFast<CrdtReader> {
5+
export class ViewDecoder extends CborDecoderBase<CrdtReader> {
56
protected time: number = -1;
67

78
constructor() {
@@ -32,49 +33,30 @@ export class ViewDecoder extends MsgPackDecoderFast<CrdtReader> {
3233
return !peek ? undefined : this.cNode();
3334
}
3435

35-
public cNode(): unknown {
36+
protected cNode(): unknown {
3637
const reader = this.reader;
3738
this.ts();
38-
const byte = reader.u8();
39-
if (byte <= 0b10001111) return this.cObj(byte & 0b1111);
40-
else if (byte <= 0b10011111) return this.cArr(byte & 0b1111);
41-
else if (byte <= 0b10111111) return this.cStr(byte & 0b11111);
42-
else {
43-
switch (byte) {
44-
case 0xc4:
45-
return this.cBin(reader.u8());
46-
case 0xc5:
47-
return this.cBin(reader.u16());
48-
case 0xc6:
49-
return this.cBin(reader.u32());
50-
case 0xd4:
51-
return this.val();
52-
case 0xd5:
53-
return null;
54-
case 0xd6:
55-
return this.cNode();
56-
case 0xde:
57-
return this.cObj(reader.u16());
58-
case 0xdf:
59-
return this.cObj(reader.u32());
60-
case 0xdc:
61-
return this.cArr(reader.u16());
62-
case 0xdd:
63-
return this.cArr(reader.u32());
64-
case 0xd9:
65-
return this.cStr(reader.u8());
66-
case 0xda:
67-
return this.cStr(reader.u16());
68-
case 0xdb:
69-
return this.cStr(reader.u32());
70-
case 0xc7:
71-
return this.cTup();
72-
}
39+
const octet = reader.u8();
40+
const major = octet >> 5;
41+
const minor = octet & 0b11111;
42+
const length = minor < 24 ? minor : minor === 24 ? reader.u8() : minor === 25 ? reader.u16() : reader.u32();
43+
switch (major) {
44+
case CRDT_MAJOR.CON: return this.cCon(length);
45+
case CRDT_MAJOR.VAL: return this.cNode();
46+
case CRDT_MAJOR.VEC: return this.cVec(length);
47+
case CRDT_MAJOR.OBJ: return this.cObj(length);
48+
case CRDT_MAJOR.STR: return this.cStr(length);
49+
case CRDT_MAJOR.BIN: return this.cBin(length);
50+
case CRDT_MAJOR.ARR: return this.cArr(length);
7351
}
7452
return undefined;
7553
}
7654

77-
public cObj(length: number): Record<string, unknown> {
55+
protected cCon(length: number): unknown {
56+
return !length ? this.val() : (this.ts(), null);
57+
}
58+
59+
protected cObj(length: number): Record<string, unknown> {
7860
const obj: Record<string, unknown> = {};
7961
for (let i = 0; i < length; i++) {
8062
const key: string = this.key();
@@ -84,10 +66,8 @@ export class ViewDecoder extends MsgPackDecoderFast<CrdtReader> {
8466
return obj;
8567
}
8668

87-
public cTup(): unknown[] {
69+
protected cVec(length: number): unknown[] {
8870
const reader = this.reader;
89-
const length = this.reader.u8();
90-
reader.x++;
9171
const obj: unknown[] = [];
9272
for (let i = 0; i < length; i++) {
9373
const octet = reader.peak();
@@ -99,7 +79,7 @@ export class ViewDecoder extends MsgPackDecoderFast<CrdtReader> {
9979
return obj;
10080
}
10181

102-
public cArr(length: number): unknown[] {
82+
protected cArr(length: number): unknown[] {
10383
const arr: unknown[] = [];
10484
for (let i = 0; i < length; i++) {
10585
const values = this.cArrChunk();
@@ -108,7 +88,7 @@ export class ViewDecoder extends MsgPackDecoderFast<CrdtReader> {
10888
return arr;
10989
}
11090

111-
private cArrChunk(): unknown[] | undefined {
91+
protected cArrChunk(): unknown[] | undefined {
11292
const [deleted, length] = this.reader.b1vu28();
11393
this.ts();
11494
if (deleted) {
@@ -120,7 +100,7 @@ export class ViewDecoder extends MsgPackDecoderFast<CrdtReader> {
120100
}
121101
}
122102

123-
public cStr(length: number): string {
103+
protected cStr(length: number): string {
124104
const reader = this.reader;
125105
let str = '';
126106
for (let i = 0; i < length; i++) {
@@ -137,7 +117,7 @@ export class ViewDecoder extends MsgPackDecoderFast<CrdtReader> {
137117
return str;
138118
}
139119

140-
public cBin(length: number): Uint8Array {
120+
protected cBin(length: number): Uint8Array {
141121
const reader = this.reader;
142122
const buffers: Uint8Array[] = [];
143123
let totalLength = 0;

0 commit comments

Comments
 (0)