Skip to content

Commit faeb17e

Browse files
committed
refactor(json-crdt): 💡 rename strictly typed .proxy() apy got get .s
1 parent 142b393 commit faeb17e

File tree

5 files changed

+46
-32
lines changed

5 files changed

+46
-32
lines changed

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.proxy().val;
261+
return this.api.s.val;
262262
}
263263

264264
/**

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.proxy().val.bin.$.length()).toBe(3);
36+
expect(doc.api.s.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.proxy();
18+
const root = model.api.s;
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.proxy();
62+
const proxy = model.api.s;
6363
const obj = proxy.val;
6464
const objApi: ObjApi = obj.$;
6565
expect(objApi).toBeInstanceOf(ObjApi);
Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
import {Model} from '../../Model';
22
import {s} from '../../../../json-crdt-patch';
33

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-
}),
4+
const model = Model.create(
5+
s.obj({
6+
obj: s.obj({
7+
str: s.str('asdf'),
8+
num: s.con(1234),
9+
nested: s.json({
10+
address: {
11+
street: s.str('1st Ave'),
12+
city: s.str('New York'),
13+
},
1614
}),
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])),
2015
}),
21-
);
16+
val: s.val(s.con('Hello')),
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+
);
2222

23+
describe('.$', () => {
2324
test('can use relative path starting from sub-object', () => {
2425
const obj = model.$.obj.nested.$;
2526
const str = obj?.$.address.city.$;
2627
expect(str?.view()).toBe('New York');
2728
});
2829
});
30+
31+
describe('.s', () => {
32+
test('can use relative path starting from sub-object', () => {
33+
const obj = model.s.obj.nested.$;
34+
const str = obj.s.address.city.$;
35+
expect(str?.view()).toBe('New York');
36+
});
37+
38+
test('can resolve a "val" node', () => {
39+
const str = model.s.val.val.$;
40+
expect(str.view()).toBe('Hello');
41+
});
42+
});

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ export class NodeApi<N extends JsonNode = JsonNode> implements Printable {
325325
diff.merge(this, value);
326326
}
327327

328-
public proxy(): types.ProxyNode<N> {
328+
public get s(): types.ProxyNode<N> {
329329
return {$: this} as unknown as types.ProxyNode<N>;
330330
}
331331

@@ -353,7 +353,7 @@ export class ConApi<N extends ConNode<any> = ConNode<any>> extends NodeApi<N> {
353353
/**
354354
* Returns a proxy object for this node.
355355
*/
356-
public proxy(): types.ProxyNodeCon<N> {
356+
public get s(): types.ProxyNodeCon<N> {
357357
return {$: this} as unknown as types.ProxyNodeCon<N>;
358358
}
359359
}
@@ -390,13 +390,13 @@ export class ValApi<N extends ValNode<any> = ValNode<any>> extends NodeApi<N> {
390390
* Returns a proxy object for this node. Allows to access the value of the
391391
* node by accessing the `.val` property.
392392
*/
393-
public proxy(): types.ProxyNodeVal<N> {
393+
public get s(): types.ProxyNodeVal<N> {
394394
const self = this;
395395
const proxy = {
396396
$: this,
397397
get val() {
398398
const childNode = self.node.node();
399-
return (<any>self).api.wrap(childNode).proxy();
399+
return (<any>self).api.wrap(childNode).s;
400400
},
401401
};
402402
return <any>proxy;
@@ -455,7 +455,7 @@ export class VecApi<N extends VecNode<any> = VecNode<any>> extends NodeApi<N> {
455455
* Returns a proxy object for this node. Allows to access vector elements by
456456
* index.
457457
*/
458-
public proxy(): types.ProxyNodeVec<N> {
458+
public get s(): types.ProxyNodeVec<N> {
459459
const proxy = new Proxy(
460460
{},
461461
{
@@ -466,7 +466,7 @@ export class VecApi<N extends VecNode<any> = VecNode<any>> extends NodeApi<N> {
466466
if (Number.isNaN(index)) throw new Error('INVALID_INDEX');
467467
const child = this.node.get(index);
468468
if (!child) throw new Error('OUT_OF_BOUNDS');
469-
return (<any>this).api.wrap(child).proxy();
469+
return (<any>this).api.wrap(child).s;
470470
},
471471
},
472472
);
@@ -538,7 +538,7 @@ export class ObjApi<N extends ObjNode<any> = ObjNode<any>> extends NodeApi<N> {
538538
* Returns a proxy object for this node. Allows to access object properties
539539
* by key.
540540
*/
541-
public proxy(): types.ProxyNodeObj<N> {
541+
public get s(): types.ProxyNodeObj<N> {
542542
const proxy = new Proxy(
543543
{},
544544
{
@@ -547,7 +547,7 @@ export class ObjApi<N extends ObjNode<any> = ObjNode<any>> extends NodeApi<N> {
547547
const key = String(prop);
548548
const child = this.node.get(key);
549549
if (!child) throw new Error('NO_SUCH_KEY');
550-
return (<any>this).api.wrap(child).proxy();
550+
return (<any>this).api.wrap(child).s;
551551
},
552552
},
553553
);
@@ -649,7 +649,7 @@ export class StrApi extends NodeApi<StrNode> {
649649
/**
650650
* Returns a proxy object for this node.
651651
*/
652-
public proxy(): types.ProxyNodeStr {
652+
public get s(): types.ProxyNodeStr {
653653
return {$: this};
654654
}
655655
}
@@ -704,7 +704,7 @@ export class BinApi extends NodeApi<BinNode> {
704704
/**
705705
* Returns a proxy object for this node.
706706
*/
707-
public proxy(): types.ProxyNodeBin {
707+
public get s(): types.ProxyNodeBin {
708708
return {$: this};
709709
}
710710
}
@@ -800,7 +800,7 @@ export class ArrApi<N extends ArrNode<any> = ArrNode<any>> extends NodeApi<N> {
800800
*
801801
* @returns Proxy object that allows to access array elements by index.
802802
*/
803-
public proxy(): types.ProxyNodeArr<N> {
803+
public get s(): types.ProxyNodeArr<N> {
804804
const proxy = new Proxy(
805805
{},
806806
{
@@ -810,7 +810,7 @@ export class ArrApi<N extends ArrNode<any> = ArrNode<any>> extends NodeApi<N> {
810810
if (Number.isNaN(index)) throw new Error('INVALID_INDEX');
811811
const child = this.node.getNode(index);
812812
if (!child) throw new Error('OUT_OF_BOUNDS');
813-
return (this.api.wrap(child) as any).proxy();
813+
return (this.api.wrap(child) as any).s;
814814
},
815815
},
816816
);

0 commit comments

Comments
 (0)