Skip to content

Commit 9eafd61

Browse files
committed
fix(json-crdt): 🐛 allow updating key on root object
1 parent 8a9db46 commit 9eafd61

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ describe('.read()', () => {
5353
expect(doc.api.read(['obj', 'asdf'])).toBe(undefined);
5454
expect(doc.api.read(['arr', 10])).toBe(undefined);
5555
});
56+
57+
test('can read within a "con" node', () => {
58+
const doc = Model.create(s.obj({
59+
con: s.con({
60+
foo: [1, 2, 3]
61+
}),
62+
}))
63+
expect(doc.api.read('/con/foo/1')).toBe(2);
64+
});
5665
});
5766

5867
describe('.add()', () => {
@@ -78,6 +87,13 @@ describe('.add()', () => {
7887
expect(doc.api.read(['newKey'])).toBe('newValue');
7988
});
8089

90+
test('can used on ObjApi', () => {
91+
const doc = createTypedModel();
92+
doc.$.obj.$!.add('/xxx', 'y');
93+
expect(doc.$.obj.$?.read('/xxx')).toBe('y');
94+
expect(doc.api.read('/obj/xxx')).toBe('y');
95+
});
96+
8197
test('can overwrite key', () => {
8298
const doc = createTypedModel();
8399
expect(doc.api.read('/str')).toBe('abc');

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {printTree} from 'tree-dump/lib/printTree';
2+
import {get} from '@jsonjoy.com/json-pointer/lib/get';
3+
import {toPath} from '@jsonjoy.com/json-pointer/lib/util';
24
import {find} from './find';
35
import {type ITimestampStruct, Timestamp} from '../../../json-crdt-patch/clock';
46
import {ObjNode, ArrNode, BinNode, ConNode, VecNode, ValNode, StrNode, RootNode} from '../../nodes';
57
import {NodeEvents} from './NodeEvents';
68
import {ExtNode} from '../../extensions/ExtNode';
7-
import {toPath} from '@jsonjoy.com/json-pointer';
89
import type {Path} from '@jsonjoy.com/json-pointer';
910
import type {Extension} from '../../extensions/Extension';
1011
import type {ExtApi} from '../../extensions/types';
@@ -192,17 +193,18 @@ export class NodeApi<N extends JsonNode = JsonNode> implements Printable {
192193
}
193194

194195
public read(path?: ApiPath): unknown {
195-
try {
196-
return !path ? this.view() : this.in(path).view();
197-
} catch {
198-
return;
199-
}
196+
const view = this.view();
197+
if (Array.isArray(path)) return get(view, path);
198+
if (!path) return view;
199+
let path2: string = path + '';
200+
if (path && path2[0] !== '/') path2 = '/' + path2;
201+
return get(view, toPath(path2));
200202
}
201203

202204
public add(path: ApiPath, value: unknown): boolean {
203205
const [parent, key] = breakPath(path);
204206
try {
205-
let node: unknown = this.in(parent);
207+
let node: unknown = parent ? this.in(parent) : this;
206208
while (node instanceof ValApi) node = node.in();
207209
if (node instanceof ObjApi) {
208210
node.set({[key]: value});

0 commit comments

Comments
 (0)