Skip to content

Commit 142b393

Browse files
committed
feat(json-crdt): 🎸 move .$ getter proxy API to NodeApi class
1 parent 069a9c1 commit 142b393

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {Model} from '../../Model';
2+
import {s} from '../../../../json-crdt-patch';
3+
4+
describe('.$', () => {
5+
const model = Model.create(
6+
s.obj({
7+
obj: s.obj({
8+
str: s.str('asdf'),
9+
num: s.con(1234),
10+
nested: s.json({
11+
address: {
12+
street: s.str('1st Ave'),
13+
city: s.str('New York'),
14+
},
15+
}),
16+
}),
17+
vec: s.vec(s.con('asdf'), s.con(1234), s.con(true), s.con(null)),
18+
arr: s.arr([s.con('asdf'), s.val(s.con(0))]),
19+
bin: s.bin(new Uint8Array([1, 2, 3])),
20+
}),
21+
);
22+
23+
test('can use relative path starting from sub-object', () => {
24+
const obj = model.$.obj.nested.$;
25+
const str = obj?.$.address.city.$;
26+
expect(str?.view()).toBe('New York');
27+
});
28+
});

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,16 @@ export class NodeApi<N extends JsonNode = JsonNode> implements Printable {
329329
return {$: this} as unknown as types.ProxyNode<N>;
330330
}
331331

332+
public get $(): JsonNodeToProxyPathNode<N> {
333+
return proxy$((path) => {
334+
try {
335+
return this.api.wrap(this.find(path));
336+
} catch {
337+
return;
338+
}
339+
}, '$') as any;
340+
}
341+
332342
public toString(tab: string = ''): string {
333343
return 'api' + printTree(tab, [(tab) => this.node.toString(tab)]);
334344
}
@@ -900,17 +910,6 @@ export class ModelApi<N extends JsonNode = JsonNode> extends ValApi<RootNode<N>>
900910
} else throw new Error('UNKNOWN_NODE');
901911
}
902912

903-
// TODO: Move to node.
904-
public get $(): JsonNodeToProxyPathNode<N> {
905-
return proxy$((path) => {
906-
try {
907-
return this.wrap(this.find(path));
908-
} catch {
909-
return;
910-
}
911-
}, '$') as any;
912-
}
913-
914913
/**
915914
* Given a JSON/CBOR value, constructs CRDT nodes recursively out of it and
916915
* sets the root node of the model to the constructed nodes.

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ export type JsonNodeToProxyPathNode<N> = 0 extends 1 & N
5858
? {[K in keyof Obj]: JsonNodeToProxyPathNode<Obj[K]>} & JsonNodeToProxyPathNodeEnd<N>
5959
: N extends nodes.VecNode<infer Tuple>
6060
? {[K in keyof Tuple]: JsonNodeToProxyPathNode<Tuple[K]>} & JsonNodeToProxyPathNodeEnd<N>
61-
: nodes.JsonNode<unknown> extends N
62-
? ProxyPathNode<{$?: NodeApi<N extends nodes.JsonNode<unknown> ? N : nodes.JsonNode>}>
63-
: JsonNodeToProxyPathNodeEnd<N>;
61+
: N extends nodes.RootNode<infer M>
62+
? JsonNodeToProxyPathNode<M>
63+
: nodes.JsonNode<unknown> extends N
64+
? ProxyPathNode<{$?: NodeApi<N extends nodes.JsonNode<unknown> ? N : nodes.JsonNode>}>
65+
: JsonNodeToProxyPathNodeEnd<N>;
6466

6567
export type ProxyPathEndMethod<Args extends unknown[], Return> = (path: PathStep[], ...args: Args) => Return;
6668
export type ProxyPathUserEndMethod<M extends ProxyPathEndMethod<any[], any>> = M extends ProxyPathEndMethod<

0 commit comments

Comments
 (0)