Skip to content

Commit 3188ef0

Browse files
authored
Merge pull request #371 from streamich/crdt-improvements
JSON CRDT .toString(), and equal object identity
2 parents 950ffc6 + 1efc3fa commit 3188ef0

File tree

6 files changed

+43
-8
lines changed

6 files changed

+43
-8
lines changed

src/json-crdt/__demos__/type-safety.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import {Model, n} from '..';
1010

11+
console.clear();
12+
1113
const model = Model.withLogicalClock(1234) as Model<
1214
n.obj<{
1315
num: n.con<number>;

src/json-crdt/model/Model.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
VectorClock,
99
ServerVectorClock,
1010
compare,
11+
toDisplayString,
1112
} from '../../json-crdt-patch/clock';
1213
import {JsonCrdtPatchOperation, Patch} from '../../json-crdt-patch/Patch';
1314
import {ModelApi} from './api/ModelApi';
@@ -340,8 +341,24 @@ export class Model<RootJsonNode extends JsonNode = JsonNode> implements Printabl
340341
this.constructor.name +
341342
printTree(tab, [
342343
(tab) => this.root.toString(tab),
343-
// nl,
344-
// (tab) => this.index.toString(tab),
344+
nl,
345+
(tab) => {
346+
const nodes: JsonNode[] = [];
347+
this.index.forEach((item) => nodes.push(item.v));
348+
return (
349+
`Index (${nodes.length} nodes)` +
350+
(nodes.length
351+
? printTree(
352+
tab,
353+
nodes.map((node) => (tab) => `${node.constructor.name} ${toDisplayString(node.id)}`),
354+
)
355+
: '')
356+
);
357+
},
358+
nl,
359+
// (tab) => `View ${toTree(this.view(), tab)}`,
360+
(tab) =>
361+
`View${printTree(tab, [(tab) => String(JSON.stringify(this.view(), null, 2)).replace(/\n/g, '\n' + tab)])}`,
345362
nl,
346363
(tab) => this.clock.toString(tab),
347364
hasExtensions ? nl : null,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {Model} from '../Model';
2+
3+
test('"obj" node returns same object if deep equality does not change', () => {
4+
const model = Model.withLogicalClock();
5+
model.api.root({
6+
foo: 'bar',
7+
qux: [1, 2, 3],
8+
});
9+
const view1 = model.view();
10+
const time1 = model.clock.time;
11+
model.api.obj([]).set({
12+
foo: 'bar',
13+
});
14+
model.api.val(['qux', 1]).set(2);
15+
const view2 = model.view();
16+
const time2 = model.clock.time;
17+
expect(view1).toBe(view2);
18+
expect(time2 > time1).toBe(true);
19+
});

src/json-crdt/nodes/obj/ObjNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export class ObjNode<Value extends Record<string, JsonNode> = Record<string, Jso
128128
// ---------------------------------------------------------------- Printable
129129

130130
public toString(tab: string = ''): string {
131-
const header = this.constructor.name + ' "obj" ' + toDisplayString(this.id);
131+
const header = this.constructor.name + ' ' + toDisplayString(this.id);
132132
return (
133133
header +
134134
printTree(

src/json-crdt/nodes/val/ValNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class ValNode<Value extends JsonNode = JsonNode> implements JsonNode<Read
8585

8686
public toString(tab: string = ''): string {
8787
const node = this.node();
88-
const header = this.constructor.name + toDisplayString(this.id);
88+
const header = this.constructor.name + ' ' + toDisplayString(this.id);
8989
return header + printTree(tab, [(tab) => (node ? node.toString(tab) : toDisplayString(this.val))]);
9090
}
9191
}

src/json-crdt/nodes/vec/VecNode.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,7 @@ export class VecNode<Value extends JsonNode[] = JsonNode[]>
175175
public toString(tab: string = ''): string {
176176
const extNode = this.ext();
177177
const header =
178-
this.constructor.name +
179-
' "vec" ' +
180-
toDisplayString(this.id) +
181-
(extNode ? ` { extension = ${this.getExtId()} }` : '');
178+
this.constructor.name + ' ' + toDisplayString(this.id) + (extNode ? ` { extension = ${this.getExtId()} }` : '');
182179
if (extNode) {
183180
return this.child()!.toString(tab);
184181
}

0 commit comments

Comments
 (0)