@dxos/app-graph 0.8.4-main.406dc2a → 0.8.4-main.40e3dcdf1b

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 (46) hide show
  1. package/dist/lib/browser/index.mjs +1416 -695
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +1415 -695
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/atoms.d.ts +8 -0
  8. package/dist/types/src/atoms.d.ts.map +1 -0
  9. package/dist/types/src/graph-builder.d.ts +112 -66
  10. package/dist/types/src/graph-builder.d.ts.map +1 -1
  11. package/dist/types/src/graph.d.ts +187 -221
  12. package/dist/types/src/graph.d.ts.map +1 -1
  13. package/dist/types/src/index.d.ts +6 -3
  14. package/dist/types/src/index.d.ts.map +1 -1
  15. package/dist/types/src/node-matcher.d.ts +218 -0
  16. package/dist/types/src/node-matcher.d.ts.map +1 -0
  17. package/dist/types/src/node-matcher.test.d.ts +2 -0
  18. package/dist/types/src/node-matcher.test.d.ts.map +1 -0
  19. package/dist/types/src/node.d.ts +44 -5
  20. package/dist/types/src/node.d.ts.map +1 -1
  21. package/dist/types/src/stories/EchoGraph.stories.d.ts.map +1 -1
  22. package/dist/types/src/util.d.ts +34 -0
  23. package/dist/types/src/util.d.ts.map +1 -0
  24. package/dist/types/tsconfig.tsbuildinfo +1 -1
  25. package/package.json +36 -34
  26. package/src/atoms.ts +25 -0
  27. package/src/graph-builder.test.ts +895 -137
  28. package/src/graph-builder.ts +703 -289
  29. package/src/graph.test.ts +450 -122
  30. package/src/graph.ts +1052 -401
  31. package/src/index.ts +9 -3
  32. package/src/node-matcher.test.ts +301 -0
  33. package/src/node-matcher.ts +282 -0
  34. package/src/node.ts +56 -8
  35. package/src/stories/EchoGraph.stories.tsx +159 -120
  36. package/src/stories/Tree.tsx +1 -1
  37. package/src/util.ts +71 -0
  38. package/dist/types/src/experimental/graph-projections.test.d.ts +0 -25
  39. package/dist/types/src/experimental/graph-projections.test.d.ts.map +0 -1
  40. package/dist/types/src/signals-integration.test.d.ts +0 -2
  41. package/dist/types/src/signals-integration.test.d.ts.map +0 -1
  42. package/dist/types/src/testing.d.ts +0 -5
  43. package/dist/types/src/testing.d.ts.map +0 -1
  44. package/src/experimental/graph-projections.test.ts +0 -56
  45. package/src/signals-integration.test.ts +0 -218
  46. package/src/testing.ts +0 -20
@@ -1,35 +1,150 @@
1
1
  import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+ var __defProp = Object.defineProperty;
3
+ var __export = (target, all) => {
4
+ for (var name in all)
5
+ __defProp(target, name, { get: all[name], enumerable: true });
6
+ };
7
+
8
+ // src/atoms.ts
9
+ var atoms_exports = {};
10
+ __export(atoms_exports, {
11
+ fromObservable: () => fromObservable
12
+ });
13
+ import { Atom } from "@effect-atom/atom-react";
14
+ var observableFamily = Atom.family((observable) => {
15
+ return Atom.make((get2) => {
16
+ const subscription = observable.subscribe((value) => get2.setSelf(value));
17
+ get2.addFinalizer(() => subscription.unsubscribe());
18
+ return observable.get();
19
+ });
20
+ });
21
+ var fromObservable = (observable) => {
22
+ return observableFamily(observable);
23
+ };
2
24
 
3
25
  // src/graph.ts
4
- import { Registry, Rx } from "@effect-rx/rx-react";
26
+ var graph_exports = {};
27
+ __export(graph_exports, {
28
+ GraphKind: () => GraphKind,
29
+ GraphTypeId: () => GraphTypeId,
30
+ addEdge: () => addEdge,
31
+ addEdges: () => addEdges,
32
+ addNode: () => addNode,
33
+ addNodes: () => addNodes,
34
+ expand: () => expand,
35
+ getActions: () => getActions,
36
+ getConnections: () => getConnections,
37
+ getEdges: () => getEdges,
38
+ getGraph: () => getGraph,
39
+ getNode: () => getNode,
40
+ getNodeOrThrow: () => getNodeOrThrow,
41
+ getPath: () => getPath,
42
+ getRoot: () => getRoot,
43
+ initialize: () => initialize,
44
+ make: () => make,
45
+ relationFromKey: () => relationFromKey,
46
+ relationKey: () => relationKey,
47
+ removeEdge: () => removeEdge,
48
+ removeEdges: () => removeEdges,
49
+ removeNode: () => removeNode,
50
+ removeNodes: () => removeNodes,
51
+ sortEdges: () => sortEdges,
52
+ toJSON: () => toJSON,
53
+ traverse: () => traverse,
54
+ waitForPath: () => waitForPath
55
+ });
56
+ import { Atom as Atom2, Registry } from "@effect-atom/atom-react";
5
57
  import * as Function from "effect/Function";
6
58
  import * as Option from "effect/Option";
59
+ import * as Pipeable from "effect/Pipeable";
7
60
  import * as Record from "effect/Record";
8
61
  import { Event, Trigger } from "@dxos/async";
9
62
  import { todo } from "@dxos/debug";
10
- import { invariant } from "@dxos/invariant";
63
+ import { invariant as invariant2 } from "@dxos/invariant";
11
64
  import { log } from "@dxos/log";
12
65
  import { isNonNullable } from "@dxos/util";
13
- function _define_property(obj, key, value) {
14
- if (key in obj) {
15
- Object.defineProperty(obj, key, {
16
- value,
17
- enumerable: true,
18
- configurable: true,
19
- writable: true
20
- });
21
- } else {
22
- obj[key] = value;
66
+
67
+ // src/node.ts
68
+ var node_exports = {};
69
+ __export(node_exports, {
70
+ ActionGroupType: () => ActionGroupType,
71
+ ActionType: () => ActionType,
72
+ RootId: () => RootId,
73
+ RootType: () => RootType,
74
+ actionGroupSymbol: () => actionGroupSymbol,
75
+ actionRelation: () => actionRelation,
76
+ childRelation: () => childRelation,
77
+ isAction: () => isAction,
78
+ isActionGroup: () => isActionGroup,
79
+ isActionLike: () => isActionLike,
80
+ isGraphNode: () => isGraphNode,
81
+ relation: () => relation
82
+ });
83
+ var RootId = "root";
84
+ var RootType = "org.dxos.type.graph-root";
85
+ var ActionType = "org.dxos.type.graph-action";
86
+ var ActionGroupType = "org.dxos.type.graph-action-group";
87
+ var relation = (kind, direction = "outbound") => ({
88
+ kind,
89
+ direction
90
+ });
91
+ var childRelation = (direction = "outbound") => relation("child", direction);
92
+ var actionRelation = (direction = "outbound") => relation("action", direction);
93
+ var isGraphNode = (data) => data && typeof data === "object" && "id" in data && "properties" in data && data.properties ? typeof data.properties === "object" && "data" in data : false;
94
+ var isAction = (data) => isGraphNode(data) ? typeof data.data === "function" && data.type === ActionType : false;
95
+ var actionGroupSymbol = /* @__PURE__ */ Symbol("ActionGroup");
96
+ var isActionGroup = (data) => isGraphNode(data) ? data.data === actionGroupSymbol && data.type === ActionGroupType : false;
97
+ var isActionLike = (data) => isAction(data) || isActionGroup(data);
98
+
99
+ // src/util.ts
100
+ import { invariant } from "@dxos/invariant";
101
+ var __dxlog_file = "/__w/dxos/dxos/packages/sdk/app-graph/src/util.ts";
102
+ var Separators = {
103
+ primary: "",
104
+ secondary: "",
105
+ path: "/"
106
+ };
107
+ var normalizeRelation = (relation2) => relation2 == null ? childRelation() : typeof relation2 === "string" ? relation(relation2) : relation2;
108
+ var shallowEqual = (a, b) => {
109
+ if (a === b) return true;
110
+ if (a == null || b == null || typeof a !== "object" || typeof b !== "object") return false;
111
+ const keysA = Object.keys(a);
112
+ const keysB = Object.keys(b);
113
+ if (keysA.length !== keysB.length) {
114
+ return false;
23
115
  }
24
- return obj;
25
- }
26
- var __dxlog_file = "/__w/dxos/dxos/packages/sdk/app-graph/src/graph.ts";
27
- var graphSymbol = Symbol("graph");
116
+ return keysA.every((k) => a[k] === b[k]);
117
+ };
118
+ var nodeArgsUnchanged = (prev, next) => {
119
+ if (prev.length !== next.length) {
120
+ return false;
121
+ }
122
+ return prev.every((prevNode, idx) => {
123
+ const nextNode = next[idx];
124
+ return prevNode.id === nextNode.id && prevNode.type === nextNode.type && shallowEqual(prevNode.data, nextNode.data) && shallowEqual(prevNode.properties, nextNode.properties);
125
+ });
126
+ };
127
+ var qualifyId = (parentId, segmentId) => `${parentId}${Separators.path}${segmentId}`;
128
+ var validateSegmentId = (id) => {
129
+ invariant(!id.includes(Separators.path), `Node segment ID must not contain '${Separators.path}': ${id}`, {
130
+ F: __dxlog_file,
131
+ L: 70,
132
+ S: void 0,
133
+ A: [
134
+ "!id.includes(Separators.path)",
135
+ "`Node segment ID must not contain '${Separators.path}': ${id}`"
136
+ ]
137
+ });
138
+ };
139
+
140
+ // src/graph.ts
141
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/sdk/app-graph/src/graph.ts";
142
+ var graphSymbol = /* @__PURE__ */ Symbol("graph");
28
143
  var getGraph = (node) => {
29
144
  const graph = node[graphSymbol];
30
- invariant(graph, "Node is not associated with a graph.", {
31
- F: __dxlog_file,
32
- L: 27,
145
+ invariant2(graph, "Node is not associated with a graph.", {
146
+ F: __dxlog_file2,
147
+ L: 33,
33
148
  S: void 0,
34
149
  A: [
35
150
  "graph",
@@ -38,499 +153,1261 @@ var getGraph = (node) => {
38
153
  });
39
154
  return graph;
40
155
  };
41
- var ROOT_ID = "root";
42
- var ROOT_TYPE = "dxos.org/type/GraphRoot";
43
- var ACTION_TYPE = "dxos.org/type/GraphAction";
44
- var ACTION_GROUP_TYPE = "dxos.org/type/GraphActionGroup";
45
- var Graph = class {
46
- toJSON(id = ROOT_ID) {
47
- return this._registry.get(this._json(id));
156
+ var GraphTypeId = /* @__PURE__ */ Symbol.for("@dxos/app-graph/Graph");
157
+ var GraphKind = /* @__PURE__ */ Symbol.for("@dxos/app-graph/GraphKind");
158
+ var GraphImpl = class {
159
+ [GraphTypeId] = GraphTypeId;
160
+ [GraphKind] = "writable";
161
+ pipe() {
162
+ return Pipeable.pipeArguments(this, arguments);
163
+ }
164
+ onNodeChanged = new Event();
165
+ _onExpand;
166
+ _onInitialize;
167
+ _onRemoveNode;
168
+ _registry;
169
+ _expanded = Record.empty();
170
+ _pendingExpands = /* @__PURE__ */ new Set();
171
+ _initialized = Record.empty();
172
+ _initialEdges = Record.empty();
173
+ _initialNodes = Record.fromEntries([
174
+ [
175
+ RootId,
176
+ this._constructNode({
177
+ id: RootId,
178
+ type: RootType,
179
+ data: null,
180
+ properties: {}
181
+ })
182
+ ]
183
+ ]);
184
+ /** @internal */
185
+ _node = Atom2.family((id) => {
186
+ const initial = Option.flatten(Record.get(this._initialNodes, id));
187
+ return Atom2.make(initial).pipe(Atom2.keepAlive, Atom2.withLabel(`graph:node:${id}`));
188
+ });
189
+ _nodeOrThrow = Atom2.family((id) => {
190
+ return Atom2.make((get2) => {
191
+ const node = get2(this._node(id));
192
+ invariant2(Option.isSome(node), `Node not available: ${id}`, {
193
+ F: __dxlog_file2,
194
+ L: 172,
195
+ S: this,
196
+ A: [
197
+ "Option.isSome(node)",
198
+ "`Node not available: ${id}`"
199
+ ]
200
+ });
201
+ return node.value;
202
+ });
203
+ });
204
+ _edges = Atom2.family((id) => {
205
+ const initial = Record.get(this._initialEdges, id).pipe(Option.getOrElse(() => ({})));
206
+ return Atom2.make(initial).pipe(Atom2.keepAlive, Atom2.withLabel(`graph:edges:${id}`));
207
+ });
208
+ // NOTE: Currently the argument to the family needs to be referentially stable for the atom to be referentially stable.
209
+ // TODO(wittjosiah): Atom feature request, support for something akin to `ComplexMap` to allow for complex arguments.
210
+ _connections = Atom2.family((key) => {
211
+ return Atom2.make((get2) => {
212
+ if (!key || key.indexOf(Separators.primary) <= 0) {
213
+ return [];
214
+ }
215
+ const { id, relation: relation2 } = relationFromConnectionKey(key);
216
+ const edges = get2(this._edges(id));
217
+ return (edges[relationKey(relation2)] ?? []).map((id2) => get2(this._node(id2))).filter(Option.isSome).map((o) => o.value);
218
+ }).pipe(Atom2.withLabel(`graph:connections:${key}`));
219
+ });
220
+ _actions = Atom2.family((id) => {
221
+ return Atom2.make((get2) => {
222
+ if (!id) {
223
+ return [];
224
+ }
225
+ return get2(this._connections(connectionKey(id, actionRelation())));
226
+ }).pipe(Atom2.withLabel(`graph:actions:${id}`));
227
+ });
228
+ _json = Atom2.family((id) => {
229
+ return Atom2.make((get2) => {
230
+ const toJSON2 = (node, seen = []) => {
231
+ const nodes = get2(this._connections(connectionKey(node.id, "child")));
232
+ const obj = {
233
+ id: node.id,
234
+ type: node.type
235
+ };
236
+ if (node.properties.label) {
237
+ obj.label = node.properties.label;
238
+ }
239
+ if (nodes.length) {
240
+ obj.nodes = nodes.map((n) => {
241
+ const nextSeen = [
242
+ ...seen,
243
+ node.id
244
+ ];
245
+ return nextSeen.includes(n.id) ? void 0 : toJSON2(n, nextSeen);
246
+ }).filter(isNonNullable);
247
+ }
248
+ return obj;
249
+ };
250
+ const root = get2(this._nodeOrThrow(id));
251
+ return toJSON2(root);
252
+ }).pipe(Atom2.withLabel(`graph:json:${id}`));
253
+ });
254
+ constructor({ registry, nodes, edges, onInitialize, onExpand, onRemoveNode } = {}) {
255
+ this._registry = registry ?? Registry.make();
256
+ this._onInitialize = onInitialize;
257
+ this._onExpand = onExpand;
258
+ this._onRemoveNode = onRemoveNode;
259
+ if (nodes) {
260
+ nodes.forEach((node) => {
261
+ Record.set(this._initialNodes, node.id, this._constructNode(node));
262
+ });
263
+ }
264
+ if (edges) {
265
+ Object.entries(edges).forEach(([source, edges2]) => {
266
+ Record.set(this._initialEdges, source, edges2);
267
+ });
268
+ }
48
269
  }
49
- json(id = ROOT_ID) {
50
- return this._json(id);
270
+ json(id = RootId) {
271
+ return jsonImpl(this, id);
51
272
  }
52
273
  node(id) {
53
- return this._node(id);
274
+ return nodeImpl(this, id);
54
275
  }
55
276
  nodeOrThrow(id) {
56
- return this._nodeOrThrow(id);
277
+ return nodeOrThrowImpl(this, id);
57
278
  }
58
- connections(id, relation = "outbound") {
59
- return this._connections(`${id}$${relation}`);
279
+ connections(id, relation2) {
280
+ return connectionsImpl(this, id, relation2);
60
281
  }
61
282
  actions(id) {
62
- return this._actions(id);
283
+ return actionsImpl(this, id);
63
284
  }
64
285
  edges(id) {
65
- return this._edges(id);
286
+ return edgesImpl(this, id);
287
+ }
288
+ /** @internal */
289
+ _constructNode(node) {
290
+ return Option.some({
291
+ [graphSymbol]: this,
292
+ data: null,
293
+ properties: {},
294
+ ...node
295
+ });
296
+ }
297
+ };
298
+ var getInternal = (graph) => {
299
+ return graph;
300
+ };
301
+ var toJSON = (graph, id = RootId) => {
302
+ const internal = getInternal(graph);
303
+ return internal._registry.get(internal._json(id));
304
+ };
305
+ var jsonImpl = (graph, id = RootId) => {
306
+ const internal = getInternal(graph);
307
+ return internal._json(id);
308
+ };
309
+ var nodeImpl = (graph, id) => {
310
+ const internal = getInternal(graph);
311
+ return internal._node(id);
312
+ };
313
+ var nodeOrThrowImpl = (graph, id) => {
314
+ const internal = getInternal(graph);
315
+ return internal._nodeOrThrow(id);
316
+ };
317
+ var connectionsImpl = (graph, id, relation2) => {
318
+ const internal = getInternal(graph);
319
+ return internal._connections(connectionKey(id, relation2));
320
+ };
321
+ var actionsImpl = (graph, id) => {
322
+ const internal = getInternal(graph);
323
+ return internal._actions(id);
324
+ };
325
+ var edgesImpl = (graph, id) => {
326
+ const internal = getInternal(graph);
327
+ return internal._edges(id);
328
+ };
329
+ var getNodeImpl = (graph, id) => {
330
+ const internal = getInternal(graph);
331
+ return internal._registry.get(nodeImpl(graph, id));
332
+ };
333
+ function getNode(graphOrId, id) {
334
+ if (typeof graphOrId === "string") {
335
+ const id2 = graphOrId;
336
+ return (graph) => getNodeImpl(graph, id2);
337
+ } else {
338
+ const graph = graphOrId;
339
+ return getNodeImpl(graph, id);
66
340
  }
67
- get root() {
68
- return this.getNodeOrThrow(ROOT_ID);
341
+ }
342
+ var getNodeOrThrowImpl = (graph, id) => {
343
+ const internal = getInternal(graph);
344
+ return internal._registry.get(nodeOrThrowImpl(graph, id));
345
+ };
346
+ function getNodeOrThrow(graphOrId, id) {
347
+ if (typeof graphOrId === "string") {
348
+ const id2 = graphOrId;
349
+ return (graph) => getNodeOrThrowImpl(graph, id2);
350
+ } else {
351
+ const graph = graphOrId;
352
+ return getNodeOrThrowImpl(graph, id);
69
353
  }
70
- getNode(id) {
71
- return this._registry.get(this.node(id));
354
+ }
355
+ function getRoot(graph) {
356
+ return getNodeOrThrowImpl(graph, RootId);
357
+ }
358
+ var getConnectionsImpl = (graph, id, relation2) => {
359
+ const internal = getInternal(graph);
360
+ return internal._registry.get(connectionsImpl(graph, id, relation2));
361
+ };
362
+ function getConnections(graphOrId, idOrRelation, relation2) {
363
+ if (typeof graphOrId === "string") {
364
+ const id = graphOrId;
365
+ const rel = idOrRelation;
366
+ return (graph) => getConnectionsImpl(graph, id, rel);
367
+ } else {
368
+ const graph = graphOrId;
369
+ const id = idOrRelation;
370
+ invariant2(relation2 !== void 0, "Relation is required.", {
371
+ F: __dxlog_file2,
372
+ L: 446,
373
+ S: this,
374
+ A: [
375
+ "relation !== undefined",
376
+ "'Relation is required.'"
377
+ ]
378
+ });
379
+ const rel = relation2;
380
+ return getConnectionsImpl(graph, id, rel);
72
381
  }
73
- getNodeOrThrow(id) {
74
- return this._registry.get(this.nodeOrThrow(id));
382
+ }
383
+ var getActionsImpl = (graph, id) => {
384
+ const internal = getInternal(graph);
385
+ return internal._registry.get(actionsImpl(graph, id));
386
+ };
387
+ function getActions(graphOrId, id) {
388
+ if (typeof graphOrId === "string") {
389
+ const id2 = graphOrId;
390
+ return (graph) => getActionsImpl(graph, id2);
391
+ } else {
392
+ const graph = graphOrId;
393
+ return getActionsImpl(graph, id);
75
394
  }
76
- getConnections(id, relation = "outbound") {
77
- return this._registry.get(this.connections(id, relation));
395
+ }
396
+ var getEdgesImpl = (graph, id) => {
397
+ const internal = getInternal(graph);
398
+ return internal._registry.get(edgesImpl(graph, id));
399
+ };
400
+ function getEdges(graphOrId, id) {
401
+ if (typeof graphOrId === "string") {
402
+ const id2 = graphOrId;
403
+ return (graph) => getEdgesImpl(graph, id2);
404
+ } else {
405
+ const graph = graphOrId;
406
+ return getEdgesImpl(graph, id);
78
407
  }
79
- getActions(id) {
80
- return this._registry.get(this.actions(id));
408
+ }
409
+ var traverseImpl = (graph, options, path = []) => {
410
+ const { visitor, source = RootId, relation: relation2 } = options;
411
+ if (path.includes(source)) {
412
+ return;
81
413
  }
82
- getEdges(id) {
83
- return this._registry.get(this.edges(id));
414
+ const node = getNodeOrThrow(graph, source);
415
+ const shouldContinue = visitor(node, [
416
+ ...path,
417
+ source
418
+ ]);
419
+ if (shouldContinue === false) {
420
+ return;
84
421
  }
85
- async initialize(id) {
86
- const initialized = Record.get(this._initialized, id).pipe(Option.getOrElse(() => false));
87
- log("initialize", {
88
- id,
89
- initialized
90
- }, {
91
- F: __dxlog_file,
92
- L: 386,
93
- S: this,
94
- C: (f, a) => f(...a)
422
+ const relations = Array.isArray(relation2) ? relation2 : [
423
+ relation2
424
+ ];
425
+ const seen = /* @__PURE__ */ new Set();
426
+ for (const rel of relations) {
427
+ for (const connected of getConnections(graph, source, rel)) {
428
+ if (!seen.has(connected.id)) {
429
+ seen.add(connected.id);
430
+ traverseImpl(graph, {
431
+ source: connected.id,
432
+ relation: relation2,
433
+ visitor
434
+ }, [
435
+ ...path,
436
+ source
437
+ ]);
438
+ }
439
+ }
440
+ }
441
+ };
442
+ function traverse(graphOrOptions, optionsOrPath, path) {
443
+ if (typeof graphOrOptions === "object" && "visitor" in graphOrOptions) {
444
+ const options = graphOrOptions;
445
+ const pathArg = Array.isArray(optionsOrPath) ? optionsOrPath : void 0;
446
+ return (graph) => traverseImpl(graph, options, pathArg);
447
+ } else {
448
+ const graph = graphOrOptions;
449
+ const options = optionsOrPath;
450
+ const pathArg = path ?? (Array.isArray(optionsOrPath) ? optionsOrPath : void 0);
451
+ return traverseImpl(graph, options, pathArg);
452
+ }
453
+ }
454
+ var getPathImpl = (graph, params) => {
455
+ return Function.pipe(getNode(graph, params.source ?? "root"), Option.flatMap((node) => {
456
+ let found = Option.none();
457
+ traverseImpl(graph, {
458
+ source: node.id,
459
+ relation: "child",
460
+ visitor: (node2, path) => {
461
+ if (Option.isSome(found)) {
462
+ return false;
463
+ }
464
+ if (node2.id === params.target) {
465
+ found = Option.some(path);
466
+ }
467
+ }
95
468
  });
96
- if (!initialized) {
97
- await this._onInitialize?.(id);
98
- Record.set(this._initialized, id, true);
469
+ return found;
470
+ }));
471
+ };
472
+ function getPath(graphOrParams, params) {
473
+ if (params === void 0 && typeof graphOrParams === "object" && "target" in graphOrParams) {
474
+ const params2 = graphOrParams;
475
+ return (graph) => getPathImpl(graph, params2);
476
+ } else {
477
+ const graph = graphOrParams;
478
+ return getPathImpl(graph, params);
479
+ }
480
+ }
481
+ var waitForPathImpl = (graph, params, options) => {
482
+ const { timeout = 5e3, interval = 500 } = options ?? {};
483
+ const path = getPathImpl(graph, params);
484
+ if (Option.isSome(path)) {
485
+ return Promise.resolve(path.value);
486
+ }
487
+ const trigger = new Trigger();
488
+ const i = setInterval(() => {
489
+ const path2 = getPathImpl(graph, params);
490
+ if (Option.isSome(path2)) {
491
+ trigger.wake(path2.value);
99
492
  }
493
+ }, interval);
494
+ return trigger.wait({
495
+ timeout
496
+ }).finally(() => clearInterval(i));
497
+ };
498
+ function waitForPath(graphOrParams, paramsOrOptions, options) {
499
+ if (typeof graphOrParams === "object" && "target" in graphOrParams) {
500
+ const params = graphOrParams;
501
+ const opts = typeof paramsOrOptions === "object" && !("target" in paramsOrOptions) ? paramsOrOptions : void 0;
502
+ return (graph) => waitForPathImpl(graph, params, opts);
503
+ } else {
504
+ const graph = graphOrParams;
505
+ const params = paramsOrOptions;
506
+ return waitForPathImpl(graph, params, options);
100
507
  }
101
- expand(id, relation = "outbound") {
102
- const key = `${id}$${relation}`;
103
- const expanded = Record.get(this._expanded, key).pipe(Option.getOrElse(() => false));
508
+ }
509
+ var initializeImpl = async (graph, id) => {
510
+ const internal = getInternal(graph);
511
+ const initialized = Record.get(internal._initialized, id).pipe(Option.getOrElse(() => false));
512
+ log("initialize", {
513
+ id,
514
+ initialized
515
+ }, {
516
+ F: __dxlog_file2,
517
+ L: 668,
518
+ S: void 0,
519
+ C: (f, a) => f(...a)
520
+ });
521
+ if (!initialized) {
522
+ Record.set(internal._initialized, id, true);
523
+ await internal._onInitialize?.(id);
524
+ }
525
+ return graph;
526
+ };
527
+ function initialize(graphOrId, id) {
528
+ if (typeof graphOrId === "string") {
529
+ const id2 = graphOrId;
530
+ return (graph) => initializeImpl(graph, id2);
531
+ } else {
532
+ const graph = graphOrId;
533
+ return initializeImpl(graph, id);
534
+ }
535
+ }
536
+ var expandImpl = (graph, id, relation2) => {
537
+ const internal = getInternal(graph);
538
+ const normalizedRelation = normalizeRelation(relation2);
539
+ const key = `${id}${Separators.primary}${relationKey(normalizedRelation)}`;
540
+ const nodeOpt = internal._registry.get(internal._node(id));
541
+ if (Option.isNone(nodeOpt)) {
542
+ internal._pendingExpands.add(key);
104
543
  log("expand", {
105
544
  key,
106
- expanded
545
+ deferred: true
107
546
  }, {
108
- F: __dxlog_file,
109
- L: 396,
110
- S: this,
547
+ F: __dxlog_file2,
548
+ L: 714,
549
+ S: void 0,
111
550
  C: (f, a) => f(...a)
112
551
  });
113
- if (!expanded) {
114
- this._onExpand?.(id, relation);
115
- Record.set(this._expanded, key, true);
116
- }
552
+ return graph;
553
+ }
554
+ const expanded = Record.get(internal._expanded, key).pipe(Option.getOrElse(() => false));
555
+ log("expand", {
556
+ key,
557
+ expanded
558
+ }, {
559
+ F: __dxlog_file2,
560
+ L: 719,
561
+ S: void 0,
562
+ C: (f, a) => f(...a)
563
+ });
564
+ if (!expanded) {
565
+ Record.set(internal._expanded, key, true);
566
+ internal._onExpand?.(id, normalizedRelation);
117
567
  }
118
- addNodes(nodes) {
119
- Rx.batch(() => {
120
- nodes.map((node) => this.addNode(node));
568
+ return graph;
569
+ };
570
+ function expand(graphOrId, idOrRelation, relation2) {
571
+ if (typeof graphOrId === "string") {
572
+ const id = graphOrId;
573
+ const rel = idOrRelation;
574
+ return (graph) => expandImpl(graph, id, rel);
575
+ } else {
576
+ const graph = graphOrId;
577
+ const id = idOrRelation;
578
+ invariant2(relation2 !== void 0, "Relation is required.", {
579
+ F: __dxlog_file2,
580
+ L: 755,
581
+ S: this,
582
+ A: [
583
+ "relation !== undefined",
584
+ "'Relation is required.'"
585
+ ]
121
586
  });
587
+ const rel = relation2;
588
+ return expandImpl(graph, id, rel);
122
589
  }
123
- addNode({ nodes, edges, ...nodeArg }) {
124
- const { id, type, data = null, properties = {} } = nodeArg;
125
- const nodeRx = this._node(id);
126
- const node = this._registry.get(nodeRx);
127
- Option.match(node, {
128
- onSome: (node2) => {
129
- const typeChanged = node2.type !== type;
130
- const dataChanged = node2.data !== data;
131
- const propertiesChanged = Object.keys(properties).some((key) => node2.properties[key] !== properties[key]);
132
- log("existing node", {
133
- id,
134
- typeChanged,
135
- dataChanged,
136
- propertiesChanged
137
- }, {
138
- F: __dxlog_file,
139
- L: 418,
140
- S: this,
141
- C: (f, a) => f(...a)
142
- });
143
- if (typeChanged || dataChanged || propertiesChanged) {
144
- log("updating node", {
145
- id,
146
- type,
147
- data,
148
- properties
149
- }, {
150
- F: __dxlog_file,
151
- L: 420,
152
- S: this,
153
- C: (f, a) => f(...a)
154
- });
155
- const newNode = Option.some({
156
- ...node2,
157
- type,
158
- data,
159
- properties: {
160
- ...node2.properties,
161
- ...properties
162
- }
163
- });
164
- this._registry.set(nodeRx, newNode);
165
- this.onNodeChanged.emit({
166
- id,
167
- node: newNode
168
- });
169
- }
170
- },
171
- onNone: () => {
172
- log("new node", {
590
+ }
591
+ var sortEdgesImpl = (graph, id, relation2, order) => {
592
+ const internal = getInternal(graph);
593
+ const edgesAtom = internal._edges(id);
594
+ const edges = internal._registry.get(edgesAtom);
595
+ const relationId = relationKey(relation2);
596
+ const current = edges[relationId] ?? [];
597
+ const unsorted = current.filter((id2) => !order.includes(id2));
598
+ const sorted = order.filter((id2) => current.includes(id2));
599
+ const newOrder = [
600
+ ...sorted,
601
+ ...unsorted
602
+ ];
603
+ if (newOrder.length === current.length && newOrder.every((id2, i) => id2 === current[i])) {
604
+ return graph;
605
+ }
606
+ internal._registry.set(edgesAtom, {
607
+ ...edges,
608
+ [relationId]: newOrder
609
+ });
610
+ return graph;
611
+ };
612
+ function sortEdges(graphOrId, idOrRelation, relationOrOrder, order) {
613
+ if (typeof graphOrId === "string") {
614
+ const id = graphOrId;
615
+ const relation2 = idOrRelation;
616
+ const order2 = relationOrOrder;
617
+ return (graph) => sortEdgesImpl(graph, id, relation2, order2);
618
+ } else {
619
+ const graph = graphOrId;
620
+ const id = idOrRelation;
621
+ const relation2 = relationOrOrder;
622
+ return sortEdgesImpl(graph, id, relation2, order);
623
+ }
624
+ }
625
+ var addNodesImpl = (graph, nodes) => {
626
+ Atom2.batch(() => {
627
+ nodes.map((node) => addNodeImpl(graph, node));
628
+ });
629
+ return graph;
630
+ };
631
+ function addNodes(graphOrNodes, nodes) {
632
+ if (nodes === void 0) {
633
+ const nodes2 = graphOrNodes;
634
+ return (graph) => addNodesImpl(graph, nodes2);
635
+ } else {
636
+ const graph = graphOrNodes;
637
+ return addNodesImpl(graph, nodes);
638
+ }
639
+ }
640
+ var addNodeImpl = (graph, nodeArg) => {
641
+ const internal = getInternal(graph);
642
+ const { nodes, edges, id, type, data = null, properties = {}, ...rest } = nodeArg;
643
+ const nodeAtom = internal._node(id);
644
+ const existingNode = internal._registry.get(nodeAtom);
645
+ Option.match(existingNode, {
646
+ onSome: (existing) => {
647
+ const typeChanged = existing.type !== type;
648
+ const dataChanged = !shallowEqual(existing.data, data);
649
+ const propertiesChanged = Object.keys(properties).some((key) => existing.properties[key] !== properties[key]);
650
+ log("existing node", {
651
+ id,
652
+ typeChanged,
653
+ dataChanged,
654
+ propertiesChanged
655
+ }, {
656
+ F: __dxlog_file2,
657
+ L: 877,
658
+ S: void 0,
659
+ C: (f, a) => f(...a)
660
+ });
661
+ if (typeChanged || dataChanged || propertiesChanged) {
662
+ log("updating node", {
173
663
  id,
174
664
  type,
175
665
  data,
176
666
  properties
177
667
  }, {
178
- F: __dxlog_file,
179
- L: 427,
180
- S: this,
668
+ F: __dxlog_file2,
669
+ L: 884,
670
+ S: void 0,
181
671
  C: (f, a) => f(...a)
182
672
  });
183
- const newNode = this._constructNode({
184
- id,
673
+ const newNode = Option.some({
674
+ ...existing,
675
+ ...rest,
185
676
  type,
186
677
  data,
187
- properties
678
+ properties: {
679
+ ...existing.properties,
680
+ ...properties
681
+ }
188
682
  });
189
- this._registry.set(nodeRx, newNode);
190
- this.onNodeChanged.emit({
683
+ internal._registry.set(nodeAtom, newNode);
684
+ graph.onNodeChanged.emit({
191
685
  id,
192
686
  node: newNode
193
687
  });
194
688
  }
689
+ },
690
+ onNone: () => {
691
+ log("new node", {
692
+ id,
693
+ type,
694
+ data,
695
+ properties
696
+ }, {
697
+ F: __dxlog_file2,
698
+ L: 897,
699
+ S: void 0,
700
+ C: (f, a) => f(...a)
701
+ });
702
+ const newNode = internal._constructNode({
703
+ id,
704
+ type,
705
+ data,
706
+ properties,
707
+ ...rest
708
+ });
709
+ internal._registry.set(nodeAtom, newNode);
710
+ graph.onNodeChanged.emit({
711
+ id,
712
+ node: newNode
713
+ });
714
+ const prefix = `${id}${Separators.primary}`;
715
+ const toApply = [
716
+ ...internal._pendingExpands
717
+ ].filter((k) => k.startsWith(prefix));
718
+ for (const pendingKey of toApply) {
719
+ internal._pendingExpands.delete(pendingKey);
720
+ const relation2 = relationFromKey(pendingKey.slice(prefix.length));
721
+ Record.set(internal._expanded, pendingKey, true);
722
+ internal._onExpand?.(id, relation2);
723
+ }
724
+ }
725
+ });
726
+ if (nodes) {
727
+ addNodesImpl(graph, nodes);
728
+ const _edges = nodes.map((node) => ({
729
+ source: id,
730
+ target: node.id,
731
+ relation: "child"
732
+ }));
733
+ addEdgesImpl(graph, _edges);
734
+ }
735
+ if (edges) {
736
+ todo();
737
+ }
738
+ return graph;
739
+ };
740
+ function addNode(graphOrNodeArg, nodeArg) {
741
+ if (nodeArg === void 0) {
742
+ const nodeArg2 = graphOrNodeArg;
743
+ return (graph) => addNodeImpl(graph, nodeArg2);
744
+ } else {
745
+ const graph = graphOrNodeArg;
746
+ return addNodeImpl(graph, nodeArg);
747
+ }
748
+ }
749
+ var removeNodesImpl = (graph, ids, edges = false) => {
750
+ Atom2.batch(() => {
751
+ ids.map((id) => removeNodeImpl(graph, id, edges));
752
+ });
753
+ return graph;
754
+ };
755
+ function removeNodes(graphOrIds, idsOrEdges, edges) {
756
+ if (Array.isArray(graphOrIds)) {
757
+ const ids = graphOrIds;
758
+ const edgesArg = typeof idsOrEdges === "boolean" ? idsOrEdges : false;
759
+ return (graph) => removeNodesImpl(graph, ids, edgesArg);
760
+ } else {
761
+ const graph = graphOrIds;
762
+ const ids = idsOrEdges;
763
+ const edgesArg = edges ?? false;
764
+ return removeNodesImpl(graph, ids, edgesArg);
765
+ }
766
+ }
767
+ var removeNodeImpl = (graph, id, edges = false) => {
768
+ const internal = getInternal(graph);
769
+ const nodeAtom = internal._node(id);
770
+ internal._registry.set(nodeAtom, Option.none());
771
+ graph.onNodeChanged.emit({
772
+ id,
773
+ node: Option.none()
774
+ });
775
+ if (edges) {
776
+ const nodeEdges = internal._registry.get(internal._edges(id));
777
+ const edgesToRemove = [];
778
+ for (const [relationKeyValue, relatedIds] of Object.entries(nodeEdges)) {
779
+ const relation2 = relationFromKey(relationKeyValue);
780
+ const isInboundRelation = relation2.direction === "inbound";
781
+ for (const relatedId of relatedIds) {
782
+ if (isInboundRelation) {
783
+ edgesToRemove.push({
784
+ source: relatedId,
785
+ target: id,
786
+ relation: inverseRelation(relation2)
787
+ });
788
+ } else {
789
+ edgesToRemove.push({
790
+ source: id,
791
+ target: relatedId,
792
+ relation: relation2
793
+ });
794
+ }
795
+ }
796
+ }
797
+ removeEdgesImpl(graph, edgesToRemove);
798
+ }
799
+ internal._onRemoveNode?.(id);
800
+ return graph;
801
+ };
802
+ function removeNode(graphOrId, idOrEdges, edges) {
803
+ if (typeof graphOrId === "string") {
804
+ const id = graphOrId;
805
+ const edgesArg = typeof idOrEdges === "boolean" ? idOrEdges : false;
806
+ return (graph) => removeNodeImpl(graph, id, edgesArg);
807
+ } else {
808
+ const graph = graphOrId;
809
+ const id = idOrEdges;
810
+ const edgesArg = edges ?? false;
811
+ return removeNodeImpl(graph, id, edgesArg);
812
+ }
813
+ }
814
+ var addEdgesImpl = (graph, edges) => {
815
+ Atom2.batch(() => {
816
+ edges.map((edge) => addEdgeImpl(graph, edge));
817
+ });
818
+ return graph;
819
+ };
820
+ function addEdges(graphOrEdges, edges) {
821
+ if (edges === void 0) {
822
+ const edges2 = graphOrEdges;
823
+ return (graph) => addEdgesImpl(graph, edges2);
824
+ } else {
825
+ const graph = graphOrEdges;
826
+ return addEdgesImpl(graph, edges);
827
+ }
828
+ }
829
+ var addEdgeImpl = (graph, edgeArg) => {
830
+ const relation2 = normalizeRelation(edgeArg.relation);
831
+ const relationId = relationKey(relation2);
832
+ const inverse = inverseRelation(relation2);
833
+ const inverseId = relationKey(inverse);
834
+ const internal = getInternal(graph);
835
+ const sourceAtom = internal._edges(edgeArg.source);
836
+ const source = internal._registry.get(sourceAtom);
837
+ const sourceList = source[relationId] ?? [];
838
+ if (!sourceList.includes(edgeArg.target)) {
839
+ log("add edge", {
840
+ source: edgeArg.source,
841
+ target: edgeArg.target,
842
+ relation: relationId
843
+ }, {
844
+ F: __dxlog_file2,
845
+ L: 1081,
846
+ S: void 0,
847
+ C: (f, a) => f(...a)
195
848
  });
196
- if (nodes) {
197
- this.addNodes(nodes);
198
- const _edges = nodes.map((node2) => ({
199
- source: id,
200
- target: node2.id
201
- }));
202
- this.addEdges(_edges);
849
+ internal._registry.set(sourceAtom, {
850
+ ...source,
851
+ [relationId]: [
852
+ ...sourceList,
853
+ edgeArg.target
854
+ ]
855
+ });
856
+ }
857
+ const targetAtom = internal._edges(edgeArg.target);
858
+ const target = internal._registry.get(targetAtom);
859
+ const targetList = target[inverseId] ?? [];
860
+ if (!targetList.includes(edgeArg.source)) {
861
+ log("add inverse edge", {
862
+ source: edgeArg.source,
863
+ target: edgeArg.target,
864
+ relation: inverseId
865
+ }, {
866
+ F: __dxlog_file2,
867
+ L: 1089,
868
+ S: void 0,
869
+ C: (f, a) => f(...a)
870
+ });
871
+ internal._registry.set(targetAtom, {
872
+ ...target,
873
+ [inverseId]: [
874
+ ...targetList,
875
+ edgeArg.source
876
+ ]
877
+ });
878
+ }
879
+ return graph;
880
+ };
881
+ function addEdge(graphOrEdgeArg, edgeArg) {
882
+ if (edgeArg === void 0) {
883
+ const edgeArg2 = graphOrEdgeArg;
884
+ return (graph) => addEdgeImpl(graph, edgeArg2);
885
+ } else {
886
+ const graph = graphOrEdgeArg;
887
+ return addEdgeImpl(graph, edgeArg);
888
+ }
889
+ }
890
+ var removeEdgesImpl = (graph, edges, removeOrphans = false) => {
891
+ Atom2.batch(() => {
892
+ edges.map((edge) => removeEdgeImpl(graph, edge, removeOrphans));
893
+ });
894
+ return graph;
895
+ };
896
+ function removeEdges(graphOrEdges, edgesOrRemoveOrphans, removeOrphans) {
897
+ if (Array.isArray(graphOrEdges)) {
898
+ const edges = graphOrEdges;
899
+ const removeOrphansArg = typeof edgesOrRemoveOrphans === "boolean" ? edgesOrRemoveOrphans : false;
900
+ return (graph) => removeEdgesImpl(graph, edges, removeOrphansArg);
901
+ } else {
902
+ const graph = graphOrEdges;
903
+ const edges = edgesOrRemoveOrphans;
904
+ const removeOrphansArg = removeOrphans ?? false;
905
+ return removeEdgesImpl(graph, edges, removeOrphansArg);
906
+ }
907
+ }
908
+ var removeEdgeImpl = (graph, edgeArg, removeOrphans = false) => {
909
+ const relation2 = normalizeRelation(edgeArg.relation);
910
+ const relationId = relationKey(relation2);
911
+ const inverse = inverseRelation(relation2);
912
+ const inverseId = relationKey(inverse);
913
+ const internal = getInternal(graph);
914
+ const sourceAtom = internal._edges(edgeArg.source);
915
+ const source = internal._registry.get(sourceAtom);
916
+ const sourceList = source[relationId] ?? [];
917
+ if (sourceList.includes(edgeArg.target)) {
918
+ internal._registry.set(sourceAtom, {
919
+ ...source,
920
+ [relationId]: sourceList.filter((id) => id !== edgeArg.target)
921
+ });
922
+ }
923
+ const targetAtom = internal._edges(edgeArg.target);
924
+ const target = internal._registry.get(targetAtom);
925
+ const targetList = target[inverseId] ?? [];
926
+ if (targetList.includes(edgeArg.source)) {
927
+ internal._registry.set(targetAtom, {
928
+ ...target,
929
+ [inverseId]: targetList.filter((id) => id !== edgeArg.source)
930
+ });
931
+ }
932
+ if (removeOrphans) {
933
+ const sourceAfter = internal._registry.get(sourceAtom);
934
+ const targetAfter = internal._registry.get(targetAtom);
935
+ const isEmpty = (edges) => Object.values(edges).every((ids) => ids.length === 0);
936
+ if (isEmpty(sourceAfter) && edgeArg.source !== RootId) {
937
+ removeNodesImpl(graph, [
938
+ edgeArg.source
939
+ ]);
203
940
  }
204
- if (edges) {
205
- todo();
941
+ if (isEmpty(targetAfter) && edgeArg.target !== RootId) {
942
+ removeNodesImpl(graph, [
943
+ edgeArg.target
944
+ ]);
945
+ }
946
+ }
947
+ return graph;
948
+ };
949
+ function removeEdge(graphOrEdgeArg, edgeArgOrRemoveOrphans, removeOrphans) {
950
+ if (edgeArgOrRemoveOrphans === void 0 || typeof edgeArgOrRemoveOrphans === "boolean" || "source" in graphOrEdgeArg) {
951
+ const edgeArg = graphOrEdgeArg;
952
+ const removeOrphansArg = typeof edgeArgOrRemoveOrphans === "boolean" ? edgeArgOrRemoveOrphans : false;
953
+ return (graph) => removeEdgeImpl(graph, edgeArg, removeOrphansArg);
954
+ } else {
955
+ const graph = graphOrEdgeArg;
956
+ const edgeArg = edgeArgOrRemoveOrphans;
957
+ const removeOrphansArg = removeOrphans ?? false;
958
+ return removeEdgeImpl(graph, edgeArg, removeOrphansArg);
959
+ }
960
+ }
961
+ var make = (params) => {
962
+ return new GraphImpl(params);
963
+ };
964
+ var relationKey = (relation2) => {
965
+ const normalized = normalizeRelation(relation2);
966
+ return `${normalized.kind}${Separators.secondary}${normalized.direction}`;
967
+ };
968
+ var relationFromKey = (encoded) => {
969
+ const separatorIndex = encoded.lastIndexOf(Separators.secondary);
970
+ invariant2(separatorIndex > 0 && separatorIndex < encoded.length - 1, `Invalid relation key: ${encoded}`, {
971
+ F: __dxlog_file2,
972
+ L: 1234,
973
+ S: void 0,
974
+ A: [
975
+ "separatorIndex > 0 && separatorIndex < encoded.length - 1",
976
+ "`Invalid relation key: ${encoded}`"
977
+ ]
978
+ });
979
+ const kind = encoded.slice(0, separatorIndex);
980
+ const directionRaw = encoded.slice(separatorIndex + 1);
981
+ invariant2(directionRaw === "outbound" || directionRaw === "inbound", `Invalid relation direction: ${directionRaw}`, {
982
+ F: __dxlog_file2,
983
+ L: 1237,
984
+ S: void 0,
985
+ A: [
986
+ "directionRaw === 'outbound' || directionRaw === 'inbound'",
987
+ "`Invalid relation direction: ${directionRaw}`"
988
+ ]
989
+ });
990
+ return relation(kind, directionRaw);
991
+ };
992
+ var connectionKey = (id, relation2) => `${id}${Separators.primary}${relationKey(relation2)}`;
993
+ var relationFromConnectionKey = (key) => {
994
+ const separatorIndex = key.indexOf(Separators.primary);
995
+ invariant2(separatorIndex > 0 && separatorIndex < key.length - 1, `Invalid connection key: ${key}`, {
996
+ F: __dxlog_file2,
997
+ L: 1246,
998
+ S: void 0,
999
+ A: [
1000
+ "separatorIndex > 0 && separatorIndex < key.length - 1",
1001
+ "`Invalid connection key: ${key}`"
1002
+ ]
1003
+ });
1004
+ const id = key.slice(0, separatorIndex);
1005
+ const encodedRelation = key.slice(separatorIndex + 1);
1006
+ return {
1007
+ id,
1008
+ relation: relationFromKey(encodedRelation)
1009
+ };
1010
+ };
1011
+ var inverseRelation = (relation2) => {
1012
+ const normalized = normalizeRelation(relation2);
1013
+ return relation(normalized.kind, normalized.direction === "outbound" ? "inbound" : "outbound");
1014
+ };
1015
+
1016
+ // src/graph-builder.ts
1017
+ var graph_builder_exports = {};
1018
+ __export(graph_builder_exports, {
1019
+ GraphBuilderTypeId: () => GraphBuilderTypeId,
1020
+ addExtension: () => addExtension,
1021
+ createConnector: () => createConnector,
1022
+ createExtension: () => createExtension,
1023
+ createExtensionRaw: () => createExtensionRaw,
1024
+ createTypeExtension: () => createTypeExtension,
1025
+ destroy: () => destroy,
1026
+ explore: () => explore,
1027
+ flattenExtensions: () => flattenExtensions,
1028
+ flush: () => flush,
1029
+ from: () => from,
1030
+ make: () => make2,
1031
+ removeExtension: () => removeExtension
1032
+ });
1033
+ import { Atom as Atom3, Registry as Registry2 } from "@effect-atom/atom-react";
1034
+ import * as Array2 from "effect/Array";
1035
+ import * as Effect from "effect/Effect";
1036
+ import * as Function2 from "effect/Function";
1037
+ import * as Option3 from "effect/Option";
1038
+ import * as Pipeable2 from "effect/Pipeable";
1039
+ import * as Record2 from "effect/Record";
1040
+ import { scheduleTask, yieldOrContinue } from "main-thread-scheduling";
1041
+ import { log as log2 } from "@dxos/log";
1042
+ import { byPosition, getDebugName, isNonNullable as isNonNullable2 } from "@dxos/util";
1043
+
1044
+ // src/node-matcher.ts
1045
+ var node_matcher_exports = {};
1046
+ __export(node_matcher_exports, {
1047
+ whenAll: () => whenAll,
1048
+ whenAny: () => whenAny,
1049
+ whenEchoObject: () => whenEchoObject,
1050
+ whenEchoObjectMatches: () => whenEchoObjectMatches,
1051
+ whenEchoType: () => whenEchoType,
1052
+ whenEchoTypeMatches: () => whenEchoTypeMatches,
1053
+ whenId: () => whenId,
1054
+ whenNodeType: () => whenNodeType,
1055
+ whenNot: () => whenNot,
1056
+ whenRoot: () => whenRoot
1057
+ });
1058
+ import * as Option2 from "effect/Option";
1059
+ import { Obj } from "@dxos/echo";
1060
+ var whenRoot = (node) => node.id === RootId ? Option2.some(node) : Option2.none();
1061
+ var whenId = (id) => (node) => node.id === id ? Option2.some(node) : Option2.none();
1062
+ var whenNodeType = (type) => (node) => node.type === type ? Option2.some(node) : Option2.none();
1063
+ var whenEchoType = (type) => (node) => Obj.instanceOf(type, node.data) ? Option2.some(node.data) : Option2.none();
1064
+ var whenEchoObject = (node) => Obj.isObject(node.data) ? Option2.some(node.data) : Option2.none();
1065
+ var whenAll = (...matchers) => (node) => {
1066
+ for (const candidate of matchers) {
1067
+ if (Option2.isNone(candidate(node))) {
1068
+ return Option2.none();
206
1069
  }
207
1070
  }
208
- removeNodes(ids, edges = false) {
209
- Rx.batch(() => {
210
- ids.map((id) => this.removeNode(id, edges));
1071
+ return Option2.some(node);
1072
+ };
1073
+ var whenAny = (...matchers) => (node) => {
1074
+ for (const candidate of matchers) {
1075
+ if (Option2.isSome(candidate(node))) {
1076
+ return Option2.some(node);
1077
+ }
1078
+ }
1079
+ return Option2.none();
1080
+ };
1081
+ var whenEchoTypeMatches = (type) => (node) => Obj.instanceOf(type, node.data) ? Option2.some(node) : Option2.none();
1082
+ var whenEchoObjectMatches = (node) => Obj.isObject(node.data) ? Option2.some(node) : Option2.none();
1083
+ var whenNot = (matcher) => (node) => Option2.isNone(matcher(node)) ? Option2.some(node) : Option2.none();
1084
+
1085
+ // src/graph-builder.ts
1086
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/sdk/app-graph/src/graph-builder.ts";
1087
+ var GraphBuilderTypeId = /* @__PURE__ */ Symbol.for("@dxos/app-graph/GraphBuilder");
1088
+ var GraphBuilderImpl = class {
1089
+ [GraphBuilderTypeId] = GraphBuilderTypeId;
1090
+ pipe() {
1091
+ return Pipeable2.pipeArguments(this, arguments);
1092
+ }
1093
+ // TODO(wittjosiah): Use Context.
1094
+ /** Active subscriptions keyed by composite ID, cleaned up on node removal. */
1095
+ _subscriptions = /* @__PURE__ */ new Map();
1096
+ /** Connector updates pending flush, keyed by connector key. */
1097
+ _dirtyConnectors = /* @__PURE__ */ new Map();
1098
+ /** Last-flushed node IDs per connector key, used for edge removal on update. */
1099
+ _connectorPrevious = /* @__PURE__ */ new Map();
1100
+ /** Last-flushed node args per connector key, used for change detection. */
1101
+ _connectorPreviousArgs = /* @__PURE__ */ new Map();
1102
+ /** Whether a dirty-flush task is already scheduled. */
1103
+ _flushScheduled = false;
1104
+ /** Resolves when the current flush completes. */
1105
+ _flushPromise = Promise.resolve();
1106
+ /** Registered builder extensions keyed by extension ID. */
1107
+ _extensions = Atom3.make(Record2.empty()).pipe(Atom3.keepAlive, Atom3.withLabel("graph-builder:extensions"));
1108
+ /** Triggers signalling that a node's resolver has fired at least once. */
1109
+ _initialized = {};
1110
+ /** Shared atom registry for reactive subscriptions. */
1111
+ _registry;
1112
+ /** Backing graph with internal accessors for node atoms and construction. */
1113
+ _graph;
1114
+ constructor({ registry, ...params } = {}) {
1115
+ this._registry = registry ?? Registry2.make();
1116
+ const graph = make({
1117
+ ...params,
1118
+ registry: this._registry,
1119
+ onExpand: (id, relation2) => this._onExpand(id, relation2),
1120
+ onInitialize: (id) => this._onInitialize(id),
1121
+ onRemoveNode: (id) => this._onRemoveNode(id)
211
1122
  });
1123
+ this._graph = graph;
1124
+ }
1125
+ get graph() {
1126
+ return this._graph;
1127
+ }
1128
+ get extensions() {
1129
+ return this._extensions;
1130
+ }
1131
+ /** Apply a set of node changes for a single connector key. */
1132
+ _applyConnectorUpdate(key, nodes, previous) {
1133
+ const { id, relation: relation2 } = relationFromConnectorKey(key);
1134
+ const ids = nodes.map((node) => node.id);
1135
+ const removed = previous.filter((pid) => !ids.includes(pid));
1136
+ this._connectorPrevious.set(key, ids);
1137
+ this._connectorPreviousArgs.set(key, nodes);
1138
+ removeEdges(this._graph, removed.map((target) => ({
1139
+ source: id,
1140
+ target,
1141
+ relation: relation2
1142
+ })), true);
1143
+ addNodes(this._graph, nodes);
1144
+ addEdges(this._graph, nodes.map((node) => ({
1145
+ source: id,
1146
+ target: node.id,
1147
+ relation: relation2
1148
+ })));
1149
+ if (ids.length > 0) {
1150
+ const sortedIds = [
1151
+ ...nodes
1152
+ ].sort((a, b) => byPosition(a.properties ?? {}, b.properties ?? {})).map((n) => n.id);
1153
+ sortEdges(this._graph, id, relation2, sortedIds);
1154
+ }
1155
+ }
1156
+ _scheduleDirtyFlush() {
1157
+ if (!this._flushScheduled) {
1158
+ this._flushScheduled = true;
1159
+ this._flushPromise = scheduleTask(() => {
1160
+ this._flushScheduled = false;
1161
+ while (this._dirtyConnectors.size > 0) {
1162
+ const entries = [
1163
+ ...this._dirtyConnectors.entries()
1164
+ ];
1165
+ this._dirtyConnectors.clear();
1166
+ Atom3.batch(() => {
1167
+ for (const [key, { nodes, previous }] of entries) {
1168
+ this._applyConnectorUpdate(key, nodes, previous);
1169
+ }
1170
+ });
1171
+ }
1172
+ }, {
1173
+ strategy: "smooth"
1174
+ });
1175
+ }
212
1176
  }
213
- removeNode(id, edges = false) {
214
- const nodeRx = this._node(id);
215
- this._registry.set(nodeRx, Option.none());
216
- this.onNodeChanged.emit({
1177
+ _resolvers = Atom3.family((id) => {
1178
+ return Atom3.make((get2) => {
1179
+ return Function2.pipe(get2(this._extensions), Record2.values, Array2.sortBy(byPosition), Array2.map(({ resolver }) => resolver), Array2.filter(isNonNullable2), Array2.map((resolver) => get2(resolver(id))), Array2.filter(isNonNullable2), Array2.head);
1180
+ });
1181
+ });
1182
+ _connectors = Atom3.family((key) => {
1183
+ return Atom3.make((get2) => {
1184
+ const { id, relation: relation2 } = relationFromConnectorKey(key);
1185
+ const node = this._graph.node(id);
1186
+ const sourceNode = Option3.getOrElse(get2(node), () => void 0);
1187
+ if (!sourceNode) {
1188
+ return [];
1189
+ }
1190
+ const extensions = Function2.pipe(get2(this._extensions), Record2.values, Array2.sortBy(byPosition), Array2.filter((ext) => relationKey(ext.relation ?? "child") === relationKey(relation2) && ext.connector != null));
1191
+ const nodes = [];
1192
+ for (const ext of extensions) {
1193
+ const result = get2(ext.connector(node));
1194
+ nodes.push(...result);
1195
+ }
1196
+ return nodes;
1197
+ }).pipe(Atom3.withLabel(`graph-builder:connectors:${key}`));
1198
+ });
1199
+ _onExpand(id, relation2) {
1200
+ log2("onExpand", {
217
1201
  id,
218
- node: Option.none()
1202
+ relation: relation2,
1203
+ registry: getDebugName(this._registry)
1204
+ }, {
1205
+ F: __dxlog_file3,
1206
+ L: 261,
1207
+ S: this,
1208
+ C: (f, a) => f(...a)
219
1209
  });
220
- if (edges) {
221
- const { inbound, outbound } = this._registry.get(this._edges(id));
222
- const edges2 = [
223
- ...inbound.map((source) => ({
224
- source,
225
- target: id
226
- })),
227
- ...outbound.map((target) => ({
228
- source: id,
229
- target
230
- }))
231
- ];
232
- this.removeEdges(edges2);
1210
+ this._expandRelation(id, relation2);
1211
+ if (relation2.kind === "child" && relation2.direction === "outbound") {
1212
+ expand(this._graph, id, "action");
233
1213
  }
234
- this._onRemoveNode?.(id);
235
- }
236
- addEdges(edges) {
237
- Rx.batch(() => {
238
- edges.map((edge) => this.addEdge(edge));
239
- });
240
1214
  }
241
- addEdge(edgeArg) {
242
- const sourceRx = this._edges(edgeArg.source);
243
- const source = this._registry.get(sourceRx);
244
- if (!source.outbound.includes(edgeArg.target)) {
245
- log("add outbound edge", {
246
- source: edgeArg.source,
247
- target: edgeArg.target
248
- }, {
249
- F: __dxlog_file,
250
- L: 482,
251
- S: this,
252
- C: (f, a) => f(...a)
253
- });
254
- this._registry.set(sourceRx, {
255
- inbound: source.inbound,
256
- outbound: [
257
- ...source.outbound,
258
- edgeArg.target
259
- ]
260
- });
261
- }
262
- const targetRx = this._edges(edgeArg.target);
263
- const target = this._registry.get(targetRx);
264
- if (!target.inbound.includes(edgeArg.source)) {
265
- log("add inbound edge", {
266
- source: edgeArg.source,
267
- target: edgeArg.target
1215
+ _expandRelation(id, relation2) {
1216
+ const key = connectorKey(id, relation2);
1217
+ const connectors = this._connectors(key);
1218
+ const cancel = this._registry.subscribe(connectors, (rawNodes) => {
1219
+ const nodes = qualifyNodeArgs(id)(rawNodes);
1220
+ const previous = this._connectorPrevious.get(key) ?? [];
1221
+ const ids = nodes.map((n) => n.id);
1222
+ if (ids.length === previous.length && ids.every((nodeId, idx) => nodeId === previous[idx])) {
1223
+ const prevArgs = this._connectorPreviousArgs.get(key);
1224
+ if (prevArgs && nodeArgsUnchanged(prevArgs, nodes)) {
1225
+ return;
1226
+ }
1227
+ }
1228
+ log2("update", {
1229
+ id,
1230
+ relation: relation2,
1231
+ ids
268
1232
  }, {
269
- F: __dxlog_file,
270
- L: 489,
1233
+ F: __dxlog_file3,
1234
+ L: 288,
271
1235
  S: this,
272
1236
  C: (f, a) => f(...a)
273
1237
  });
274
- this._registry.set(targetRx, {
275
- inbound: [
276
- ...target.inbound,
277
- edgeArg.source
278
- ],
279
- outbound: target.outbound
1238
+ this._dirtyConnectors.set(key, {
1239
+ nodes,
1240
+ previous
280
1241
  });
281
- }
282
- }
283
- removeEdges(edges, removeOrphans = false) {
284
- Rx.batch(() => {
285
- edges.map((edge) => this.removeEdge(edge, removeOrphans));
1242
+ this._scheduleDirtyFlush();
1243
+ }, {
1244
+ immediate: true
286
1245
  });
1246
+ this._subscriptions.set(subscriptionKey(id, "expand", key), cancel);
287
1247
  }
288
- removeEdge(edgeArg, removeOrphans = false) {
289
- const sourceRx = this._edges(edgeArg.source);
290
- const source = this._registry.get(sourceRx);
291
- if (source.outbound.includes(edgeArg.target)) {
292
- this._registry.set(sourceRx, {
293
- inbound: source.inbound,
294
- outbound: source.outbound.filter((id) => id !== edgeArg.target)
295
- });
296
- }
297
- const targetRx = this._edges(edgeArg.target);
298
- const target = this._registry.get(targetRx);
299
- if (target.inbound.includes(edgeArg.source)) {
300
- this._registry.set(targetRx, {
301
- inbound: target.inbound.filter((id) => id !== edgeArg.source),
302
- outbound: target.outbound
1248
+ // TODO(wittjosiah): If the same node is added by a connector, the resolver should probably cancel itself?
1249
+ async _onInitialize(id) {
1250
+ log2("onInitialize", {
1251
+ id
1252
+ }, {
1253
+ F: __dxlog_file3,
1254
+ L: 300,
1255
+ S: this,
1256
+ C: (f, a) => f(...a)
1257
+ });
1258
+ const resolver = this._resolvers(id);
1259
+ const cancel = this._registry.subscribe(resolver, (node) => {
1260
+ const trigger = this._initialized[id];
1261
+ Option3.match(node, {
1262
+ onSome: (node2) => {
1263
+ const connectorOwned = [
1264
+ ...this._connectorPrevious.values()
1265
+ ].some((ids) => ids.includes(id));
1266
+ if (!connectorOwned) {
1267
+ addNodes(this._graph, [
1268
+ node2
1269
+ ]);
1270
+ }
1271
+ trigger?.wake();
1272
+ },
1273
+ onNone: () => {
1274
+ trigger?.wake();
1275
+ removeNodes(this._graph, [
1276
+ id
1277
+ ]);
1278
+ }
303
1279
  });
304
- }
305
- if (removeOrphans) {
306
- const source2 = this._registry.get(sourceRx);
307
- const target2 = this._registry.get(targetRx);
308
- if (source2.outbound.length === 0 && source2.inbound.length === 0 && edgeArg.source !== ROOT_ID) {
309
- this.removeNodes([
310
- edgeArg.source
311
- ]);
312
- }
313
- if (target2.outbound.length === 0 && target2.inbound.length === 0 && edgeArg.target !== ROOT_ID) {
314
- this.removeNodes([
315
- edgeArg.target
316
- ]);
1280
+ }, {
1281
+ immediate: true
1282
+ });
1283
+ this._subscriptions.set(subscriptionKey(id, "init"), cancel);
1284
+ }
1285
+ _onRemoveNode(id) {
1286
+ const prefix = `${id}${Separators.primary}`;
1287
+ for (const [key, cleanup] of this._subscriptions) {
1288
+ if (key.startsWith(prefix)) {
1289
+ cleanup();
1290
+ this._subscriptions.delete(key);
317
1291
  }
318
1292
  }
319
1293
  }
320
- sortEdges(id, relation, order) {
321
- const edgesRx = this._edges(id);
322
- const edges = this._registry.get(edgesRx);
323
- const unsorted = edges[relation].filter((id2) => !order.includes(id2)) ?? [];
324
- const sorted = order.filter((id2) => edges[relation].includes(id2)) ?? [];
325
- edges[relation].splice(0, edges[relation].length, ...[
326
- ...sorted,
327
- ...unsorted
328
- ]);
329
- this._registry.set(edgesRx, edges);
1294
+ };
1295
+ var make2 = (params) => {
1296
+ return new GraphBuilderImpl(params);
1297
+ };
1298
+ var from = (pickle, registry) => {
1299
+ if (!pickle) {
1300
+ return make2({
1301
+ registry
1302
+ });
330
1303
  }
331
- traverse({ visitor, source = ROOT_ID, relation = "outbound" }, path = []) {
332
- if (path.includes(source)) {
333
- return;
334
- }
335
- const node = this.getNodeOrThrow(source);
336
- const shouldContinue = visitor(node, [
337
- ...path,
338
- source
339
- ]);
340
- if (shouldContinue === false) {
341
- return;
342
- }
343
- Object.values(this.getConnections(source, relation)).forEach((child) => this.traverse({
344
- source: child.id,
345
- relation,
1304
+ const { nodes, edges } = JSON.parse(pickle);
1305
+ return make2({
1306
+ nodes,
1307
+ edges,
1308
+ registry
1309
+ });
1310
+ };
1311
+ var addExtensionImpl = (builder, extensions) => {
1312
+ const internal = builder;
1313
+ flattenExtensions(extensions).forEach((extension) => {
1314
+ const extensions2 = internal._registry.get(internal._extensions);
1315
+ internal._registry.set(internal._extensions, Record2.set(extensions2, extension.id, extension));
1316
+ });
1317
+ return builder;
1318
+ };
1319
+ function addExtension(builderOrExtensions, extensions) {
1320
+ if (extensions === void 0) {
1321
+ const extensions2 = builderOrExtensions;
1322
+ return (builder) => addExtensionImpl(builder, extensions2);
1323
+ } else {
1324
+ const builder = builderOrExtensions;
1325
+ return addExtensionImpl(builder, extensions);
1326
+ }
1327
+ }
1328
+ var removeExtensionImpl = (builder, id) => {
1329
+ const internal = builder;
1330
+ const extensions = internal._registry.get(internal._extensions);
1331
+ internal._registry.set(internal._extensions, Record2.remove(extensions, id));
1332
+ return builder;
1333
+ };
1334
+ function removeExtension(builderOrId, id) {
1335
+ if (typeof builderOrId === "string") {
1336
+ const id2 = builderOrId;
1337
+ return (builder) => removeExtensionImpl(builder, id2);
1338
+ } else {
1339
+ const builder = builderOrId;
1340
+ return removeExtensionImpl(builder, id);
1341
+ }
1342
+ }
1343
+ var exploreImpl = async (builder, options, path = []) => {
1344
+ const internal = builder;
1345
+ const { registry = Registry2.make(), source = RootId, relation: relation2, visitor } = options;
1346
+ if (path.includes(source)) {
1347
+ return;
1348
+ }
1349
+ await yieldOrContinue("idle");
1350
+ const node = registry.get(internal._graph.nodeOrThrow(source));
1351
+ const shouldContinue = await visitor(node, [
1352
+ ...path,
1353
+ node.id
1354
+ ]);
1355
+ if (shouldContinue === false) {
1356
+ return;
1357
+ }
1358
+ const nodes = Function2.pipe(internal._registry.get(internal._extensions), Record2.values, Array2.map((extension) => extension.connector), Array2.filter(isNonNullable2), Array2.flatMap((connector) => registry.get(connector(internal._graph.node(source)))), qualifyNodeArgs(source));
1359
+ await Promise.all(nodes.map((nodeArg) => {
1360
+ registry.set(internal._graph._node(nodeArg.id), internal._graph._constructNode(nodeArg));
1361
+ return exploreImpl(builder, {
1362
+ registry,
1363
+ source: nodeArg.id,
1364
+ relation: relation2,
346
1365
  visitor
347
1366
  }, [
348
1367
  ...path,
349
- source
350
- ]));
351
- }
352
- getPath({ source = "root", target }) {
353
- return Function.pipe(this.getNode(source), Option.flatMap((node) => {
354
- let found = Option.none();
355
- this.traverse({
356
- source: node.id,
357
- visitor: (node2, path) => {
358
- if (Option.isSome(found)) {
359
- return false;
360
- }
361
- if (node2.id === target) {
362
- found = Option.some(path);
363
- }
364
- }
365
- });
366
- return found;
367
- }));
368
- }
369
- async waitForPath(params, { timeout = 5e3, interval = 500 } = {}) {
370
- const path = this.getPath(params);
371
- if (Option.isSome(path)) {
372
- return path.value;
373
- }
374
- const trigger = new Trigger();
375
- const i = setInterval(() => {
376
- const path2 = this.getPath(params);
377
- if (Option.isSome(path2)) {
378
- trigger.wake(path2.value);
379
- }
380
- }, interval);
381
- return trigger.wait({
382
- timeout
383
- }).finally(() => clearInterval(i));
384
- }
385
- /** @internal */
386
- _constructNode(node) {
387
- return Option.some({
388
- [graphSymbol]: this,
389
- data: null,
390
- properties: {},
391
- ...node
392
- });
1368
+ node.id
1369
+ ]);
1370
+ }));
1371
+ if (registry !== internal._registry) {
1372
+ registry.reset();
1373
+ registry.dispose();
393
1374
  }
394
- constructor({ registry, nodes, edges, onInitialize, onExpand, onRemoveNode } = {}) {
395
- _define_property(this, "onNodeChanged", new Event());
396
- _define_property(this, "_onExpand", void 0);
397
- _define_property(this, "_onInitialize", void 0);
398
- _define_property(this, "_onRemoveNode", void 0);
399
- _define_property(this, "_registry", void 0);
400
- _define_property(this, "_expanded", Record.empty());
401
- _define_property(this, "_initialized", Record.empty());
402
- _define_property(this, "_initialEdges", Record.empty());
403
- _define_property(this, "_initialNodes", Record.fromEntries([
404
- [
405
- ROOT_ID,
406
- this._constructNode({
407
- id: ROOT_ID,
408
- type: ROOT_TYPE,
409
- data: null,
410
- properties: {}
411
- })
412
- ]
413
- ]));
414
- _define_property(this, "_node", Rx.family((id) => {
415
- const initial = Option.flatten(Record.get(this._initialNodes, id));
416
- return Rx.make(initial).pipe(Rx.keepAlive, Rx.withLabel(`graph:node:${id}`));
417
- }));
418
- _define_property(this, "_nodeOrThrow", Rx.family((id) => {
419
- return Rx.make((get2) => {
420
- const node = get2(this._node(id));
421
- invariant(Option.isSome(node), `Node not available: ${id}`, {
422
- F: __dxlog_file,
423
- L: 254,
424
- S: this,
425
- A: [
426
- "Option.isSome(node)",
427
- "`Node not available: ${id}`"
428
- ]
429
- });
430
- return node.value;
431
- });
432
- }));
433
- _define_property(this, "_edges", Rx.family((id) => {
434
- const initial = Record.get(this._initialEdges, id).pipe(Option.getOrElse(() => ({
435
- inbound: [],
436
- outbound: []
437
- })));
438
- return Rx.make(initial).pipe(Rx.keepAlive, Rx.withLabel(`graph:edges:${id}`));
439
- }));
440
- _define_property(this, "_connections", Rx.family((key) => {
441
- return Rx.make((get2) => {
442
- const [id, relation] = key.split("$");
443
- const edges2 = get2(this._edges(id));
444
- return edges2[relation].map((id2) => get2(this._node(id2))).filter(Option.isSome).map((o) => o.value);
445
- }).pipe(Rx.withLabel(`graph:connections:${key}`));
446
- }));
447
- _define_property(this, "_actions", Rx.family((id) => {
448
- return Rx.make((get2) => {
449
- return get2(this._connections(`${id}$outbound`)).filter((node) => node.type === ACTION_TYPE || node.type === ACTION_GROUP_TYPE);
450
- }).pipe(Rx.withLabel(`graph:actions:${id}`));
451
- }));
452
- _define_property(this, "_json", Rx.family((id) => {
453
- return Rx.make((get2) => {
454
- const toJSON = (node, seen = []) => {
455
- const nodes2 = get2(this.connections(node.id));
456
- const obj = {
457
- id: node.id,
458
- type: node.type
459
- };
460
- if (node.properties.label) {
461
- obj.label = node.properties.label;
462
- }
463
- if (nodes2.length) {
464
- obj.nodes = nodes2.map((n) => {
465
- const nextSeen = [
466
- ...seen,
467
- node.id
468
- ];
469
- return nextSeen.includes(n.id) ? void 0 : toJSON(n, nextSeen);
470
- }).filter(isNonNullable);
471
- }
472
- return obj;
473
- };
474
- const root = get2(this.nodeOrThrow(id));
475
- return toJSON(root);
476
- }).pipe(Rx.withLabel(`graph:json:${id}`));
477
- }));
478
- this._registry = registry ?? Registry.make();
479
- this._onInitialize = onInitialize;
480
- this._onExpand = onExpand;
481
- this._onRemoveNode = onRemoveNode;
482
- if (nodes) {
483
- nodes.forEach((node) => {
484
- Record.set(this._initialNodes, node.id, this._constructNode(node));
485
- });
486
- }
487
- if (edges) {
488
- Object.entries(edges).forEach(([source, edges2]) => {
489
- Record.set(this._initialEdges, source, edges2);
490
- });
491
- }
1375
+ };
1376
+ function explore(builderOrOptions, optionsOrPath, path) {
1377
+ if (typeof builderOrOptions === "object" && "visitor" in builderOrOptions) {
1378
+ const options = builderOrOptions;
1379
+ const path2 = Array2.isArray(optionsOrPath) ? optionsOrPath : void 0;
1380
+ return (builder) => exploreImpl(builder, options, path2);
1381
+ } else {
1382
+ const builder = builderOrOptions;
1383
+ const options = optionsOrPath;
1384
+ const pathArg = path ?? (Array2.isArray(optionsOrPath) ? optionsOrPath : void 0);
1385
+ return exploreImpl(builder, options, pathArg);
492
1386
  }
1387
+ }
1388
+ var destroyImpl = (builder) => {
1389
+ const internal = builder;
1390
+ internal._subscriptions.forEach((unsubscribe) => unsubscribe());
1391
+ internal._subscriptions.clear();
493
1392
  };
494
-
495
- // src/graph-builder.ts
496
- import { Registry as Registry2, Rx as Rx2 } from "@effect-rx/rx-react";
497
- import { effect } from "@preact/signals-core";
498
- import * as Array from "effect/Array";
499
- import * as Function2 from "effect/Function";
500
- import * as Option2 from "effect/Option";
501
- import * as Record2 from "effect/Record";
502
- import { log as log2 } from "@dxos/log";
503
- import { byPosition, getDebugName, isNode, isNonNullable as isNonNullable2 } from "@dxos/util";
504
-
505
- // src/node.ts
506
- var isGraphNode = (data) => data && typeof data === "object" && "id" in data && "properties" in data && data.properties ? typeof data.properties === "object" && "data" in data : false;
507
- var isAction = (data) => isGraphNode(data) ? typeof data.data === "function" && data.type === ACTION_TYPE : false;
508
- var actionGroupSymbol = Symbol("ActionGroup");
509
- var isActionGroup = (data) => isGraphNode(data) ? data.data === actionGroupSymbol && data.type === ACTION_GROUP_TYPE : false;
510
- var isActionLike = (data) => isAction(data) || isActionGroup(data);
511
-
512
- // src/graph-builder.ts
513
- function _define_property2(obj, key, value) {
514
- if (key in obj) {
515
- Object.defineProperty(obj, key, {
516
- value,
517
- enumerable: true,
518
- configurable: true,
519
- writable: true
520
- });
1393
+ function destroy(builder) {
1394
+ if (builder === void 0) {
1395
+ return (builder2) => destroyImpl(builder2);
521
1396
  } else {
522
- obj[key] = value;
1397
+ return destroyImpl(builder);
523
1398
  }
524
- return obj;
525
1399
  }
526
- var __dxlog_file2 = "/__w/dxos/dxos/packages/sdk/app-graph/src/graph-builder.ts";
527
- var createExtension = (extension) => {
528
- const { id, position = "static", relation = "outbound", resolver: _resolver, connector: _connector, actions: _actions, actionGroups: _actionGroups } = extension;
1400
+ var flush = (builder) => {
1401
+ return builder._flushPromise;
1402
+ };
1403
+ var createExtensionRaw = (extension) => {
1404
+ const { id, position = "static", relation: relation2 = "child", resolver: _resolver, connector: _connector, actions: _actions, actionGroups: _actionGroups } = extension;
1405
+ const normalizedRelation = normalizeRelation(relation2);
529
1406
  const getId = (key) => `${id}/${key}`;
530
- const resolver = _resolver && Rx2.family((id2) => _resolver(id2).pipe(Rx2.withLabel(`graph-builder:_resolver:${id2}`)));
531
- const connector = _connector && Rx2.family((node) => _connector(node).pipe(Rx2.withLabel(`graph-builder:_connector:${id}`)));
532
- const actionGroups = _actionGroups && Rx2.family((node) => _actionGroups(node).pipe(Rx2.withLabel(`graph-builder:_actionGroups:${id}`)));
533
- const actions = _actions && Rx2.family((node) => _actions(node).pipe(Rx2.withLabel(`graph-builder:_actions:${id}`)));
1407
+ const resolver = _resolver && Atom3.family((id2) => _resolver(id2).pipe(Atom3.withLabel(`graph-builder:_resolver:${id2}`)));
1408
+ const connector = _connector && Atom3.family((node) => _connector(node).pipe(Atom3.withLabel(`graph-builder:_connector:${id}`)));
1409
+ const actionGroups = _actionGroups && Atom3.family((node) => _actionGroups(node).pipe(Atom3.withLabel(`graph-builder:_actionGroups:${id}`)));
1410
+ const actions = _actions && Atom3.family((node) => _actions(node).pipe(Atom3.withLabel(`graph-builder:_actions:${id}`)));
534
1411
  return [
535
1412
  resolver ? {
536
1413
  id: getId("resolver"),
@@ -540,77 +1417,148 @@ var createExtension = (extension) => {
540
1417
  connector ? {
541
1418
  id: getId("connector"),
542
1419
  position,
543
- relation,
544
- connector: Rx2.family((node) => Rx2.make((get2) => {
1420
+ relation: normalizedRelation,
1421
+ connector: Atom3.family((node) => Atom3.make((get2) => {
545
1422
  try {
546
1423
  return get2(connector(node));
547
- } catch {
1424
+ } catch (error) {
548
1425
  log2.warn("Error in connector", {
549
1426
  id: getId("connector"),
550
- node
1427
+ node,
1428
+ error
551
1429
  }, {
552
- F: __dxlog_file2,
553
- L: 112,
1430
+ F: __dxlog_file3,
1431
+ L: 596,
554
1432
  S: void 0,
555
1433
  C: (f, a) => f(...a)
556
1434
  });
557
1435
  return [];
558
1436
  }
559
- }).pipe(Rx2.withLabel(`graph-builder:connector:${id}`)))
1437
+ }).pipe(Atom3.withLabel(`graph-builder:connector:${id}`)))
560
1438
  } : void 0,
561
1439
  actionGroups ? {
562
1440
  id: getId("actionGroups"),
563
1441
  position,
564
- relation: "outbound",
565
- connector: Rx2.family((node) => Rx2.make((get2) => {
1442
+ relation: actionRelation(),
1443
+ connector: Atom3.family((node) => Atom3.make((get2) => {
566
1444
  try {
567
1445
  return get2(actionGroups(node)).map((arg) => ({
568
1446
  ...arg,
569
1447
  data: actionGroupSymbol,
570
- type: ACTION_GROUP_TYPE
1448
+ type: ActionGroupType
571
1449
  }));
572
- } catch {
1450
+ } catch (error) {
573
1451
  log2.warn("Error in actionGroups", {
574
1452
  id: getId("actionGroups"),
575
- node
1453
+ node,
1454
+ error
576
1455
  }, {
577
- F: __dxlog_file2,
578
- L: 133,
1456
+ F: __dxlog_file3,
1457
+ L: 617,
579
1458
  S: void 0,
580
1459
  C: (f, a) => f(...a)
581
1460
  });
582
1461
  return [];
583
1462
  }
584
- }).pipe(Rx2.withLabel(`graph-builder:connector:actionGroups:${id}`)))
1463
+ }).pipe(Atom3.withLabel(`graph-builder:connector:actionGroups:${id}`)))
585
1464
  } : void 0,
586
1465
  actions ? {
587
1466
  id: getId("actions"),
588
1467
  position,
589
- relation: "outbound",
590
- connector: Rx2.family((node) => Rx2.make((get2) => {
1468
+ relation: actionRelation(),
1469
+ connector: Atom3.family((node) => Atom3.make((get2) => {
591
1470
  try {
592
1471
  return get2(actions(node)).map((arg) => ({
593
1472
  ...arg,
594
- type: ACTION_TYPE
1473
+ type: ActionType
595
1474
  }));
596
- } catch {
1475
+ } catch (error) {
597
1476
  log2.warn("Error in actions", {
598
1477
  id: getId("actions"),
599
- node
1478
+ node,
1479
+ error
600
1480
  }, {
601
- F: __dxlog_file2,
602
- L: 150,
1481
+ F: __dxlog_file3,
1482
+ L: 634,
603
1483
  S: void 0,
604
1484
  C: (f, a) => f(...a)
605
1485
  });
606
1486
  return [];
607
1487
  }
608
- }).pipe(Rx2.withLabel(`graph-builder:connector:actions:${id}`)))
1488
+ }).pipe(Atom3.withLabel(`graph-builder:connector:actions:${id}`)))
609
1489
  } : void 0
610
1490
  ].filter(isNonNullable2);
611
1491
  };
1492
+ var runEffectSyncWithFallback = (effect, context2, extensionId, fallback) => {
1493
+ return Effect.runSync(effect.pipe(Effect.provide(context2), Effect.catchAll((error) => {
1494
+ log2.warn("Extension failed", {
1495
+ extension: extensionId,
1496
+ error
1497
+ }, {
1498
+ F: __dxlog_file3,
1499
+ L: 677,
1500
+ S: void 0,
1501
+ C: (f, a) => f(...a)
1502
+ });
1503
+ return Effect.succeed(fallback);
1504
+ })));
1505
+ };
1506
+ var createExtension = (options) => Effect.map(Effect.context(), (context2) => {
1507
+ const { id, match: match3, actions, connector, resolver, relation: relation2, position } = options;
1508
+ const connectorExtension = connector ? createConnectorWithRuntime(id, match3, connector, context2) : void 0;
1509
+ const actionsExtension = actions ? (node) => Atom3.make((get2) => Function2.pipe(get2(node), Option3.flatMap(match3), Option3.map((matched) => runEffectSyncWithFallback(actions(matched, get2), context2, id, []).map((action) => ({
1510
+ ...action,
1511
+ // Attach captured context for action execution.
1512
+ _actionContext: context2
1513
+ }))), Option3.getOrElse(() => []))) : void 0;
1514
+ const resolverExtension = resolver ? (nodeId) => Atom3.make((get2) => runEffectSyncWithFallback(resolver(nodeId, get2), context2, id, null) ?? null) : void 0;
1515
+ return createExtensionRaw({
1516
+ id,
1517
+ relation: relation2,
1518
+ position,
1519
+ connector: connectorExtension,
1520
+ actions: actionsExtension,
1521
+ resolver: resolverExtension
1522
+ });
1523
+ });
1524
+ var createConnector = (matcher, factory) => {
1525
+ return (node) => Atom3.make((get2) => Function2.pipe(get2(node), Option3.flatMap(matcher), Option3.map((data) => factory(data, get2)), Option3.getOrElse(() => [])));
1526
+ };
1527
+ var createConnectorWithRuntime = (extensionId, matcher, factory, context2) => {
1528
+ return (node) => Atom3.make((get2) => Function2.pipe(get2(node), Option3.flatMap(matcher), Option3.map((data) => runEffectSyncWithFallback(factory(data, get2), context2, extensionId, [])), Option3.getOrElse(() => [])));
1529
+ };
1530
+ var createTypeExtension = (options) => {
1531
+ const { id, type, actions, connector, relation: relation2, position } = options;
1532
+ return createExtension({
1533
+ id,
1534
+ match: whenEchoType(type),
1535
+ actions,
1536
+ connector,
1537
+ relation: relation2,
1538
+ position
1539
+ });
1540
+ };
1541
+ var qualifyNodeArgs = (parentId) => (nodes) => nodes.map((node) => {
1542
+ validateSegmentId(node.id);
1543
+ const qualified = qualifyId(parentId, node.id);
1544
+ return {
1545
+ ...node,
1546
+ id: qualified,
1547
+ nodes: node.nodes ? qualifyNodeArgs(qualified)(node.nodes) : void 0
1548
+ };
1549
+ });
1550
+ var connectorKey = (id, relation2) => `${id}${Separators.primary}${relationKey(relation2)}`;
1551
+ var relationFromConnectorKey = (key) => {
1552
+ const separatorIndex = key.indexOf(Separators.primary);
1553
+ const id = key.slice(0, separatorIndex);
1554
+ return {
1555
+ id,
1556
+ relation: relationFromKey(key.slice(separatorIndex + 1))
1557
+ };
1558
+ };
1559
+ var subscriptionKey = (id, kind, detail) => detail != null ? `${id}${Separators.primary}${kind}${Separators.primary}${detail}` : `${id}${Separators.primary}${kind}`;
612
1560
  var flattenExtensions = (extension, acc = []) => {
613
- if (Array.isArray(extension)) {
1561
+ if (Array2.isArray(extension)) {
614
1562
  return [
615
1563
  ...acc,
616
1564
  ...extension.flatMap((ext) => flattenExtensions(ext, acc))
@@ -622,239 +1570,11 @@ var flattenExtensions = (extension, acc = []) => {
622
1570
  ];
623
1571
  }
624
1572
  };
625
- var GraphBuilder = class _GraphBuilder {
626
- static from(pickle, registry) {
627
- if (!pickle) {
628
- return new _GraphBuilder({
629
- registry
630
- });
631
- }
632
- const { nodes, edges } = JSON.parse(pickle);
633
- return new _GraphBuilder({
634
- nodes,
635
- edges,
636
- registry
637
- });
638
- }
639
- get graph() {
640
- return this._graph;
641
- }
642
- get extensions() {
643
- return this._extensions;
644
- }
645
- addExtension(extensions) {
646
- flattenExtensions(extensions).forEach((extension) => {
647
- const extensions2 = this._registry.get(this._extensions);
648
- this._registry.set(this._extensions, Record2.set(extensions2, extension.id, extension));
649
- });
650
- return this;
651
- }
652
- removeExtension(id) {
653
- const extensions = this._registry.get(this._extensions);
654
- this._registry.set(this._extensions, Record2.remove(extensions, id));
655
- return this;
656
- }
657
- async explore({ registry = Registry2.make(), source = ROOT_ID, relation = "outbound", visitor }, path = []) {
658
- if (path.includes(source)) {
659
- return;
660
- }
661
- if (!isNode()) {
662
- const { yieldOrContinue } = await import("main-thread-scheduling");
663
- await yieldOrContinue("idle");
664
- }
665
- const node = registry.get(this._graph.nodeOrThrow(source));
666
- const shouldContinue = await visitor(node, [
667
- ...path,
668
- node.id
669
- ]);
670
- if (shouldContinue === false) {
671
- return;
672
- }
673
- const nodes = Object.values(this._registry.get(this._extensions)).filter((extension) => relation === (extension.relation ?? "outbound")).map((extension) => extension.connector).filter(isNonNullable2).flatMap((connector) => registry.get(connector(this._graph.node(source))));
674
- await Promise.all(nodes.map((nodeArg) => {
675
- registry.set(this._graph._node(nodeArg.id), this._graph._constructNode(nodeArg));
676
- return this.explore({
677
- registry,
678
- source: nodeArg.id,
679
- relation,
680
- visitor
681
- }, [
682
- ...path,
683
- node.id
684
- ]);
685
- }));
686
- if (registry !== this._registry) {
687
- registry.reset();
688
- registry.dispose();
689
- }
690
- }
691
- destroy() {
692
- this._subscriptions.forEach((unsubscribe) => unsubscribe());
693
- this._subscriptions.clear();
694
- }
695
- _onExpand(id, relation) {
696
- log2("onExpand", {
697
- id,
698
- relation,
699
- registry: getDebugName(this._registry)
700
- }, {
701
- F: __dxlog_file2,
702
- L: 327,
703
- S: this,
704
- C: (f, a) => f(...a)
705
- });
706
- const connectors = this._connectors(`${id}+${relation}`);
707
- let previous = [];
708
- const cancel = this._registry.subscribe(connectors, (nodes) => {
709
- const ids = nodes.map((n) => n.id);
710
- const removed = previous.filter((id2) => !ids.includes(id2));
711
- previous = ids;
712
- log2("update", {
713
- id,
714
- relation,
715
- ids,
716
- removed
717
- }, {
718
- F: __dxlog_file2,
719
- L: 338,
720
- S: this,
721
- C: (f, a) => f(...a)
722
- });
723
- const update = () => {
724
- Rx2.batch(() => {
725
- this._graph.removeEdges(removed.map((target) => ({
726
- source: id,
727
- target
728
- })), true);
729
- this._graph.addNodes(nodes);
730
- this._graph.addEdges(nodes.map((node) => relation === "outbound" ? {
731
- source: id,
732
- target: node.id
733
- } : {
734
- source: node.id,
735
- target: id
736
- }));
737
- this._graph.sortEdges(id, relation, nodes.map(({ id: id2 }) => id2));
738
- });
739
- };
740
- if (typeof requestAnimationFrame === "function") {
741
- requestAnimationFrame(update);
742
- } else {
743
- update();
744
- }
745
- }, {
746
- immediate: true
747
- });
748
- this._subscriptions.set(id, cancel);
749
- }
750
- // TODO(wittjosiah): If the same node is added by a connector, the resolver should probably cancel itself?
751
- async _onInitialize(id) {
752
- log2("onInitialize", {
753
- id
754
- }, {
755
- F: __dxlog_file2,
756
- L: 375,
757
- S: this,
758
- C: (f, a) => f(...a)
759
- });
760
- const resolver = this._resolvers(id);
761
- const cancel = this._registry.subscribe(resolver, (node) => {
762
- const trigger = this._initialized[id];
763
- Option2.match(node, {
764
- onSome: (node2) => {
765
- this._graph.addNodes([
766
- node2
767
- ]);
768
- trigger?.wake();
769
- },
770
- onNone: () => {
771
- trigger?.wake();
772
- this._graph.removeNodes([
773
- id
774
- ]);
775
- }
776
- });
777
- }, {
778
- immediate: true
779
- });
780
- this._subscriptions.set(id, cancel);
781
- }
782
- _onRemoveNode(id) {
783
- this._subscriptions.get(id)?.();
784
- this._subscriptions.delete(id);
785
- }
786
- constructor({ registry, ...params } = {}) {
787
- _define_property2(this, "_subscriptions", /* @__PURE__ */ new Map());
788
- _define_property2(this, "_extensions", Rx2.make(Record2.empty()).pipe(Rx2.keepAlive, Rx2.withLabel("graph-builder:extensions")));
789
- _define_property2(this, "_initialized", {});
790
- _define_property2(this, "_registry", void 0);
791
- _define_property2(this, "_graph", void 0);
792
- _define_property2(this, "_resolvers", Rx2.family((id) => {
793
- return Rx2.make((get2) => {
794
- return Function2.pipe(get2(this._extensions), Record2.values, Array.sortBy(byPosition), Array.map(({ resolver }) => resolver), Array.filter(isNonNullable2), Array.map((resolver) => get2(resolver(id))), Array.filter(isNonNullable2), Array.head);
795
- });
796
- }));
797
- _define_property2(this, "_connectors", Rx2.family((key) => {
798
- return Rx2.make((get2) => {
799
- const [id, relation] = key.split("+");
800
- const node = this._graph.node(id);
801
- return Function2.pipe(
802
- get2(this._extensions),
803
- Record2.values,
804
- // TODO(wittjosiah): Sort on write rather than read.
805
- Array.sortBy(byPosition),
806
- Array.filter(({ relation: _relation = "outbound" }) => _relation === relation),
807
- Array.map(({ connector }) => connector?.(node)),
808
- Array.filter(isNonNullable2),
809
- Array.flatMap((result) => get2(result))
810
- );
811
- }).pipe(Rx2.withLabel(`graph-builder:connectors:${key}`));
812
- }));
813
- this._registry = registry ?? Registry2.make();
814
- this._graph = new Graph({
815
- ...params,
816
- registry: this._registry,
817
- onExpand: (id, relation) => this._onExpand(id, relation),
818
- onInitialize: (id) => this._onInitialize(id),
819
- onRemoveNode: (id) => this._onRemoveNode(id)
820
- });
821
- }
822
- };
823
- var rxFromSignal = (cb) => {
824
- return Rx2.make((get2) => {
825
- const dispose = effect(() => {
826
- get2.setSelf(cb());
827
- });
828
- get2.addFinalizer(() => dispose());
829
- return cb();
830
- });
831
- };
832
- var observableFamily = Rx2.family((observable) => {
833
- return Rx2.make((get2) => {
834
- const subscription = observable.subscribe((value) => get2.setSelf(value));
835
- get2.addFinalizer(() => subscription.unsubscribe());
836
- return observable.get();
837
- });
838
- });
839
- var rxFromObservable = (observable) => {
840
- return observableFamily(observable);
841
- };
842
1573
  export {
843
- ACTION_GROUP_TYPE,
844
- ACTION_TYPE,
845
- Graph,
846
- GraphBuilder,
847
- ROOT_ID,
848
- ROOT_TYPE,
849
- actionGroupSymbol,
850
- createExtension,
851
- flattenExtensions,
852
- getGraph,
853
- isAction,
854
- isActionGroup,
855
- isActionLike,
856
- isGraphNode,
857
- rxFromObservable,
858
- rxFromSignal
1574
+ atoms_exports as CreateAtom,
1575
+ graph_exports as Graph,
1576
+ graph_builder_exports as GraphBuilder,
1577
+ node_exports as Node,
1578
+ node_matcher_exports as NodeMatcher
859
1579
  };
860
1580
  //# sourceMappingURL=index.mjs.map