diff --git a/src/json-crdt/codec/sidecar/binary/Decoder.ts b/src/json-crdt/codec/sidecar/binary/Decoder.ts index 5b9a15c864..800d4f9be3 100644 --- a/src/json-crdt/codec/sidecar/binary/Decoder.ts +++ b/src/json-crdt/codec/sidecar/binary/Decoder.ts @@ -75,8 +75,8 @@ export class Decoder { switch (major) { case CRDT_MAJOR.CON: return this.cCon(view, id); - // case CRDT_MAJOR.VAL: - // return this.cVal(id); + case CRDT_MAJOR.VAL: + return this.cVal(view, id); // case CRDT_MAJOR.OBJ: // return this.cObj(id, length); // case CRDT_MAJOR.VEC: @@ -98,13 +98,13 @@ export class Decoder { return node; } - // protected cVal(id: ITimestampStruct): ValNode { - // const child = this.cNode(); - // const doc = this.doc; - // const node = new ValNode(doc, id, child.id); - // doc.index.set(id, node); - // return node; - // } + protected cVal(view: unknown, id: ITimestampStruct): ValNode { + const child = this.cNode(view); + const doc = this.doc; + const node = new ValNode(doc, id, child.id); + doc.index.set(id, node); + return node; + } // protected cObj(id: ITimestampStruct, length: number): ObjNode { // const obj = new ObjNode(this.doc, id); diff --git a/src/json-crdt/codec/sidecar/binary/Encoder.ts b/src/json-crdt/codec/sidecar/binary/Encoder.ts index 3fdb3e2412..abbd1bff49 100644 --- a/src/json-crdt/codec/sidecar/binary/Encoder.ts +++ b/src/json-crdt/codec/sidecar/binary/Encoder.ts @@ -93,9 +93,9 @@ export class Encoder { } protected cVal(node: nodes.ValNode): void { - // this.ts(node.id); - // this.writeTL(CRDT_MAJOR_OVERLAY.VAL, 0); - // this.cNode(node.node()); + this.ts(node.id); + this.writeTL(CRDT_MAJOR_OVERLAY.VAL, 0); + this.cNode(node.node()); } protected cObj(node: nodes.ObjNode): void { diff --git a/src/json-crdt/codec/sidecar/binary/__tests__/Encoder.spec.ts b/src/json-crdt/codec/sidecar/binary/__tests__/Encoder.spec.ts index 91fe62b569..3a00ee6ca0 100644 --- a/src/json-crdt/codec/sidecar/binary/__tests__/Encoder.spec.ts +++ b/src/json-crdt/codec/sidecar/binary/__tests__/Encoder.spec.ts @@ -19,24 +19,39 @@ test('con', () => { expect(decoded.clock.time).toBe(model.clock.time); }); -// test('con', () => { -// const model = Model.withLogicalClock(); -// const encoder = new Encoder(); -// const decoder = new Decoder(); -// const cborDecoder = new CborDecoder(); -// model.api.root(s.con(123)); +test('val', () => { + const model = Model.withLogicalClock(); + const encoder = new Encoder(); + const decoder = new Decoder(); + const cborDecoder = new CborDecoder(); + model.api.root(s.val(s.con(123))); + const [view, meta] = encoder.encode(model); + const viewDecoded = cborDecoder.decode(view); + const decoded = decoder.decode(viewDecoded, meta); + expect(model.view()).toEqual(decoded.view()); + expect(model.view()).toEqual(viewDecoded); + expect(decoded.clock.sid).toBe(model.clock.sid); + expect(decoded.clock.time).toBe(model.clock.time); +}); + +test('obj', () => { + const model = Model.withLogicalClock(); + const encoder = new Encoder(); + const decoder = new Decoder(); + const cborDecoder = new CborDecoder(); + model.api.root(s.val(s.con(123))); -// console.log(model + ''); + console.log(model + ''); -// const [view, meta] = encoder.encode(model); + const [view, meta] = encoder.encode(model); -// console.log(view); -// console.log(meta); + console.log(view); + console.log(meta); -// const viewDecoded = cborDecoder.decode(view); -// const decoded = decoder.decode(viewDecoded, meta); + const viewDecoded = cborDecoder.decode(view); + const decoded = decoder.decode(viewDecoded, meta); -// console.log(viewDecoded); -// console.log(decoded + ''); + console.log(viewDecoded); + console.log(decoded + ''); -// }); +});