Skip to content

Commit 84009d3

Browse files
committed
refactor(json-crdt): 💡 cleanup ModelApi, remove duplicate methods
1 parent a1699cb commit 84009d3

File tree

10 files changed

+24
-69
lines changed

10 files changed

+24
-69
lines changed

src/json-crdt/__demos__/events.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const model = Model.create(void 0, 1234); // 1234 is the session ID
2424
// });
2525

2626
// DOM Level 2 node events
27-
const root = model.api.r;
27+
const root = model.api;
2828
root.events.onViewChanges.listen(() => {
2929
console.log('Root value changed');
3030
});

src/json-crdt/json-patch/JsonPatch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ export class JsonPatch<N extends JsonNode = JsonNode<any>> {
167167
}
168168

169169
public get(path: string | Path): unknown {
170-
return (this.base ?? this.model.api.r).read(this.toPath(path));
170+
return (this.base ?? this.model.api).read(this.toPath(path));
171171
}
172172

173173
private json(steps: Path): unknown {
174-
const json = (this.base ?? this.model.api.r).read(steps);
174+
const json = (this.base ?? this.model.api).read(steps);
175175
if (json === undefined) throw new Error('NOT_FOUND');
176176
return json;
177177
}

src/json-crdt/model/Model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export class Model<N extends JsonNode = JsonNode<any>> implements Printable {
258258
* typed proxy wrapper around the value of the root node.
259259
*/
260260
public get s() {
261-
return this.api.r.proxy().val;
261+
return this.api.proxy().val;
262262
}
263263

264264
/**

src/json-crdt/model/__tests__/Model.schema.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ describe('.setSchema()', () => {
3535
num: s.con(123),
3636
}),
3737
);
38-
expect(model.api.r.get().node.id.sid).toBe(SESSION.GLOBAL);
39-
expect(model.api.r.get().get('id').node.id.sid).toBe(SESSION.GLOBAL);
40-
expect(model.api.r.get().get('num').node.id.sid).toBe(SESSION.GLOBAL);
38+
expect(model.api.get().node.id.sid).toBe(SESSION.GLOBAL);
39+
expect(model.api.get().get('id').node.id.sid).toBe(SESSION.GLOBAL);
40+
expect(model.api.get().get('num').node.id.sid).toBe(SESSION.GLOBAL);
4141
});
4242

4343
test('allows to specify custom session ID', () => {
@@ -46,9 +46,9 @@ describe('.setSchema()', () => {
4646
num: s.con(123),
4747
});
4848
const model = Model.create().setSchema(schema, false);
49-
expect(model.api.r.get().node.id.sid).toBe(model.clock.sid);
50-
expect(model.api.r.get().get('id').node.id.sid).toBe(model.clock.sid);
51-
expect(model.api.r.get().get('num').node.id.sid).toBe(model.clock.sid);
49+
expect(model.api.get().node.id.sid).toBe(model.clock.sid);
50+
expect(model.api.get().get('id').node.id.sid).toBe(model.clock.sid);
51+
expect(model.api.get().get('num').node.id.sid).toBe(model.clock.sid);
5252
});
5353

5454
test('resets session ID to user specified', () => {
@@ -59,12 +59,12 @@ describe('.setSchema()', () => {
5959
}),
6060
);
6161
expect(model.view().num).toBe(123);
62-
expect(model.api.r.get().get('num').node.id.sid).toBe(SESSION.GLOBAL);
63-
model.api.r.get().set({
62+
expect(model.api.get().get('num').node.id.sid).toBe(SESSION.GLOBAL);
63+
model.api.get().set({
6464
num: 456,
6565
});
6666
expect(model.view().num).toBe(456);
67-
expect(model.api.r.get().get('num').node.id.sid).not.toBe(SESSION.GLOBAL);
67+
expect(model.api.get().get('num').node.id.sid).not.toBe(SESSION.GLOBAL);
6868
});
6969
});
7070

src/json-crdt/model/api/__tests__/BinaryApi.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ test('.length()', () => {
3333
bin: s.bin(new Uint8Array([1, 2, 3])),
3434
}),
3535
);
36-
expect(doc.api.r.proxy().val.bin.$.length()).toBe(3);
36+
expect(doc.api.proxy().val.bin.$.length()).toBe(3);
3737
});

src/json-crdt/model/api/__tests__/ModelApi.proxy.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test('proxy API supports object types', () => {
1515
foo: 'asdf',
1616
bar: 1234,
1717
});
18-
const root = model.api.r.proxy();
18+
const root = model.api.proxy();
1919
const rootApi = root.$;
2020
expect(rootApi).toBeInstanceOf(ValApi);
2121
expect(rootApi.node).toBeInstanceOf(RootNode);
@@ -59,7 +59,7 @@ describe('supports all node types', () => {
5959
// console.log(model.root + '');
6060

6161
test('object as root node', () => {
62-
const proxy = model.api.r.proxy();
62+
const proxy = model.api.proxy();
6363
const obj = proxy.val;
6464
const objApi: ObjApi = obj.$;
6565
expect(objApi).toBeInstanceOf(ObjApi);

src/json-crdt/model/api/__tests__/NodeApi.diff.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('.merge()', () => {
3636
foo: 'bar',
3737
x: 123,
3838
});
39-
doc.api.r.merge({
39+
doc.api.merge({
4040
foo: 'baz!',
4141
x: 123,
4242
y: 'new',
@@ -57,7 +57,7 @@ describe('.merge()', () => {
5757
},
5858
},
5959
});
60-
doc.api.r.merge({
60+
doc.api.merge({
6161
foo: {
6262
bar: {
6363
baz: 'asdf!',

src/json-crdt/model/api/__tests__/NodeApi.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('.read()', () => {
8383

8484
test('retrieve deep within document', () => {
8585
const doc = createUntypedModel();
86-
expect(doc.api.r.read('/foo')).toEqual('bar');
86+
expect(doc.api.read('/foo')).toEqual('bar');
8787
expect(doc.api.read('/foo')).toEqual('bar');
8888
expect(doc.api.read('/arr/0')).toEqual(1);
8989
expect(doc.api.read('/arr/2/nested/1')).toEqual(2);

src/json-crdt/model/api/__tests__/in.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ describe('can use .in() method to reference any model node', () => {
66
const doc = Model.create();
77

88
test('can access root node', () => {
9-
const node = doc.api.r.asVal();
9+
const node = doc.api.asVal();
1010
expect(node.node).toBeInstanceOf(RootNode);
1111
});
1212

1313
doc.api.set({
1414
foo: [1],
1515
});
16-
doc.api.r.in('/foo/0').asVal().set({bar: 'baz'});
16+
doc.api.in('/foo/0').asVal().set({bar: 'baz'});
1717

1818
test('can access array element ValNode and its contents', () => {
19-
const register1 = doc.api.r.in().in('foo').in(0).asVal();
19+
const register1 = doc.api.in().in('foo').in(0).asVal();
2020
const register2 = doc.api.val('/foo/0');
2121
const register3 = doc.api.val(['foo', 0]);
2222
expect(register1).toBeInstanceOf(ValApi);
@@ -31,7 +31,7 @@ describe('can use .in() method to reference any model node', () => {
3131
doc.api.obj([]).set({val: doc.api.builder.jsonVal(123)});
3232

3333
test('can access object key ValNode and its contents', () => {
34-
const register1 = doc.api.r.in().in('val').asVal();
34+
const register1 = doc.api.in().in('val').asVal();
3535
const register2 = doc.api.val('/val');
3636
const register3 = doc.api.val(['val']);
3737
expect(register1).toBeInstanceOf(ValApi);

src/json-crdt/model/api/nodes.ts

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -900,13 +900,7 @@ export class ModelApi<N extends JsonNode = JsonNode> extends ValApi<RootNode<N>>
900900
} else throw new Error('UNKNOWN_NODE');
901901
}
902902

903-
/**
904-
* Local changes API for the root node.
905-
*/
906-
public get r() {
907-
return new ValApi(this.model.root, this);
908-
}
909-
903+
// TODO: Move to node.
910904
public get $(): JsonNodeToProxyPathNode<N> {
911905
return proxy$((path) => {
912906
try {
@@ -917,17 +911,6 @@ export class ModelApi<N extends JsonNode = JsonNode> extends ValApi<RootNode<N>>
917911
}, '$') as any;
918912
}
919913

920-
/**
921-
* Traverses the model starting from the root node and returns a local
922-
* changes API for a node at the given path.
923-
*
924-
* @param path Path at which to locate a node.
925-
* @returns A local changes API for a node at the given path.
926-
*/
927-
public in(path?: ApiPath) {
928-
return this.r.in(path);
929-
}
930-
931914
/**
932915
* Given a JSON/CBOR value, constructs CRDT nodes recursively out of it and
933916
* sets the root node of the model to the constructed nodes.
@@ -977,34 +960,6 @@ export class ModelApi<N extends JsonNode = JsonNode> extends ValApi<RootNode<N>>
977960
this.onLocalChange.emit(from);
978961
}
979962

980-
public select(path?: ApiPath, leaf?: boolean) {
981-
return this.r.select(path, leaf);
982-
}
983-
984-
/**
985-
* Reads the value at the given path in the model. If no path is provided,
986-
* returns the root node's view.
987-
*
988-
* @param path Path at which to read the value.
989-
* @returns The value at the given path, or the root node's view if no path
990-
* is provided.
991-
*/
992-
public read(path?: ApiPath): unknown {
993-
return this.r.read(path);
994-
}
995-
996-
public add(path: ApiPath, value: unknown): boolean {
997-
return this.r.add(path, value);
998-
}
999-
1000-
public replace(path: ApiPath, value: unknown): boolean {
1001-
return this.r.replace(path, value);
1002-
}
1003-
1004-
public remove(path: ApiPath, length?: number): boolean {
1005-
return this.r.remove(path, length);
1006-
}
1007-
1008963
private inTx = false;
1009964
public transaction(callback: () => void) {
1010965
if (this.inTx) callback();

0 commit comments

Comments
 (0)