Skip to content

Commit

Permalink
feat(json-crdt): 🎸 add suport for "val" node type in sidecar codec
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Nov 6, 2023
1 parent 7afa423 commit 12ed6bf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
18 changes: 9 additions & 9 deletions src/json-crdt/codec/sidecar/binary/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/json-crdt/codec/sidecar/binary/Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
45 changes: 30 additions & 15 deletions src/json-crdt/codec/sidecar/binary/__tests__/Encoder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '');

// });
});

0 comments on commit 12ed6bf

Please sign in to comment.