Skip to content

Commit

Permalink
fix(json-crdt): 馃悰 make extension schema copyable
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed May 3, 2024
1 parent f0435a1 commit 7bbee8f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/json-crdt/extensions/ExtensionNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ export abstract class ExtensionNode<N extends JsonNode> implements ExtensionJson
// ---------------------------------------------------------------- Printable

public toString(tab?: string, parentId?: ITimestampStruct): string {
return this.name() + (parentId ? ' ' + printTs(parentId) : '') + ', ' + this.data.toString(tab);
return this.name() + (parentId ? ' ' + printTs(parentId) : '') + ' ' + this.data.toString(tab);
}
}
1 change: 0 additions & 1 deletion src/json-crdt/nodes/vec/VecNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ export class VecNode<Value extends JsonNode[] = JsonNode[]> implements JsonNode<
* @ignore
*/
public children(callback: (node: JsonNode) => void) {
if (this.isExt()) return;
const elements = this.elements;
const length = elements.length;
const index = this.doc.index;
Expand Down
26 changes: 18 additions & 8 deletions src/json-crdt/schema/__tests__/toSchema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {deepEqual} from '../../../json-equal/deepEqual';
import {cmpUint8Array} from '@jsonjoy.com/util/lib/buffers/cmpUint8Array';
import {Model} from '../../model';
import {toSchema} from '../toSchema';
import {cnt} from '../../../json-crdt-extensions';

const cmp = (a: NodeBuilder, b: NodeBuilder): boolean => {
if (a instanceof nodes.con && b instanceof nodes.con) return deepEqual(a.raw, b.raw);
Expand Down Expand Up @@ -62,7 +63,7 @@ test('can infer schema of a document nodes', () => {
vec: s.vec(s.con(1), s.con({foo: 'bar'})),
arr: s.arr([s.con(1), s.con({foo: 'bar'})]),
});
const model = Model.withLogicalClock().setSchema(schema);
const model = Model.create(schema);
const node = model.root.node();
const schema2 = toSchema(node);
expect(cmp(schema, schema2)).toBe(true);
Expand All @@ -77,13 +78,22 @@ test('can infer schema of a document nodes', () => {
expect(cmp(con, objSchema)).toBe(false);
});

test('can infer schema of a typed model', () => {
test('can copy a model with extension', () => {
const schema = s.obj({
id: s.con('id'),
val: s.val(s.str('world')),
count: cnt.new(1),
});
const model = Model.create();
model.api.root(schema);
model.ext.register(cnt);
const copy = toSchema(model.root.node());
const model2 = Model.create(copy, model.clock.sid);
expect(model2.view()).toMatchObject({
count: [
expect.any(Uint8Array),
{
[model2.clock.sid.toString(36)]: 1,
},
],
});
const model = Model.withLogicalClock().setSchema(schema);
const schema2 = toSchema(model.root.node());
expect(schema2.obj.id).toBeInstanceOf(nodes.con);
expect(schema2.obj.val).toBeInstanceOf(nodes.val);
});

0 comments on commit 7bbee8f

Please sign in to comment.