@dxos/app-graph 0.8.4-main.72ec0f3 → 0.8.4-main.74a063c4e0

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.
Files changed (60) hide show
  1. package/dist/lib/browser/chunk-AKBGYELG.mjs +1603 -0
  2. package/dist/lib/browser/chunk-AKBGYELG.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +29 -827
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +39 -0
  7. package/dist/lib/browser/testing/index.mjs.map +7 -0
  8. package/dist/lib/node-esm/chunk-HR5S4XYH.mjs +1604 -0
  9. package/dist/lib/node-esm/chunk-HR5S4XYH.mjs.map +7 -0
  10. package/dist/lib/node-esm/index.mjs +29 -828
  11. package/dist/lib/node-esm/index.mjs.map +4 -4
  12. package/dist/lib/node-esm/meta.json +1 -1
  13. package/dist/lib/node-esm/testing/index.mjs +40 -0
  14. package/dist/lib/node-esm/testing/index.mjs.map +7 -0
  15. package/dist/types/src/atoms.d.ts +8 -0
  16. package/dist/types/src/atoms.d.ts.map +1 -0
  17. package/dist/types/src/graph-builder.d.ts +111 -65
  18. package/dist/types/src/graph-builder.d.ts.map +1 -1
  19. package/dist/types/src/graph.d.ts +179 -213
  20. package/dist/types/src/graph.d.ts.map +1 -1
  21. package/dist/types/src/index.d.ts +7 -3
  22. package/dist/types/src/index.d.ts.map +1 -1
  23. package/dist/types/src/node-matcher.d.ts +244 -0
  24. package/dist/types/src/node-matcher.d.ts.map +1 -0
  25. package/dist/types/src/node-matcher.test.d.ts +2 -0
  26. package/dist/types/src/node-matcher.test.d.ts.map +1 -0
  27. package/dist/types/src/node.d.ts +50 -5
  28. package/dist/types/src/node.d.ts.map +1 -1
  29. package/dist/types/src/stories/EchoGraph.stories.d.ts.map +1 -1
  30. package/dist/types/src/testing/index.d.ts +2 -0
  31. package/dist/types/src/testing/index.d.ts.map +1 -0
  32. package/dist/types/src/testing/setup-graph-builder.d.ts +31 -0
  33. package/dist/types/src/testing/setup-graph-builder.d.ts.map +1 -0
  34. package/dist/types/src/util.d.ts +39 -0
  35. package/dist/types/src/util.d.ts.map +1 -0
  36. package/dist/types/tsconfig.tsbuildinfo +1 -1
  37. package/package.json +47 -35
  38. package/src/atoms.ts +25 -0
  39. package/src/graph-builder.test.ts +995 -112
  40. package/src/graph-builder.ts +705 -284
  41. package/src/graph.test.ts +448 -120
  42. package/src/graph.ts +1019 -413
  43. package/src/index.ts +10 -3
  44. package/src/node-matcher.test.ts +301 -0
  45. package/src/node-matcher.ts +314 -0
  46. package/src/node.ts +82 -8
  47. package/src/stories/EchoGraph.stories.tsx +151 -121
  48. package/src/stories/Tree.tsx +2 -2
  49. package/src/testing/index.ts +5 -0
  50. package/src/testing/setup-graph-builder.ts +41 -0
  51. package/src/util.ts +95 -0
  52. package/dist/types/src/experimental/graph-projections.test.d.ts +0 -25
  53. package/dist/types/src/experimental/graph-projections.test.d.ts.map +0 -1
  54. package/dist/types/src/signals-integration.test.d.ts +0 -2
  55. package/dist/types/src/signals-integration.test.d.ts.map +0 -1
  56. package/dist/types/src/testing.d.ts +0 -5
  57. package/dist/types/src/testing.d.ts.map +0 -1
  58. package/src/experimental/graph-projections.test.ts +0 -56
  59. package/src/signals-integration.test.ts +0 -219
  60. package/src/testing.ts +0 -20
package/src/util.ts ADDED
@@ -0,0 +1,95 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { invariant } from '@dxos/invariant';
6
+
7
+ import * as Node from './node';
8
+
9
+ // PRIMARY separates top-level components (e.g., node ID from relation) in compound string keys used within the app-graph package.
10
+ const PRIMARY = '\u0001';
11
+
12
+ // SECONDARY separates sub-components within an encoded value (e.g., relation kind from direction) in the same context.
13
+ const SECONDARY = '\u0002';
14
+
15
+ // PATH separates segments in qualified node IDs (e.g., parent path from local segment).
16
+ const PATH = '/';
17
+
18
+ /** Join parts with the primary separator. */
19
+ export const primaryKey = (...parts: string[]): string => parts.join(PRIMARY);
20
+
21
+ /** Split a key on the primary separator. */
22
+ export const primaryParts = (key: string): string[] => key.split(PRIMARY);
23
+
24
+ /** Join parts with the secondary separator. */
25
+ export const secondaryKey = (...parts: string[]): string => parts.join(SECONDARY);
26
+
27
+ /** Split a key on the secondary separator. */
28
+ export const secondaryParts = (key: string): string[] => key.split(SECONDARY);
29
+
30
+ /**
31
+ * Normalize a relation input to a full Relation object.
32
+ */
33
+ export const normalizeRelation = (relation?: Node.RelationInput): Node.Relation =>
34
+ relation == null ? Node.childRelation() : typeof relation === 'string' ? Node.relation(relation) : relation;
35
+
36
+ /**
37
+ * Shallow-compare two values: same reference, or same own-keys with === values.
38
+ */
39
+ export const shallowEqual = (a: unknown, b: unknown): boolean => {
40
+ if (a === b) return true;
41
+ if (a == null || b == null || typeof a !== 'object' || typeof b !== 'object') return false;
42
+ const keysA = Object.keys(a as Record<string, unknown>);
43
+ const keysB = Object.keys(b as Record<string, unknown>);
44
+ if (keysA.length !== keysB.length) {
45
+ return false;
46
+ }
47
+ return keysA.every((k) => (a as Record<string, unknown>)[k] === (b as Record<string, unknown>)[k]);
48
+ };
49
+
50
+ /**
51
+ * Returns true if two NodeArg arrays are semantically identical (same id, type, data, properties per index).
52
+ */
53
+ export const nodeArgsUnchanged = (prev: Node.NodeArg<any>[], next: Node.NodeArg<any>[]): boolean => {
54
+ if (prev.length !== next.length) {
55
+ return false;
56
+ }
57
+
58
+ return prev.every((prevNode, idx) => {
59
+ const nextNode = next[idx];
60
+ return (
61
+ prevNode.id === nextNode.id &&
62
+ prevNode.type === nextNode.type &&
63
+ shallowEqual(prevNode.data, nextNode.data) &&
64
+ shallowEqual(prevNode.properties, nextNode.properties)
65
+ );
66
+ });
67
+ };
68
+
69
+ /**
70
+ * Build a qualified node ID by joining path segments.
71
+ */
72
+ export const qualifyId = (parentId: string, ...segmentIds: string[]): string => [parentId, ...segmentIds].join(PATH);
73
+
74
+ /**
75
+ * Validate that a segment ID does not contain the path separator.
76
+ */
77
+ export const validateSegmentId = (id: string): void => {
78
+ invariant(!id.includes(PATH), `Node segment ID must not contain '${PATH}': ${id}`);
79
+ };
80
+
81
+ /**
82
+ * Extract the parent qualified ID (everything before the last path separator).
83
+ * Returns undefined for IDs with no parent (single segment).
84
+ */
85
+ export const getParentId = (qualifiedId: string): string | undefined => {
86
+ const lastSlash = qualifiedId.lastIndexOf(PATH);
87
+ return lastSlash > 0 ? qualifiedId.slice(0, lastSlash) : undefined;
88
+ };
89
+
90
+ /**
91
+ * Extract the last segment of a qualified ID.
92
+ */
93
+ export const getSegmentId = (qualifiedId: string): string => {
94
+ return qualifiedId.split(PATH).pop() ?? qualifiedId;
95
+ };
@@ -1,25 +0,0 @@
1
- type Cb = () => void;
2
- interface Query {
3
- id?: string[];
4
- typename?: string[];
5
- }
6
- /**
7
- * Lazy query result that can be executed.
8
- * Does not run without the run function being called.
9
- */
10
- interface QueryResult<T> {
11
- /**
12
- * Execute the query and subscribe to the result.
13
- * @param next Called at least once with the first value (maybe synchronously) and then for every subsequent update.
14
- * @param error Called on error. `next` is never called after that.
15
- * @returns Function to dispose the query and unsubscribe.
16
- */
17
- run(onData: (value?: T[]) => void, onError: (err: Error) => void): Cb;
18
- }
19
- declare const QueryResult: Readonly<{
20
- fromPromise: <T>(run: (onDispose: (cb: Cb) => void) => Promise<T[]>) => QueryResult<T>;
21
- }>;
22
- interface _Resolver<T> {
23
- query(query: Query): QueryResult<T>;
24
- }
25
- //# sourceMappingURL=graph-projections.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"graph-projections.test.d.ts","sourceRoot":"","sources":["../../../../src/experimental/graph-projections.test.ts"],"names":[],"mappings":"AAIA,KAAK,EAAE,GAAG,MAAM,IAAI,CAAC;AAErB,UAAU,KAAK;IACb,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB;AAED;;;GAGG;AACH,UAAU,WAAW,CAAC,CAAC;IACrB;;;;;OAKG;IACH,GAAG,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,KAAK,IAAI,EAAE,OAAO,EAAE,CAAC,GAAG,EAAE,KAAK,KAAK,IAAI,GAAG,EAAE,CAAC;CACvE;AAED,QAAA,MAAM,WAAW;kBACD,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC,EAAE,EAAE,EAAE,KAAK,IAAI,KAAK,OAAO,CAAC,CAAC,EAAE,CAAC,KAAG,WAAW,CAAC,CAAC,CAAC;EAyBpF,CAAC;AAEH,UAAU,SAAS,CAAC,CAAC;IACnB,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;CACrC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=signals-integration.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"signals-integration.test.d.ts","sourceRoot":"","sources":["../../../src/signals-integration.test.ts"],"names":[],"mappings":""}
@@ -1,5 +0,0 @@
1
- import { Atom } from '@effect-atom/atom-react';
2
- import { type AnyEchoObject } from '@dxos/echo/internal';
3
- import { type QueryResult } from '@dxos/echo-db';
4
- export declare const atomFromQuery: <T extends AnyEchoObject>(query: QueryResult<T>) => Atom.Atom<T[]>;
5
- //# sourceMappingURL=testing.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../../../src/testing.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAE/C,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,eAAe,CAAC;AAEjD,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,aAAa,EAAE,OAAO,WAAW,CAAC,CAAC,CAAC,KAAG,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,CAU3F,CAAC"}
@@ -1,56 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- type Cb = () => void;
6
-
7
- interface Query {
8
- id?: string[];
9
- typename?: string[];
10
- }
11
-
12
- /**
13
- * Lazy query result that can be executed.
14
- * Does not run without the run function being called.
15
- */
16
- interface QueryResult<T> {
17
- /**
18
- * Execute the query and subscribe to the result.
19
- * @param next Called at least once with the first value (maybe synchronously) and then for every subsequent update.
20
- * @param error Called on error. `next` is never called after that.
21
- * @returns Function to dispose the query and unsubscribe.
22
- */
23
- run(onData: (value?: T[]) => void, onError: (err: Error) => void): Cb;
24
- }
25
-
26
- const QueryResult = Object.freeze({
27
- fromPromise: <T>(run: (onDispose: (cb: Cb) => void) => Promise<T[]>): QueryResult<T> => {
28
- return {
29
- run: (onData, onError) => {
30
- const cbs: Cb[] = [];
31
- let disposed = false;
32
- const dispose = () => {
33
- cbs.forEach((cb) => cb());
34
- disposed = true;
35
- };
36
- run((cb) => (disposed ? cb() : cbs.push(cb))).then(
37
- (data) => {
38
- if (disposed) {
39
- return;
40
- }
41
- onData(data);
42
- },
43
- (err) => {
44
- dispose();
45
- onError(err);
46
- },
47
- );
48
- return dispose;
49
- },
50
- };
51
- },
52
- });
53
-
54
- interface _Resolver<T> {
55
- query(query: Query): QueryResult<T>;
56
- }
@@ -1,219 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Atom, Registry } from '@effect-atom/atom-react';
6
- import { signal } from '@preact/signals-core';
7
- import { afterEach, beforeEach, describe, expect, onTestFinished, test } from 'vitest';
8
-
9
- import { Trigger } from '@dxos/async';
10
- import { Obj, Type } from '@dxos/echo';
11
- import { Ref } from '@dxos/echo/internal';
12
- import { Filter } from '@dxos/echo-db';
13
- import { EchoTestBuilder } from '@dxos/echo-db/testing';
14
- import { registerSignalsRuntime } from '@dxos/echo-signals';
15
-
16
- import { ROOT_ID } from './graph';
17
- import { GraphBuilder, atomFromSignal, createExtension } from './graph-builder';
18
- import { atomFromQuery } from './testing';
19
-
20
- registerSignalsRuntime();
21
-
22
- const EXAMPLE_TYPE = 'dxos.org/type/example';
23
-
24
- describe('signals integration', () => {
25
- test('creating atom from signal', () => {
26
- const registry = Registry.make();
27
- const state = signal<number>(0);
28
- const value = atomFromSignal(() => state.value);
29
- const inline = Atom.make((get) => {
30
- // NOTE: This will create a new atom instance each time.
31
- // This test is verifying that this behaves the same as using a stable atom instance.
32
- // The parent will remain subscribed to one instance until the new one is created.
33
- // The old one will then be garbage collected because it is no longer referenced.
34
- const atom = atomFromSignal(() => get(value));
35
- return get(atom);
36
- });
37
-
38
- let count = 0;
39
- const cancel = registry.subscribe(value, (value) => {
40
- count = value;
41
- });
42
- onTestFinished(() => cancel());
43
-
44
- let inlineCount = 0;
45
- const inlineCancel = registry.subscribe(inline, (value) => {
46
- inlineCount = value;
47
- });
48
- onTestFinished(() => inlineCancel());
49
-
50
- registry.get(value);
51
- registry.get(inline);
52
- expect(count).to.eq(0);
53
- expect(inlineCount).to.eq(0);
54
-
55
- state.value = 1;
56
- expect(count).to.eq(1);
57
- expect(inlineCount).to.eq(1);
58
-
59
- state.value = 2;
60
- expect(count).to.eq(2);
61
- expect(inlineCount).to.eq(2);
62
- });
63
-
64
- describe('echo', () => {
65
- let dbBuilder: EchoTestBuilder;
66
-
67
- beforeEach(async () => {
68
- dbBuilder = await new EchoTestBuilder().open();
69
- });
70
-
71
- afterEach(async () => {
72
- await dbBuilder.close();
73
- });
74
-
75
- test('atom references are loaded lazily and receive signal notifications', async () => {
76
- const registry = Registry.make();
77
- await using peer = await dbBuilder.createPeer();
78
-
79
- let outerId: string;
80
- {
81
- await using db = await peer.createDatabase();
82
- const inner = db.add({ name: 'inner' });
83
- const outer = db.add({ inner: Ref.make(inner) });
84
- outerId = outer.id;
85
- await db.flush();
86
- }
87
-
88
- await peer.reload();
89
- {
90
- await using db = await peer.openLastDatabase();
91
- const outer = (await db.query(Filter.ids(outerId)).first()) as any;
92
- const innerAtom = atomFromSignal(() => outer.inner.target);
93
- const loaded = new Trigger();
94
-
95
- let count = 0;
96
- const cancel = registry.subscribe(innerAtom, (inner) => {
97
- count++;
98
- if (inner) {
99
- loaded.wake();
100
- }
101
- });
102
-
103
- onTestFinished(() => cancel());
104
-
105
- expect(registry.get(innerAtom)).to.eq(undefined);
106
- expect(count).to.eq(1);
107
-
108
- await loaded.wait();
109
- expect(registry.get(innerAtom)).to.include({ name: 'inner' });
110
- expect(count).to.eq(2);
111
- }
112
- });
113
-
114
- test('references graph builder', async () => {
115
- const registry = Registry.make();
116
- await using peer = await dbBuilder.createPeer();
117
-
118
- let outerId, innerId: string;
119
- {
120
- await using db = await peer.createDatabase();
121
- const inner = db.add({ name: 'inner' });
122
- const outer = db.add({ inner: Ref.make(inner) });
123
- innerId = inner.id;
124
- outerId = outer.id;
125
- await db.flush();
126
- }
127
-
128
- await peer.reload();
129
-
130
- {
131
- await using db = await peer.openLastDatabase();
132
- const outer = (await db.query(Filter.ids(outerId)).first()) as any;
133
- const innerAtom = atomFromSignal(() => outer.inner.target);
134
- const inner = registry.get(innerAtom);
135
- expect(inner).to.eq(undefined);
136
-
137
- const builder = new GraphBuilder({ registry });
138
- builder.addExtension(
139
- createExtension({
140
- id: 'outbound-connector',
141
- connector: () =>
142
- Atom.make((get) => {
143
- const inner = get(innerAtom) as any;
144
- return inner ? [{ id: inner.id, type: EXAMPLE_TYPE, data: inner.name }] : [];
145
- }),
146
- }),
147
- );
148
-
149
- const graph = builder.graph;
150
-
151
- const loaded = new Trigger();
152
- let count = 0;
153
- const cancel = registry.subscribe(graph.connections(ROOT_ID), (nodes) => {
154
- count++;
155
- if (nodes.length > 0) {
156
- loaded.wake();
157
- }
158
- });
159
- onTestFinished(() => cancel());
160
- registry.get(graph.connections(ROOT_ID));
161
- expect(count).to.eq(1);
162
-
163
- graph.expand(ROOT_ID);
164
- await loaded.wait();
165
- expect(count).to.eq(2);
166
-
167
- const nodes = registry.get(graph.connections(ROOT_ID));
168
- expect(nodes).has.length(1);
169
- expect(nodes[0].id).to.eq(innerId);
170
- expect(nodes[0].data).to.eq('inner');
171
- }
172
- });
173
-
174
- test('query graph builder', async () => {
175
- const registry = Registry.make();
176
- await using peer = await dbBuilder.createPeer();
177
- await using db = await peer.createDatabase();
178
- db.add(Obj.make(Type.Expando, { name: 'a' }));
179
- db.add(Obj.make(Type.Expando, { name: 'b' }));
180
-
181
- const builder = new GraphBuilder({ registry });
182
- builder.addExtension(
183
- createExtension({
184
- id: 'expando',
185
- connector: () => {
186
- const query = db.query(Filter.type(Type.Expando));
187
-
188
- return Atom.make((get) => {
189
- const objects = get(atomFromQuery(query));
190
- return objects.map((object) => ({ id: object.id, type: EXAMPLE_TYPE, data: object.name }));
191
- });
192
- },
193
- }),
194
- );
195
-
196
- const graph = builder.graph;
197
- let count = 0;
198
- const cancel = registry.subscribe(graph.connections(ROOT_ID), (nodes) => {
199
- count = nodes.length;
200
- });
201
- onTestFinished(() => cancel());
202
-
203
- registry.get(graph.connections(ROOT_ID));
204
- expect(count).to.eq(0);
205
-
206
- graph.expand(ROOT_ID);
207
- expect(count).to.eq(2);
208
-
209
- const object = db.add(Obj.make(Type.Expando, { name: 'c' }));
210
- await db.flush();
211
- expect(count).to.eq(3);
212
-
213
- // NOTE: This graph builder is not reactive to the object update.
214
- object.name = 'updated';
215
- await db.flush();
216
- expect(count).to.eq(3);
217
- });
218
- });
219
- });
package/src/testing.ts DELETED
@@ -1,20 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { Atom } from '@effect-atom/atom-react';
6
-
7
- import { type AnyEchoObject } from '@dxos/echo/internal';
8
- import { type QueryResult } from '@dxos/echo-db';
9
-
10
- export const atomFromQuery = <T extends AnyEchoObject>(query: QueryResult<T>): Atom.Atom<T[]> => {
11
- return Atom.make((get) => {
12
- const unsubscribe = query.subscribe((result) => {
13
- get.setSelf(result.objects);
14
- });
15
-
16
- get.addFinalizer(() => unsubscribe());
17
-
18
- return query.objects;
19
- });
20
- };