@dxos/app-graph 0.16.0 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/chunk-AppGraph.mjs +62 -19
- package/dist/lib/chunk-AppGraph.mjs.map +1 -1
- package/dist/lib/chunk-atoms.mjs +1 -1
- package/dist/types/src/AppGraph.bench.d.ts +2 -0
- package/dist/types/src/AppGraph.bench.d.ts.map +1 -0
- package/dist/types/src/AppGraph.d.ts.map +1 -1
- package/dist/types/src/util.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +27 -23
- package/src/AppGraph.bench.ts +111 -0
- package/src/AppGraph.test.ts +170 -0
- package/src/AppGraph.ts +83 -7
- package/src/expansion.test.ts +19 -0
- package/src/util.test.ts +16 -0
- package/src/util.ts +2 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/app-graph",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "Constructs knowledge graphs for the purpose of building applications on top of",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -66,38 +66,42 @@
|
|
|
66
66
|
"src"
|
|
67
67
|
],
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"
|
|
70
|
-
"@dxos/
|
|
71
|
-
"@dxos/
|
|
72
|
-
"@dxos/
|
|
73
|
-
"@dxos/
|
|
74
|
-
"@dxos/
|
|
75
|
-
"@dxos/
|
|
76
|
-
"@dxos/
|
|
77
|
-
"@dxos/
|
|
78
|
-
"
|
|
69
|
+
"@dxos/async": "0.17.0",
|
|
70
|
+
"@dxos/debug": "0.17.0",
|
|
71
|
+
"@dxos/echo": "0.17.0",
|
|
72
|
+
"@dxos/effect": "0.17.0",
|
|
73
|
+
"@dxos/graph": "0.17.0",
|
|
74
|
+
"@dxos/invariant": "0.17.0",
|
|
75
|
+
"@dxos/keys": "0.17.0",
|
|
76
|
+
"@dxos/log": "0.17.0",
|
|
77
|
+
"@dxos/util": "0.17.0",
|
|
78
|
+
"main-thread-scheduling": "^14.1.1"
|
|
79
79
|
},
|
|
80
80
|
"devDependencies": {
|
|
81
|
+
"@dxos/random": "0.17.0",
|
|
82
|
+
"@dxos/react-client": "0.17.0",
|
|
83
|
+
"@dxos/react-ui": "0.17.0",
|
|
84
|
+
"@dxos/ui-theme": "0.17.0",
|
|
81
85
|
"@effect/atom-react": "4.0.4",
|
|
82
86
|
"@storybook/react-vite": "^10.6.0",
|
|
83
|
-
"@types/react": "~19.
|
|
84
|
-
"@types/react-dom": "~19.
|
|
87
|
+
"@types/react": "~19.3.0",
|
|
88
|
+
"@types/react-dom": "~19.3.0",
|
|
85
89
|
"effect": "4.0.4",
|
|
86
|
-
"react": "~19.
|
|
87
|
-
"react-dom": "~19.
|
|
88
|
-
"
|
|
89
|
-
"
|
|
90
|
-
"@dxos/react-client": "0.16.0",
|
|
91
|
-
"@dxos/react-ui": "0.16.0",
|
|
92
|
-
"@dxos/ui-theme": "0.16.0"
|
|
90
|
+
"react": "~19.3.1",
|
|
91
|
+
"react-dom": "~19.3.1",
|
|
92
|
+
"typedoc": "0.28.1",
|
|
93
|
+
"vite": "^8.3.4"
|
|
93
94
|
},
|
|
94
95
|
"peerDependencies": {
|
|
96
|
+
"@dxos/ui-theme": "^0.17.0",
|
|
95
97
|
"effect": "4.0.4",
|
|
96
|
-
"react": "~19.
|
|
97
|
-
"react-dom": "~19.
|
|
98
|
-
"@dxos/ui-theme": "^0.16.0"
|
|
98
|
+
"react": "~19.3.1",
|
|
99
|
+
"react-dom": "~19.3.1"
|
|
99
100
|
},
|
|
100
101
|
"publishConfig": {
|
|
101
102
|
"access": "public"
|
|
103
|
+
},
|
|
104
|
+
"scripts": {
|
|
105
|
+
"typedoc": "typedoc --out typedoc"
|
|
102
106
|
}
|
|
103
107
|
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import * as Option from 'effect/Option';
|
|
7
|
+
import * as Atom from 'effect/reactivity/Atom';
|
|
8
|
+
import * as Registry from 'effect/reactivity/AtomRegistry';
|
|
9
|
+
import { afterAll, bench, describe } from 'vitest';
|
|
10
|
+
|
|
11
|
+
import * as GraphNode from '@dxos/graph/GraphNode';
|
|
12
|
+
|
|
13
|
+
import * as Graph from './AppGraph.ts';
|
|
14
|
+
import * as GraphBuilder from './AppGraphBuilder.ts';
|
|
15
|
+
import * as Node from './AppGraphNode.ts';
|
|
16
|
+
|
|
17
|
+
//
|
|
18
|
+
// A builder re-running over output that did not change: an object's actions are rebuilt whenever
|
|
19
|
+
// anything the builder reads changes, with a new closure and a new label tuple per action, as the
|
|
20
|
+
// Composer space plugin builds them. Each re-run should cost the diff and emit no node changes.
|
|
21
|
+
//
|
|
22
|
+
// DX_RUN_MANUAL_TESTS=1 pnpm exec vitest bench --run --project node src/AppGraph.bench.ts
|
|
23
|
+
//
|
|
24
|
+
|
|
25
|
+
const ROOT = GraphNode.RootId;
|
|
26
|
+
const OBJECTS = 8;
|
|
27
|
+
const ACTIONS = 40;
|
|
28
|
+
const BENCH_OPTIONS = { time: 500 };
|
|
29
|
+
|
|
30
|
+
const setup = () => {
|
|
31
|
+
const registry = Registry.make();
|
|
32
|
+
const revision = Atom.make(0).pipe(Atom.keepAlive);
|
|
33
|
+
const builder = GraphBuilder.make({ registry });
|
|
34
|
+
GraphBuilder.addExtension(
|
|
35
|
+
builder,
|
|
36
|
+
GraphBuilder.createExtensionRaw({
|
|
37
|
+
id: 'objects',
|
|
38
|
+
connector: (node) =>
|
|
39
|
+
Atom.make((get) =>
|
|
40
|
+
Option.match(get(node), {
|
|
41
|
+
onNone: () => [],
|
|
42
|
+
onSome: (source) =>
|
|
43
|
+
source.id === ROOT
|
|
44
|
+
? Array.from({ length: OBJECTS }, (unusedValue, index) => ({
|
|
45
|
+
id: `o${index}`,
|
|
46
|
+
type: 'org.dxos.type.example',
|
|
47
|
+
data: index,
|
|
48
|
+
properties: { label: ['object.label', { ns: 'org.dxos.type.example' }] },
|
|
49
|
+
}))
|
|
50
|
+
: [],
|
|
51
|
+
}),
|
|
52
|
+
),
|
|
53
|
+
actions: (node) =>
|
|
54
|
+
Atom.make((get) => {
|
|
55
|
+
get(revision);
|
|
56
|
+
return Option.match(get(node), {
|
|
57
|
+
onNone: () => [],
|
|
58
|
+
onSome: (source) =>
|
|
59
|
+
source.id === ROOT
|
|
60
|
+
? []
|
|
61
|
+
: Array.from({ length: ACTIONS }, (unusedValue, index) => ({
|
|
62
|
+
id: `a${index}`,
|
|
63
|
+
data: () => Effect.void,
|
|
64
|
+
properties: {
|
|
65
|
+
label: [`action-${index}.label`, { ns: 'org.dxos.type.example', defaultValue: 'Action' }],
|
|
66
|
+
icon: 'ph--pencil-simple-line--regular',
|
|
67
|
+
disposition: 'list-item',
|
|
68
|
+
},
|
|
69
|
+
})),
|
|
70
|
+
});
|
|
71
|
+
}),
|
|
72
|
+
}),
|
|
73
|
+
);
|
|
74
|
+
return { registry, revision, builder, graph: builder.graph };
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const { registry, revision, builder, graph } = setup();
|
|
78
|
+
Graph.expandSync(graph, ROOT, 'child');
|
|
79
|
+
await GraphBuilder.flush(builder);
|
|
80
|
+
for (let index = 0; index < OBJECTS; index++) {
|
|
81
|
+
Graph.expandSync(graph, `${ROOT}/o${index}`, Node.action);
|
|
82
|
+
}
|
|
83
|
+
await GraphBuilder.flush(builder);
|
|
84
|
+
|
|
85
|
+
let changes = 0;
|
|
86
|
+
let reruns = 0;
|
|
87
|
+
graph.onNodeChanged.on(() => {
|
|
88
|
+
changes++;
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const actions = registry.get(graph.actions(`${ROOT}/o0`));
|
|
92
|
+
if (actions.length !== ACTIONS) {
|
|
93
|
+
throw new Error(`fixture expanded ${actions.length} actions, expected ${ACTIONS}`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
afterAll(() => {
|
|
97
|
+
// eslint-disable-next-line no-console
|
|
98
|
+
console.log(`node changes per re-run: ${(changes / Math.max(reruns, 1)).toFixed(1)} (${reruns} re-runs)`);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('app-graph re-offer', { tags: ['manual'], timeout: 120_000 }, () => {
|
|
102
|
+
bench(
|
|
103
|
+
`re-run ${OBJECTS} objects × ${ACTIONS} unchanged actions`,
|
|
104
|
+
async () => {
|
|
105
|
+
registry.set(revision, registry.get(revision) + 1);
|
|
106
|
+
await GraphBuilder.flush(builder);
|
|
107
|
+
reruns++;
|
|
108
|
+
},
|
|
109
|
+
BENCH_OPTIONS,
|
|
110
|
+
);
|
|
111
|
+
});
|
package/src/AppGraph.test.ts
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import * as Context from 'effect/Context';
|
|
5
6
|
import * as Effect from 'effect/Effect';
|
|
6
7
|
import * as Fiber from 'effect/Fiber';
|
|
8
|
+
import * as Hash from 'effect/Hash';
|
|
7
9
|
import * as Option from 'effect/Option';
|
|
8
10
|
import * as Atom from 'effect/reactivity/Atom';
|
|
9
11
|
import * as Registry from 'effect/reactivity/AtomRegistry';
|
|
@@ -35,6 +37,21 @@ describe('Graph', () => {
|
|
|
35
37
|
expect(Graph.getGraph(root.value)).toEqual(graph);
|
|
36
38
|
});
|
|
37
39
|
|
|
40
|
+
test('a node hashes without walking the graph it belongs to', () => {
|
|
41
|
+
const registry = Registry.make();
|
|
42
|
+
const graph = Graph.make({ registry });
|
|
43
|
+
registry.mount(
|
|
44
|
+
Atom.make({
|
|
45
|
+
get shell(): never {
|
|
46
|
+
throw new Error('walked into the registry');
|
|
47
|
+
},
|
|
48
|
+
}),
|
|
49
|
+
);
|
|
50
|
+
const root = registry.get(graph.node(GraphNode.RootId));
|
|
51
|
+
assert.ok(Option.isSome(root));
|
|
52
|
+
expect(() => Hash.hash(root.value)).not.toThrow();
|
|
53
|
+
});
|
|
54
|
+
|
|
38
55
|
test('add node', () => {
|
|
39
56
|
const registry = Registry.make();
|
|
40
57
|
const graph = Graph.make({ registry });
|
|
@@ -226,6 +243,159 @@ describe('Graph', () => {
|
|
|
226
243
|
expect(node.pipe(Option.getOrNull)).toEqual(null);
|
|
227
244
|
});
|
|
228
245
|
|
|
246
|
+
test('re-adding an action with a new closure and rebuilt label emits nothing and invokes the latest closure', () => {
|
|
247
|
+
const graph = Graph.make();
|
|
248
|
+
const invoked: string[] = [];
|
|
249
|
+
const action = (name: string) =>
|
|
250
|
+
Node.makeAction({
|
|
251
|
+
id: EXAMPLE_ID,
|
|
252
|
+
data: () => Effect.sync(() => invoked.push(name)),
|
|
253
|
+
properties: { label: ['action.label', { ns: EXAMPLE_TYPE }] },
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
Graph.addNode(graph, action('first'));
|
|
257
|
+
let changes = 0;
|
|
258
|
+
graph.onNodeChanged.on(() => {
|
|
259
|
+
changes++;
|
|
260
|
+
});
|
|
261
|
+
Graph.addNode(graph, action('second'));
|
|
262
|
+
expect(changes).toEqual(0);
|
|
263
|
+
|
|
264
|
+
const node = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrThrow);
|
|
265
|
+
assert.ok(Node.isAction(node));
|
|
266
|
+
Effect.runSync(node.data());
|
|
267
|
+
expect(invoked).toEqual(['second']);
|
|
268
|
+
|
|
269
|
+
Graph.addNode(graph, { ...action('third'), properties: { label: ['other.label', { ns: EXAMPLE_TYPE }] } });
|
|
270
|
+
expect(changes).toEqual(1);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test('a node whose data is a function but no action keeps that function, as a type node keeps its schema', () => {
|
|
274
|
+
class Schema {
|
|
275
|
+
static readonly typename = 'org.dxos.type.example';
|
|
276
|
+
}
|
|
277
|
+
const graph = Graph.make({ nodes: [{ id: exampleId(1), type: EXAMPLE_TYPE, data: Schema, properties: {} }] });
|
|
278
|
+
Graph.addNode(graph, { id: exampleId(2), type: EXAMPLE_TYPE, data: Schema });
|
|
279
|
+
Graph.addNode(graph, { id: exampleId(2), type: EXAMPLE_TYPE, data: Schema });
|
|
280
|
+
|
|
281
|
+
for (const id of [exampleId(1), exampleId(2)]) {
|
|
282
|
+
expect(Graph.getNode(graph, id).pipe(Option.getOrThrow).data).toBe(Schema);
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
test('re-adding an action read back from the graph keeps it invocable', () => {
|
|
287
|
+
const graph = Graph.make();
|
|
288
|
+
const invoked: string[] = [];
|
|
289
|
+
Graph.addNode(
|
|
290
|
+
graph,
|
|
291
|
+
Node.makeAction({ id: EXAMPLE_ID, data: () => Effect.sync(() => invoked.push('first')), properties: {} }),
|
|
292
|
+
);
|
|
293
|
+
const read = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrThrow);
|
|
294
|
+
let changes = 0;
|
|
295
|
+
graph.onNodeChanged.on(() => {
|
|
296
|
+
changes++;
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
Graph.addNode(graph, read);
|
|
300
|
+
expect(changes).toEqual(0);
|
|
301
|
+
const node = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrThrow);
|
|
302
|
+
assert.ok(Node.isAction(node));
|
|
303
|
+
Effect.runSync(node.data());
|
|
304
|
+
expect(invoked).toEqual(['first']);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
test('a constructor-seeded action re-offered with the same closure emits nothing', () => {
|
|
308
|
+
const invoke = () => Effect.void;
|
|
309
|
+
const graph = Graph.make({ nodes: [{ id: EXAMPLE_ID, type: Node.ActionType, data: invoke, properties: {} }] });
|
|
310
|
+
let changes = 0;
|
|
311
|
+
graph.onNodeChanged.on(() => {
|
|
312
|
+
changes++;
|
|
313
|
+
});
|
|
314
|
+
Graph.addNode(graph, Node.makeAction({ id: EXAMPLE_ID, data: invoke, properties: {} }));
|
|
315
|
+
expect(changes).toEqual(0);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
test('an action copied from another node keeps its own forwarder', () => {
|
|
319
|
+
const graph = Graph.make();
|
|
320
|
+
const invoked: string[] = [];
|
|
321
|
+
const action = (id: string, name: string) =>
|
|
322
|
+
Node.makeAction({ id, data: () => Effect.sync(() => invoked.push(name)), properties: {} });
|
|
323
|
+
Graph.addNode(graph, action(exampleId(1), 'a'));
|
|
324
|
+
const copied = Graph.getNode(graph, exampleId(1)).pipe(Option.getOrThrow);
|
|
325
|
+
Graph.addNode(graph, { ...copied, id: exampleId(2) });
|
|
326
|
+
Graph.addNode(graph, action(exampleId(2), 'b'));
|
|
327
|
+
|
|
328
|
+
for (const id of [exampleId(1), exampleId(2)]) {
|
|
329
|
+
const node = Graph.getNode(graph, id).pipe(Option.getOrThrow);
|
|
330
|
+
assert.ok(Node.isAction(node));
|
|
331
|
+
Effect.runSync(node.data());
|
|
332
|
+
}
|
|
333
|
+
expect(invoked).toEqual(['a', 'b']);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
test('re-offering an action with a new context updates it', () => {
|
|
337
|
+
const graph = Graph.make();
|
|
338
|
+
const invoke = () => Effect.void;
|
|
339
|
+
const withContext = (context: Node.ActionContext) => {
|
|
340
|
+
const action = { ...Node.makeAction({ id: EXAMPLE_ID, data: invoke, properties: {} }), _actionContext: context };
|
|
341
|
+
return action;
|
|
342
|
+
};
|
|
343
|
+
// Distinct contexts: `Context.empty()` is one shared instance, which would hide a stale context.
|
|
344
|
+
const Value = Context.Service<number>('app-graph/test/Value');
|
|
345
|
+
const second = Context.make(Value, 2);
|
|
346
|
+
Graph.addNode(graph, withContext(Context.make(Value, 1)));
|
|
347
|
+
Graph.addNode(graph, withContext(second));
|
|
348
|
+
const node = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrThrow);
|
|
349
|
+
assert.ok(Node.isAction(node));
|
|
350
|
+
expect(node._actionContext).toBe(second);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
test('a constructor-seeded node without data keeps null data', () => {
|
|
354
|
+
const graph = Graph.make({ nodes: [{ id: EXAMPLE_ID, type: EXAMPLE_TYPE, properties: {} }] });
|
|
355
|
+
expect(Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrThrow).data).toBeNull();
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
test('an action copied between graphs and back stays invocable', () => {
|
|
359
|
+
const first = Graph.make();
|
|
360
|
+
const second = Graph.make();
|
|
361
|
+
const invoked: string[] = [];
|
|
362
|
+
Graph.addNode(
|
|
363
|
+
first,
|
|
364
|
+
Node.makeAction({ id: EXAMPLE_ID, data: () => Effect.sync(() => invoked.push('a')), properties: {} }),
|
|
365
|
+
);
|
|
366
|
+
Graph.addNode(second, Graph.getNode(first, EXAMPLE_ID).pipe(Option.getOrThrow));
|
|
367
|
+
Graph.addNode(first, Graph.getNode(second, EXAMPLE_ID).pipe(Option.getOrThrow));
|
|
368
|
+
|
|
369
|
+
for (const graph of [first, second]) {
|
|
370
|
+
const node = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrThrow);
|
|
371
|
+
assert.ok(Node.isAction(node));
|
|
372
|
+
Effect.runSync(node.data());
|
|
373
|
+
}
|
|
374
|
+
expect(invoked).toEqual(['a', 'a']);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
test('re-offering an action without its context keeps the stored one and emits nothing', () => {
|
|
378
|
+
const graph = Graph.make();
|
|
379
|
+
const invoke = () => Effect.void;
|
|
380
|
+
const Value = Context.Service<number>('app-graph/test/Value');
|
|
381
|
+
const context = Context.make(Value, 1);
|
|
382
|
+
const withContext = {
|
|
383
|
+
...Node.makeAction({ id: EXAMPLE_ID, data: invoke, properties: {} }),
|
|
384
|
+
_actionContext: context,
|
|
385
|
+
};
|
|
386
|
+
Graph.addNode(graph, withContext);
|
|
387
|
+
let changes = 0;
|
|
388
|
+
graph.onNodeChanged.on(() => {
|
|
389
|
+
changes++;
|
|
390
|
+
});
|
|
391
|
+
Graph.addNode(graph, Node.makeAction({ id: EXAMPLE_ID, data: invoke, properties: {} }));
|
|
392
|
+
Graph.addNode(graph, Node.makeAction({ id: EXAMPLE_ID, data: invoke, properties: {} }));
|
|
393
|
+
expect(changes).toEqual(0);
|
|
394
|
+
const node = Graph.getNode(graph, EXAMPLE_ID).pipe(Option.getOrThrow);
|
|
395
|
+
assert.ok(Node.isAction(node));
|
|
396
|
+
expect(node._actionContext).toBe(context);
|
|
397
|
+
});
|
|
398
|
+
|
|
229
399
|
test('add edge', () => {
|
|
230
400
|
const registry = Registry.make();
|
|
231
401
|
const graph = Graph.make({ registry });
|
package/src/AppGraph.ts
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import * as Effect from 'effect/Effect';
|
|
6
|
+
import * as Equal from 'effect/Equal';
|
|
6
7
|
import * as Function from 'effect/Function';
|
|
8
|
+
import * as Hash from 'effect/Hash';
|
|
7
9
|
import * as Option from 'effect/Option';
|
|
8
10
|
import * as Pipeable from 'effect/Pipeable';
|
|
9
11
|
import * as Atom from 'effect/reactivity/Atom';
|
|
@@ -213,6 +215,16 @@ export const getGraph = (node: Node.Node): Graph => {
|
|
|
213
215
|
return graph as Graph;
|
|
214
216
|
};
|
|
215
217
|
|
|
218
|
+
/** Carried by each forwarder {@link stableData} makes, so any graph recognizes one without a lookup table. */
|
|
219
|
+
const ForwardedKey = Symbol('app-graph/forwarded');
|
|
220
|
+
|
|
221
|
+
/** The closure a forwarder calls, never itself a forwarder, and the graph node it belongs to. */
|
|
222
|
+
type Forwarded = { target: (...args: unknown[]) => unknown; readonly graph: object; readonly id: string };
|
|
223
|
+
|
|
224
|
+
type Forwarder = ((...args: unknown[]) => unknown) & { readonly [ForwardedKey]: Forwarded };
|
|
225
|
+
|
|
226
|
+
const isForwarder = (value: unknown): value is Forwarder => typeof value === 'function' && ForwardedKey in value;
|
|
227
|
+
|
|
216
228
|
/**
|
|
217
229
|
* The Graph represents the user interface information architecture of the application constructed via plugins.
|
|
218
230
|
* @internal
|
|
@@ -226,6 +238,14 @@ export class GraphImpl implements WritableGraph {
|
|
|
226
238
|
return Pipeable.pipeArguments(this, arguments);
|
|
227
239
|
}
|
|
228
240
|
|
|
241
|
+
[Hash.symbol](): number {
|
|
242
|
+
return Hash.random(this);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
[Equal.symbol](that: unknown): boolean {
|
|
246
|
+
return this === that;
|
|
247
|
+
}
|
|
248
|
+
|
|
229
249
|
readonly onNodeChanged = new Event<{
|
|
230
250
|
id: string;
|
|
231
251
|
node: Option.Option<Node.Node>;
|
|
@@ -371,7 +391,16 @@ export class GraphImpl implements WritableGraph {
|
|
|
371
391
|
|
|
372
392
|
this._model.batch(() => {
|
|
373
393
|
this._setNode(GraphNode.RootId, this._constructNode({ id: GraphNode.RootId, type: Node.RootType, data: null }));
|
|
374
|
-
nodes?.forEach((node) =>
|
|
394
|
+
nodes?.forEach((node) =>
|
|
395
|
+
this._setNode(
|
|
396
|
+
node.id,
|
|
397
|
+
this._constructNode(
|
|
398
|
+
typeof node.data === 'function'
|
|
399
|
+
? { ...node, data: stableData(this, node.id, node.type, undefined, node.data) }
|
|
400
|
+
: node,
|
|
401
|
+
),
|
|
402
|
+
),
|
|
403
|
+
);
|
|
375
404
|
Object.entries(edges ?? {}).forEach(([source, relations]) => {
|
|
376
405
|
Object.entries(relations).forEach(([relation, targets]) => {
|
|
377
406
|
targets.forEach((target) => this._setEdge(source, target, relation));
|
|
@@ -832,6 +861,32 @@ export const addNodes = <T extends WritableGraph>(graph: T, nodes: Node.NodeArg<
|
|
|
832
861
|
return graph;
|
|
833
862
|
};
|
|
834
863
|
|
|
864
|
+
/**
|
|
865
|
+
* Builders recreate an action's closure on every run, so an action's `data` is held behind a stable
|
|
866
|
+
* forwarder per node whose target is swapped: a re-offer that changes nothing else emits nothing,
|
|
867
|
+
* and invoking the node still runs the latest closure. Only actions: other function data (a type
|
|
868
|
+
* node's schema class) is read for its statics, which a forwarder does not carry.
|
|
869
|
+
*/
|
|
870
|
+
const stableData = (graph: GraphImpl, id: string, type: string, existing: unknown, data: any): any => {
|
|
871
|
+
if (type !== Node.ActionType || typeof data !== 'function') {
|
|
872
|
+
return data;
|
|
873
|
+
}
|
|
874
|
+
const owned = (forwarder: Forwarder) => forwarder[ForwardedKey].graph === graph && forwarder[ForwardedKey].id === id;
|
|
875
|
+
// This node's own forwarder read back and re-added is kept: retargeting it at itself would recurse.
|
|
876
|
+
if (isForwarder(data) && owned(data)) {
|
|
877
|
+
return data;
|
|
878
|
+
}
|
|
879
|
+
// Any other forwarder, from another node or graph, is unwrapped: a target is never a forwarder, so none can cycle.
|
|
880
|
+
const target = isForwarder(data) ? data[ForwardedKey].target : data;
|
|
881
|
+
if (isForwarder(existing) && owned(existing)) {
|
|
882
|
+
existing[ForwardedKey].target = target;
|
|
883
|
+
return existing;
|
|
884
|
+
}
|
|
885
|
+
const cell: Forwarded = { target, graph, id };
|
|
886
|
+
const forwarder: Forwarder = Object.assign((...args: unknown[]) => cell.target(...args), { [ForwardedKey]: cell });
|
|
887
|
+
return forwarder;
|
|
888
|
+
};
|
|
889
|
+
|
|
835
890
|
/**
|
|
836
891
|
* Add a node to the graph.
|
|
837
892
|
*/
|
|
@@ -854,17 +909,26 @@ export const addNode = <T extends WritableGraph>(graph: T, nodeArg: Node.NodeArg
|
|
|
854
909
|
Option.match(existingNode, {
|
|
855
910
|
onSome: (existing) => {
|
|
856
911
|
const typeChanged = existing.type !== type;
|
|
857
|
-
const
|
|
858
|
-
const
|
|
912
|
+
const nextData = stableData(internal, id, type, existing.data, data);
|
|
913
|
+
const dataChanged = !shallowEqual(existing.data, nextData);
|
|
914
|
+
const propertiesChanged = Object.keys(properties).some(
|
|
915
|
+
(key) => !Equal.equals(existing.properties[key], properties[key]),
|
|
916
|
+
);
|
|
917
|
+
// The forwarder keeps `data` stable, so the context an action runs with is compared on its own; an
|
|
918
|
+
// offer without one keeps the stored context, since the update spreads the existing node.
|
|
919
|
+
const contextChanged =
|
|
920
|
+
rest._actionContext !== undefined &&
|
|
921
|
+
(Node.isAction(existing) ? existing._actionContext : undefined) !== rest._actionContext;
|
|
859
922
|
// `changed` is on the visit log because counting `existing node` lines alone measures how often a
|
|
860
923
|
// node was re-offered, not how often it actually changed — two very different costs.
|
|
861
|
-
const changed = typeChanged || dataChanged || propertiesChanged;
|
|
924
|
+
const changed = typeChanged || dataChanged || propertiesChanged || contextChanged;
|
|
862
925
|
log('existing node', {
|
|
863
926
|
id,
|
|
864
927
|
changed,
|
|
865
928
|
typeChanged,
|
|
866
929
|
dataChanged,
|
|
867
930
|
propertiesChanged,
|
|
931
|
+
contextChanged,
|
|
868
932
|
});
|
|
869
933
|
if (changed) {
|
|
870
934
|
log('updating node', { id, type, data, properties });
|
|
@@ -872,7 +936,7 @@ export const addNode = <T extends WritableGraph>(graph: T, nodeArg: Node.NodeArg
|
|
|
872
936
|
...existing,
|
|
873
937
|
...rest,
|
|
874
938
|
type,
|
|
875
|
-
data,
|
|
939
|
+
data: nextData,
|
|
876
940
|
// Merged, not replaced, so several extensions can each contribute properties to one node —
|
|
877
941
|
// which makes a key one generation sets and the next omits impossible to clear. A builder
|
|
878
942
|
// that varies a property must emit it on every generation, `false`/`undefined` included.
|
|
@@ -884,7 +948,13 @@ export const addNode = <T extends WritableGraph>(graph: T, nodeArg: Node.NodeArg
|
|
|
884
948
|
},
|
|
885
949
|
onNone: () => {
|
|
886
950
|
log('new node', { id, type, data, properties });
|
|
887
|
-
const newNode = internal._constructNode({
|
|
951
|
+
const newNode = internal._constructNode({
|
|
952
|
+
id,
|
|
953
|
+
type,
|
|
954
|
+
data: stableData(internal, id, type, undefined, data),
|
|
955
|
+
properties,
|
|
956
|
+
...rest,
|
|
957
|
+
});
|
|
888
958
|
internal._setNode(id, newNode);
|
|
889
959
|
graph.onNodeChanged.emit({ id, node: newNode });
|
|
890
960
|
|
|
@@ -946,7 +1016,13 @@ export const removeNode = <T extends WritableGraph>(graph: T, id: string, edges
|
|
|
946
1016
|
const internal = getInternal(graph);
|
|
947
1017
|
internal._setNode(id, Option.none());
|
|
948
1018
|
graph.onNodeChanged.emit({ id, node: Option.none() });
|
|
949
|
-
//
|
|
1019
|
+
// The builder drops the node's connectors with it, so its expansions wait to re-run when it comes back.
|
|
1020
|
+
for (const key of internal._expanded) {
|
|
1021
|
+
if (primaryParts(key)[0] === id) {
|
|
1022
|
+
internal._expanded.delete(key);
|
|
1023
|
+
internal._pendingExpands.add(key);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
950
1026
|
|
|
951
1027
|
if (edges) {
|
|
952
1028
|
const nodeEdges = internal._currentEdges(id);
|
package/src/expansion.test.ts
CHANGED
|
@@ -108,6 +108,25 @@ describe('expansion and removal through a materialized subtree', () => {
|
|
|
108
108
|
expect(children()).to.deep.equal(['root/c', 'root/b', 'root/a']);
|
|
109
109
|
});
|
|
110
110
|
|
|
111
|
+
test('a node that leaves and comes back is expanded again', async () => {
|
|
112
|
+
const { registry, builder, graph, tree, children } = setup();
|
|
113
|
+
registry.set(tree, { [GraphNode.RootId]: ['a'], a: ['b'] });
|
|
114
|
+
|
|
115
|
+
Graph.expandSync(graph, GraphNode.RootId, 'child');
|
|
116
|
+
await GraphBuilder.flush(builder);
|
|
117
|
+
Graph.expandSync(graph, 'root/a', 'child');
|
|
118
|
+
await GraphBuilder.flush(builder);
|
|
119
|
+
expect(children('root/a')).to.deep.equal(['root/a/b']);
|
|
120
|
+
|
|
121
|
+
registry.set(tree, { [GraphNode.RootId]: [] });
|
|
122
|
+
await GraphBuilder.flush(builder);
|
|
123
|
+
expect(Graph.getNode(graph, 'root/a').pipe(Option.isNone)).to.be.true;
|
|
124
|
+
|
|
125
|
+
registry.set(tree, { [GraphNode.RootId]: ['a'], a: ['b'] });
|
|
126
|
+
await GraphBuilder.flush(builder);
|
|
127
|
+
expect(children('root/a')).to.deep.equal(['root/a/b']);
|
|
128
|
+
});
|
|
129
|
+
|
|
111
130
|
test('deleting a collection removes it and its descendants', async () => {
|
|
112
131
|
const { registry, builder, graph, tree, children } = setup();
|
|
113
132
|
registry.set(tree, { [GraphNode.RootId]: ['a'], a: ['b'], b: ['c'] });
|
package/src/util.test.ts
CHANGED
|
@@ -68,3 +68,19 @@ describe('nodeArgsUnchanged', () => {
|
|
|
68
68
|
expect(nodeArgsUnchanged(stable(), stable())).to.be.true;
|
|
69
69
|
});
|
|
70
70
|
});
|
|
71
|
+
|
|
72
|
+
describe('nodeArgsUnchanged properties', () => {
|
|
73
|
+
// Builders rebuild label tuples on every run, so property values compare by content.
|
|
74
|
+
test('rebuilt label tuples compare unchanged', ({ expect }) => {
|
|
75
|
+
const label = () => ['delete-object.label', { ns: 'org.dxos.type.example', defaultValue: 'Delete' }];
|
|
76
|
+
expect(nodeArgsUnchanged([node({ properties: { label: label() } })], [node({ properties: { label: label() } })])).to
|
|
77
|
+
.be.true;
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('a differing nested value, length or key reads as changed', ({ expect }) => {
|
|
81
|
+
const withLabel = (label: unknown) => [node({ properties: { label } })];
|
|
82
|
+
expect(nodeArgsUnchanged(withLabel(['key', { ns: 'a' }]), withLabel(['key', { ns: 'b' }]))).to.be.false;
|
|
83
|
+
expect(nodeArgsUnchanged(withLabel(['key']), withLabel(['key', {}]))).to.be.false;
|
|
84
|
+
expect(nodeArgsUnchanged([node({ properties: { a: 1 } })], [node({ properties: { b: 1 } })])).to.be.false;
|
|
85
|
+
});
|
|
86
|
+
});
|
package/src/util.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import * as Equal from 'effect/Equal';
|
|
5
6
|
import * as Atom from 'effect/reactivity/Atom';
|
|
6
7
|
|
|
7
8
|
import { shallowEqual } from '@dxos/util';
|
|
@@ -59,7 +60,7 @@ export const nodeArgsUnchanged = (prev: readonly Node.NodeArg<any>[], next: read
|
|
|
59
60
|
prevNode.id === nextNode.id &&
|
|
60
61
|
prevNode.type === nextNode.type &&
|
|
61
62
|
shallowEqual(prevNode.data, nextNode.data) &&
|
|
62
|
-
|
|
63
|
+
Equal.equals(prevNode.properties ?? {}, nextNode.properties ?? {}) &&
|
|
63
64
|
nodeArgsUnchanged(prevNode.nodes ?? [], nextNode.nodes ?? []) &&
|
|
64
65
|
nodeArgsUnchanged(prevNode.actions ?? [], nextNode.actions ?? [])
|
|
65
66
|
);
|