@dxos/app-graph 0.8.4-main.ead640a → 0.8.4-main.ef1bc66f44

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 (43) hide show
  1. package/dist/lib/browser/index.mjs +1105 -666
  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 +1104 -666
  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 +108 -66
  10. package/dist/types/src/graph-builder.d.ts.map +1 -1
  11. package/dist/types/src/graph.d.ts +182 -212
  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 +32 -3
  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/tsconfig.tsbuildinfo +1 -1
  23. package/package.json +35 -33
  24. package/src/atoms.ts +25 -0
  25. package/src/graph-builder.test.ts +520 -104
  26. package/src/graph-builder.ts +550 -255
  27. package/src/graph.test.ts +299 -106
  28. package/src/graph.ts +964 -394
  29. package/src/index.ts +9 -3
  30. package/src/node-matcher.test.ts +301 -0
  31. package/src/node-matcher.ts +284 -0
  32. package/src/node.ts +39 -6
  33. package/src/stories/EchoGraph.stories.tsx +105 -96
  34. package/src/stories/Tree.tsx +2 -2
  35. package/dist/types/src/experimental/graph-projections.test.d.ts +0 -25
  36. package/dist/types/src/experimental/graph-projections.test.d.ts.map +0 -1
  37. package/dist/types/src/signals-integration.test.d.ts +0 -2
  38. package/dist/types/src/signals-integration.test.d.ts.map +0 -1
  39. package/dist/types/src/testing.d.ts +0 -5
  40. package/dist/types/src/testing.d.ts.map +0 -1
  41. package/src/experimental/graph-projections.test.ts +0 -56
  42. package/src/signals-integration.test.ts +0 -218
  43. package/src/testing.ts +0 -20
@@ -1,33 +1,97 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ // src/atoms.ts
8
+ var atoms_exports = {};
9
+ __export(atoms_exports, {
10
+ fromObservable: () => fromObservable
11
+ });
12
+ import { Atom } from "@effect-atom/atom-react";
13
+ var observableFamily = Atom.family((observable) => {
14
+ return Atom.make((get2) => {
15
+ const subscription = observable.subscribe((value) => get2.setSelf(value));
16
+ get2.addFinalizer(() => subscription.unsubscribe());
17
+ return observable.get();
18
+ });
19
+ });
20
+ var fromObservable = (observable) => {
21
+ return observableFamily(observable);
22
+ };
23
+
1
24
  // src/graph.ts
2
- import { Registry, Rx } from "@effect-rx/rx-react";
25
+ var graph_exports = {};
26
+ __export(graph_exports, {
27
+ GraphKind: () => GraphKind,
28
+ GraphTypeId: () => GraphTypeId,
29
+ addEdge: () => addEdge,
30
+ addEdges: () => addEdges,
31
+ addNode: () => addNode,
32
+ addNodes: () => addNodes,
33
+ expand: () => expand,
34
+ getActions: () => getActions,
35
+ getConnections: () => getConnections,
36
+ getEdges: () => getEdges,
37
+ getGraph: () => getGraph,
38
+ getNode: () => getNode,
39
+ getNodeOrThrow: () => getNodeOrThrow,
40
+ getPath: () => getPath,
41
+ getRoot: () => getRoot,
42
+ initialize: () => initialize,
43
+ make: () => make,
44
+ removeEdge: () => removeEdge,
45
+ removeEdges: () => removeEdges,
46
+ removeNode: () => removeNode,
47
+ removeNodes: () => removeNodes,
48
+ sortEdges: () => sortEdges,
49
+ toJSON: () => toJSON,
50
+ traverse: () => traverse,
51
+ waitForPath: () => waitForPath
52
+ });
53
+ import { Atom as Atom2, Registry } from "@effect-atom/atom-react";
3
54
  import * as Function from "effect/Function";
4
55
  import * as Option from "effect/Option";
56
+ import * as Pipeable from "effect/Pipeable";
5
57
  import * as Record from "effect/Record";
6
58
  import { Event, Trigger } from "@dxos/async";
7
59
  import { todo } from "@dxos/debug";
8
60
  import { invariant } from "@dxos/invariant";
9
61
  import { log } from "@dxos/log";
10
62
  import { isNonNullable } from "@dxos/util";
11
- function _define_property(obj, key, value) {
12
- if (key in obj) {
13
- Object.defineProperty(obj, key, {
14
- value,
15
- enumerable: true,
16
- configurable: true,
17
- writable: true
18
- });
19
- } else {
20
- obj[key] = value;
21
- }
22
- return obj;
23
- }
63
+
64
+ // src/node.ts
65
+ var node_exports = {};
66
+ __export(node_exports, {
67
+ ActionGroupType: () => ActionGroupType,
68
+ ActionType: () => ActionType,
69
+ RootId: () => RootId,
70
+ RootType: () => RootType,
71
+ actionGroupSymbol: () => actionGroupSymbol,
72
+ isAction: () => isAction,
73
+ isActionGroup: () => isActionGroup,
74
+ isActionLike: () => isActionLike,
75
+ isGraphNode: () => isGraphNode
76
+ });
77
+ var RootId = "root";
78
+ var RootType = "dxos.org/type/GraphRoot";
79
+ var ActionType = "dxos.org/type/GraphAction";
80
+ var ActionGroupType = "dxos.org/type/GraphActionGroup";
81
+ var isGraphNode = (data) => data && typeof data === "object" && "id" in data && "properties" in data && data.properties ? typeof data.properties === "object" && "data" in data : false;
82
+ var isAction = (data) => isGraphNode(data) ? typeof data.data === "function" && data.type === ActionType : false;
83
+ var actionGroupSymbol = Symbol("ActionGroup");
84
+ var isActionGroup = (data) => isGraphNode(data) ? data.data === actionGroupSymbol && data.type === ActionGroupType : false;
85
+ var isActionLike = (data) => isAction(data) || isActionGroup(data);
86
+
87
+ // src/graph.ts
24
88
  var __dxlog_file = "/__w/dxos/dxos/packages/sdk/app-graph/src/graph.ts";
25
89
  var graphSymbol = Symbol("graph");
26
90
  var getGraph = (node) => {
27
91
  const graph = node[graphSymbol];
28
92
  invariant(graph, "Node is not associated with a graph.", {
29
93
  F: __dxlog_file,
30
- L: 27,
94
+ L: 32,
31
95
  S: void 0,
32
96
  A: [
33
97
  "graph",
@@ -36,603 +100,815 @@ var getGraph = (node) => {
36
100
  });
37
101
  return graph;
38
102
  };
39
- var ROOT_ID = "root";
40
- var ROOT_TYPE = "dxos.org/type/GraphRoot";
41
- var ACTION_TYPE = "dxos.org/type/GraphAction";
42
- var ACTION_GROUP_TYPE = "dxos.org/type/GraphActionGroup";
43
- var Graph = class {
44
- toJSON(id = ROOT_ID) {
45
- return this._registry.get(this._json(id));
103
+ var GraphTypeId = Symbol.for("@dxos/app-graph/Graph");
104
+ var GraphKind = Symbol.for("@dxos/app-graph/GraphKind");
105
+ var GraphImpl = class {
106
+ [GraphTypeId] = GraphTypeId;
107
+ [GraphKind] = "writable";
108
+ pipe() {
109
+ return Pipeable.pipeArguments(this, arguments);
110
+ }
111
+ onNodeChanged = new Event();
112
+ _onExpand;
113
+ _onInitialize;
114
+ _onRemoveNode;
115
+ _registry;
116
+ _expanded = Record.empty();
117
+ _initialized = Record.empty();
118
+ _initialEdges = Record.empty();
119
+ _initialNodes = Record.fromEntries([
120
+ [
121
+ RootId,
122
+ this._constructNode({
123
+ id: RootId,
124
+ type: RootType,
125
+ data: null,
126
+ properties: {}
127
+ })
128
+ ]
129
+ ]);
130
+ /** @internal */
131
+ _node = Atom2.family((id) => {
132
+ const initial = Option.flatten(Record.get(this._initialNodes, id));
133
+ return Atom2.make(initial).pipe(Atom2.keepAlive, Atom2.withLabel(`graph:node:${id}`));
134
+ });
135
+ _nodeOrThrow = Atom2.family((id) => {
136
+ return Atom2.make((get2) => {
137
+ const node = get2(this._node(id));
138
+ invariant(Option.isSome(node), `Node not available: ${id}`, {
139
+ F: __dxlog_file,
140
+ L: 174,
141
+ S: this,
142
+ A: [
143
+ "Option.isSome(node)",
144
+ "`Node not available: ${id}`"
145
+ ]
146
+ });
147
+ return node.value;
148
+ });
149
+ });
150
+ _edges = Atom2.family((id) => {
151
+ const initial = Record.get(this._initialEdges, id).pipe(Option.getOrElse(() => ({
152
+ inbound: [],
153
+ outbound: []
154
+ })));
155
+ return Atom2.make(initial).pipe(Atom2.keepAlive, Atom2.withLabel(`graph:edges:${id}`));
156
+ });
157
+ // NOTE: Currently the argument to the family needs to be referentially stable for the atom to be referentially stable.
158
+ // TODO(wittjosiah): Atom feature request, support for something akin to `ComplexMap` to allow for complex arguments.
159
+ _connections = Atom2.family((key) => {
160
+ return Atom2.make((get2) => {
161
+ const [id, relation] = key.split("$");
162
+ const edges = get2(this._edges(id));
163
+ return edges[relation].map((id2) => get2(this._node(id2))).filter(Option.isSome).map((o) => o.value);
164
+ }).pipe(Atom2.withLabel(`graph:connections:${key}`));
165
+ });
166
+ _actions = Atom2.family((id) => {
167
+ return Atom2.make((get2) => {
168
+ return get2(this._connections(`${id}$outbound`)).filter((node) => node.type === ActionType || node.type === ActionGroupType);
169
+ }).pipe(Atom2.withLabel(`graph:actions:${id}`));
170
+ });
171
+ _json = Atom2.family((id) => {
172
+ return Atom2.make((get2) => {
173
+ const toJSON2 = (node, seen = []) => {
174
+ const nodes = get2(this._connections(`${node.id}$outbound`));
175
+ const obj = {
176
+ id: node.id,
177
+ type: node.type
178
+ };
179
+ if (node.properties.label) {
180
+ obj.label = node.properties.label;
181
+ }
182
+ if (nodes.length) {
183
+ obj.nodes = nodes.map((n) => {
184
+ const nextSeen = [
185
+ ...seen,
186
+ node.id
187
+ ];
188
+ return nextSeen.includes(n.id) ? void 0 : toJSON2(n, nextSeen);
189
+ }).filter(isNonNullable);
190
+ }
191
+ return obj;
192
+ };
193
+ const root = get2(this._nodeOrThrow(id));
194
+ return toJSON2(root);
195
+ }).pipe(Atom2.withLabel(`graph:json:${id}`));
196
+ });
197
+ constructor({ registry, nodes, edges, onInitialize, onExpand, onRemoveNode } = {}) {
198
+ this._registry = registry ?? Registry.make();
199
+ this._onInitialize = onInitialize;
200
+ this._onExpand = onExpand;
201
+ this._onRemoveNode = onRemoveNode;
202
+ if (nodes) {
203
+ nodes.forEach((node) => {
204
+ Record.set(this._initialNodes, node.id, this._constructNode(node));
205
+ });
206
+ }
207
+ if (edges) {
208
+ Object.entries(edges).forEach(([source, edges2]) => {
209
+ Record.set(this._initialEdges, source, edges2);
210
+ });
211
+ }
46
212
  }
47
- json(id = ROOT_ID) {
48
- return this._json(id);
213
+ json(id = RootId) {
214
+ return jsonImpl(this, id);
49
215
  }
50
216
  node(id) {
51
- return this._node(id);
217
+ return nodeImpl(this, id);
52
218
  }
53
219
  nodeOrThrow(id) {
54
- return this._nodeOrThrow(id);
220
+ return nodeOrThrowImpl(this, id);
55
221
  }
56
222
  connections(id, relation = "outbound") {
57
- return this._connections(`${id}$${relation}`);
223
+ return connectionsImpl(this, id, relation);
58
224
  }
59
225
  actions(id) {
60
- return this._actions(id);
226
+ return actionsImpl(this, id);
61
227
  }
62
228
  edges(id) {
63
- return this._edges(id);
229
+ return edgesImpl(this, id);
64
230
  }
65
- get root() {
66
- return this.getNodeOrThrow(ROOT_ID);
231
+ /** @internal */
232
+ _constructNode(node) {
233
+ return Option.some({
234
+ [graphSymbol]: this,
235
+ data: null,
236
+ properties: {},
237
+ ...node
238
+ });
67
239
  }
68
- getNode(id) {
69
- return this._registry.get(this.node(id));
240
+ };
241
+ var getInternal = (graph) => {
242
+ return graph;
243
+ };
244
+ var toJSON = (graph, id = RootId) => {
245
+ const internal = getInternal(graph);
246
+ return internal._registry.get(internal._json(id));
247
+ };
248
+ var jsonImpl = (graph, id = RootId) => {
249
+ const internal = getInternal(graph);
250
+ return internal._json(id);
251
+ };
252
+ var nodeImpl = (graph, id) => {
253
+ const internal = getInternal(graph);
254
+ return internal._node(id);
255
+ };
256
+ var nodeOrThrowImpl = (graph, id) => {
257
+ const internal = getInternal(graph);
258
+ return internal._nodeOrThrow(id);
259
+ };
260
+ var connectionsImpl = (graph, id, relation = "outbound") => {
261
+ const internal = getInternal(graph);
262
+ return internal._connections(`${id}$${relation}`);
263
+ };
264
+ var actionsImpl = (graph, id) => {
265
+ const internal = getInternal(graph);
266
+ return internal._actions(id);
267
+ };
268
+ var edgesImpl = (graph, id) => {
269
+ const internal = getInternal(graph);
270
+ return internal._edges(id);
271
+ };
272
+ var getNodeImpl = (graph, id) => {
273
+ const internal = getInternal(graph);
274
+ return internal._registry.get(nodeImpl(graph, id));
275
+ };
276
+ function getNode(graphOrId, id) {
277
+ if (typeof graphOrId === "string") {
278
+ const id2 = graphOrId;
279
+ return (graph) => getNodeImpl(graph, id2);
280
+ } else {
281
+ const graph = graphOrId;
282
+ return getNodeImpl(graph, id);
70
283
  }
71
- getNodeOrThrow(id) {
72
- return this._registry.get(this.nodeOrThrow(id));
284
+ }
285
+ var getNodeOrThrowImpl = (graph, id) => {
286
+ const internal = getInternal(graph);
287
+ return internal._registry.get(nodeOrThrowImpl(graph, id));
288
+ };
289
+ function getNodeOrThrow(graphOrId, id) {
290
+ if (typeof graphOrId === "string") {
291
+ const id2 = graphOrId;
292
+ return (graph) => getNodeOrThrowImpl(graph, id2);
293
+ } else {
294
+ const graph = graphOrId;
295
+ return getNodeOrThrowImpl(graph, id);
73
296
  }
74
- getConnections(id, relation = "outbound") {
75
- return this._registry.get(this.connections(id, relation));
297
+ }
298
+ function getRoot(graph) {
299
+ return getNodeOrThrowImpl(graph, RootId);
300
+ }
301
+ var getConnectionsImpl = (graph, id, relation = "outbound") => {
302
+ const internal = getInternal(graph);
303
+ return internal._registry.get(connectionsImpl(graph, id, relation));
304
+ };
305
+ function getConnections(graphOrId, idOrRelation, relation) {
306
+ if (typeof graphOrId === "string") {
307
+ const id = graphOrId;
308
+ const rel = (typeof idOrRelation === "string" ? "outbound" : idOrRelation) ?? "outbound";
309
+ return (graph) => getConnectionsImpl(graph, id, rel);
310
+ } else {
311
+ const graph = graphOrId;
312
+ const id = idOrRelation;
313
+ const rel = relation ?? "outbound";
314
+ return getConnectionsImpl(graph, id, rel);
76
315
  }
77
- getActions(id) {
78
- return this._registry.get(this.actions(id));
316
+ }
317
+ var getActionsImpl = (graph, id) => {
318
+ const internal = getInternal(graph);
319
+ return internal._registry.get(actionsImpl(graph, id));
320
+ };
321
+ function getActions(graphOrId, id) {
322
+ if (typeof graphOrId === "string") {
323
+ const id2 = graphOrId;
324
+ return (graph) => getActionsImpl(graph, id2);
325
+ } else {
326
+ const graph = graphOrId;
327
+ return getActionsImpl(graph, id);
79
328
  }
80
- getEdges(id) {
81
- return this._registry.get(this.edges(id));
329
+ }
330
+ var getEdgesImpl = (graph, id) => {
331
+ const internal = getInternal(graph);
332
+ return internal._registry.get(edgesImpl(graph, id));
333
+ };
334
+ function getEdges(graphOrId, id) {
335
+ if (typeof graphOrId === "string") {
336
+ const id2 = graphOrId;
337
+ return (graph) => getEdgesImpl(graph, id2);
338
+ } else {
339
+ const graph = graphOrId;
340
+ return getEdgesImpl(graph, id);
82
341
  }
83
- async initialize(id) {
84
- const initialized = Record.get(this._initialized, id).pipe(Option.getOrElse(() => false));
85
- log("initialize", {
86
- id,
87
- initialized
88
- }, {
89
- F: __dxlog_file,
90
- L: 386,
91
- S: this,
92
- C: (f, a) => f(...a)
93
- });
94
- if (!initialized) {
95
- await this._onInitialize?.(id);
96
- Record.set(this._initialized, id, true);
97
- }
342
+ }
343
+ var traverseImpl = (graph, options, path = []) => {
344
+ const { visitor, source = RootId, relation = "outbound" } = options;
345
+ if (path.includes(source)) {
346
+ return;
98
347
  }
99
- expand(id, relation = "outbound") {
100
- const key = `${id}$${relation}`;
101
- const expanded = Record.get(this._expanded, key).pipe(Option.getOrElse(() => false));
102
- log("expand", {
103
- key,
104
- expanded
105
- }, {
106
- F: __dxlog_file,
107
- L: 396,
108
- S: this,
109
- C: (f, a) => f(...a)
348
+ const node = getNodeOrThrow(graph, source);
349
+ const shouldContinue = visitor(node, [
350
+ ...path,
351
+ source
352
+ ]);
353
+ if (shouldContinue === false) {
354
+ return;
355
+ }
356
+ Object.values(getConnections(graph, source, relation)).forEach((child) => traverseImpl(graph, {
357
+ source: child.id,
358
+ relation,
359
+ visitor
360
+ }, [
361
+ ...path,
362
+ source
363
+ ]));
364
+ };
365
+ function traverse(graphOrOptions, optionsOrPath, path) {
366
+ if (typeof graphOrOptions === "object" && "visitor" in graphOrOptions) {
367
+ const options = graphOrOptions;
368
+ const pathArg = Array.isArray(optionsOrPath) ? optionsOrPath : void 0;
369
+ return (graph) => traverseImpl(graph, options, pathArg);
370
+ } else {
371
+ const graph = graphOrOptions;
372
+ const options = optionsOrPath;
373
+ const pathArg = path ?? (Array.isArray(optionsOrPath) ? optionsOrPath : void 0);
374
+ return traverseImpl(graph, options, pathArg);
375
+ }
376
+ }
377
+ var getPathImpl = (graph, params) => {
378
+ return Function.pipe(getNode(graph, params.source ?? "root"), Option.flatMap((node) => {
379
+ let found = Option.none();
380
+ traverseImpl(graph, {
381
+ source: node.id,
382
+ visitor: (node2, path) => {
383
+ if (Option.isSome(found)) {
384
+ return false;
385
+ }
386
+ if (node2.id === params.target) {
387
+ found = Option.some(path);
388
+ }
389
+ }
110
390
  });
111
- if (!expanded) {
112
- this._onExpand?.(id, relation);
113
- Record.set(this._expanded, key, true);
391
+ return found;
392
+ }));
393
+ };
394
+ function getPath(graphOrParams, params) {
395
+ if (params === void 0 && typeof graphOrParams === "object" && "target" in graphOrParams) {
396
+ const params2 = graphOrParams;
397
+ return (graph) => getPathImpl(graph, params2);
398
+ } else {
399
+ const graph = graphOrParams;
400
+ return getPathImpl(graph, params);
401
+ }
402
+ }
403
+ var waitForPathImpl = (graph, params, options) => {
404
+ const { timeout = 5e3, interval = 500 } = options ?? {};
405
+ const path = getPathImpl(graph, params);
406
+ if (Option.isSome(path)) {
407
+ return Promise.resolve(path.value);
408
+ }
409
+ const trigger = new Trigger();
410
+ const i = setInterval(() => {
411
+ const path2 = getPathImpl(graph, params);
412
+ if (Option.isSome(path2)) {
413
+ trigger.wake(path2.value);
114
414
  }
415
+ }, interval);
416
+ return trigger.wait({
417
+ timeout
418
+ }).finally(() => clearInterval(i));
419
+ };
420
+ function waitForPath(graphOrParams, paramsOrOptions, options) {
421
+ if (typeof graphOrParams === "object" && "target" in graphOrParams) {
422
+ const params = graphOrParams;
423
+ const opts = typeof paramsOrOptions === "object" && !("target" in paramsOrOptions) ? paramsOrOptions : void 0;
424
+ return (graph) => waitForPathImpl(graph, params, opts);
425
+ } else {
426
+ const graph = graphOrParams;
427
+ const params = paramsOrOptions;
428
+ return waitForPathImpl(graph, params, options);
115
429
  }
116
- addNodes(nodes) {
117
- Rx.batch(() => {
118
- nodes.map((node) => this.addNode(node));
119
- });
430
+ }
431
+ var initializeImpl = async (graph, id) => {
432
+ const internal = getInternal(graph);
433
+ const initialized = Record.get(internal._initialized, id).pipe(Option.getOrElse(() => false));
434
+ log("initialize", {
435
+ id,
436
+ initialized
437
+ }, {
438
+ F: __dxlog_file,
439
+ L: 661,
440
+ S: void 0,
441
+ C: (f, a) => f(...a)
442
+ });
443
+ if (!initialized) {
444
+ await internal._onInitialize?.(id);
445
+ Record.set(internal._initialized, id, true);
120
446
  }
121
- addNode({ nodes, edges, ...nodeArg }) {
122
- const { id, type, data = null, properties = {} } = nodeArg;
123
- const nodeRx = this._node(id);
124
- const node = this._registry.get(nodeRx);
125
- Option.match(node, {
126
- onSome: (node2) => {
127
- const typeChanged = node2.type !== type;
128
- const dataChanged = node2.data !== data;
129
- const propertiesChanged = Object.keys(properties).some((key) => node2.properties[key] !== properties[key]);
130
- log("existing node", {
131
- id,
132
- typeChanged,
133
- dataChanged,
134
- propertiesChanged
135
- }, {
136
- F: __dxlog_file,
137
- L: 418,
138
- S: this,
139
- C: (f, a) => f(...a)
140
- });
141
- if (typeChanged || dataChanged || propertiesChanged) {
142
- log("updating node", {
143
- id,
144
- type,
145
- data,
146
- properties
147
- }, {
148
- F: __dxlog_file,
149
- L: 420,
150
- S: this,
151
- C: (f, a) => f(...a)
152
- });
153
- const newNode = Option.some({
154
- ...node2,
155
- type,
156
- data,
157
- properties: {
158
- ...node2.properties,
159
- ...properties
160
- }
161
- });
162
- this._registry.set(nodeRx, newNode);
163
- this.onNodeChanged.emit({
164
- id,
165
- node: newNode
166
- });
167
- }
168
- },
169
- onNone: () => {
170
- log("new node", {
447
+ return graph;
448
+ };
449
+ function initialize(graphOrId, id) {
450
+ if (typeof graphOrId === "string") {
451
+ const id2 = graphOrId;
452
+ return (graph) => initializeImpl(graph, id2);
453
+ } else {
454
+ const graph = graphOrId;
455
+ return initializeImpl(graph, id);
456
+ }
457
+ }
458
+ var expandImpl = (graph, id, relation = "outbound") => {
459
+ const internal = getInternal(graph);
460
+ const key = `${id}$${relation}`;
461
+ const expanded = Record.get(internal._expanded, key).pipe(Option.getOrElse(() => false));
462
+ log("expand", {
463
+ key,
464
+ expanded
465
+ }, {
466
+ F: __dxlog_file,
467
+ L: 702,
468
+ S: void 0,
469
+ C: (f, a) => f(...a)
470
+ });
471
+ if (!expanded) {
472
+ internal._onExpand?.(id, relation);
473
+ Record.set(internal._expanded, key, true);
474
+ }
475
+ return graph;
476
+ };
477
+ function expand(graphOrId, idOrRelation, relation) {
478
+ if (typeof graphOrId === "string") {
479
+ const id = graphOrId;
480
+ const rel = (typeof idOrRelation === "string" ? "outbound" : idOrRelation) ?? "outbound";
481
+ return (graph) => expandImpl(graph, id, rel);
482
+ } else {
483
+ const graph = graphOrId;
484
+ const id = idOrRelation;
485
+ const rel = relation ?? "outbound";
486
+ return expandImpl(graph, id, rel);
487
+ }
488
+ }
489
+ var sortEdgesImpl = (graph, id, relation, order) => {
490
+ const internal = getInternal(graph);
491
+ const edgesAtom = internal._edges(id);
492
+ const edges = internal._registry.get(edgesAtom);
493
+ const unsorted = edges[relation].filter((id2) => !order.includes(id2)) ?? [];
494
+ const sorted = order.filter((id2) => edges[relation].includes(id2)) ?? [];
495
+ edges[relation].splice(0, edges[relation].length, ...[
496
+ ...sorted,
497
+ ...unsorted
498
+ ]);
499
+ internal._registry.set(edgesAtom, edges);
500
+ return graph;
501
+ };
502
+ function sortEdges(graphOrId, idOrRelation, relationOrOrder, order) {
503
+ if (typeof graphOrId === "string") {
504
+ const id = graphOrId;
505
+ const relation = idOrRelation;
506
+ const order2 = relationOrOrder;
507
+ return (graph) => sortEdgesImpl(graph, id, relation, order2);
508
+ } else {
509
+ const graph = graphOrId;
510
+ const id = idOrRelation;
511
+ const relation = relationOrOrder;
512
+ return sortEdgesImpl(graph, id, relation, order);
513
+ }
514
+ }
515
+ var addNodesImpl = (graph, nodes) => {
516
+ Atom2.batch(() => {
517
+ nodes.map((node) => addNodeImpl(graph, node));
518
+ });
519
+ return graph;
520
+ };
521
+ function addNodes(graphOrNodes, nodes) {
522
+ if (nodes === void 0) {
523
+ const nodes2 = graphOrNodes;
524
+ return (graph) => addNodesImpl(graph, nodes2);
525
+ } else {
526
+ const graph = graphOrNodes;
527
+ return addNodesImpl(graph, nodes);
528
+ }
529
+ }
530
+ var addNodeImpl = (graph, nodeArg) => {
531
+ const internal = getInternal(graph);
532
+ const { nodes, edges, id, type, data = null, properties = {}, ...rest } = nodeArg;
533
+ const nodeAtom = internal._node(id);
534
+ const existingNode = internal._registry.get(nodeAtom);
535
+ Option.match(existingNode, {
536
+ onSome: (existing) => {
537
+ const typeChanged = existing.type !== type;
538
+ const dataChanged = existing.data !== data;
539
+ const propertiesChanged = Object.keys(properties).some((key) => existing.properties[key] !== properties[key]);
540
+ log("existing node", {
541
+ id,
542
+ typeChanged,
543
+ dataChanged,
544
+ propertiesChanged
545
+ }, {
546
+ F: __dxlog_file,
547
+ L: 847,
548
+ S: void 0,
549
+ C: (f, a) => f(...a)
550
+ });
551
+ if (typeChanged || dataChanged || propertiesChanged) {
552
+ log("updating node", {
171
553
  id,
172
554
  type,
173
555
  data,
174
556
  properties
175
557
  }, {
176
558
  F: __dxlog_file,
177
- L: 427,
178
- S: this,
559
+ L: 854,
560
+ S: void 0,
179
561
  C: (f, a) => f(...a)
180
562
  });
181
- const newNode = this._constructNode({
182
- id,
563
+ const newNode = Option.some({
564
+ ...existing,
565
+ ...rest,
183
566
  type,
184
567
  data,
185
- properties
568
+ properties: {
569
+ ...existing.properties,
570
+ ...properties
571
+ }
186
572
  });
187
- this._registry.set(nodeRx, newNode);
188
- this.onNodeChanged.emit({
573
+ internal._registry.set(nodeAtom, newNode);
574
+ graph.onNodeChanged.emit({
189
575
  id,
190
576
  node: newNode
191
577
  });
192
578
  }
193
- });
194
- if (nodes) {
195
- this.addNodes(nodes);
196
- const _edges = nodes.map((node2) => ({
197
- source: id,
198
- target: node2.id
199
- }));
200
- this.addEdges(_edges);
201
- }
202
- if (edges) {
203
- todo();
204
- }
205
- }
206
- removeNodes(ids, edges = false) {
207
- Rx.batch(() => {
208
- ids.map((id) => this.removeNode(id, edges));
209
- });
210
- }
211
- removeNode(id, edges = false) {
212
- const nodeRx = this._node(id);
213
- this._registry.set(nodeRx, Option.none());
214
- this.onNodeChanged.emit({
215
- id,
216
- node: Option.none()
217
- });
218
- if (edges) {
219
- const { inbound, outbound } = this._registry.get(this._edges(id));
220
- const edges2 = [
221
- ...inbound.map((source) => ({
222
- source,
223
- target: id
224
- })),
225
- ...outbound.map((target) => ({
226
- source: id,
227
- target
228
- }))
229
- ];
230
- this.removeEdges(edges2);
231
- }
232
- this._onRemoveNode?.(id);
233
- }
234
- addEdges(edges) {
235
- Rx.batch(() => {
236
- edges.map((edge) => this.addEdge(edge));
237
- });
238
- }
239
- addEdge(edgeArg) {
240
- const sourceRx = this._edges(edgeArg.source);
241
- const source = this._registry.get(sourceRx);
242
- if (!source.outbound.includes(edgeArg.target)) {
243
- log("add outbound edge", {
244
- source: edgeArg.source,
245
- target: edgeArg.target
579
+ },
580
+ onNone: () => {
581
+ log("new node", {
582
+ id,
583
+ type,
584
+ data,
585
+ properties
246
586
  }, {
247
587
  F: __dxlog_file,
248
- L: 482,
249
- S: this,
588
+ L: 867,
589
+ S: void 0,
250
590
  C: (f, a) => f(...a)
251
591
  });
252
- this._registry.set(sourceRx, {
253
- inbound: source.inbound,
254
- outbound: [
255
- ...source.outbound,
256
- edgeArg.target
257
- ]
258
- });
259
- }
260
- const targetRx = this._edges(edgeArg.target);
261
- const target = this._registry.get(targetRx);
262
- if (!target.inbound.includes(edgeArg.source)) {
263
- log("add inbound edge", {
264
- source: edgeArg.source,
265
- target: edgeArg.target
266
- }, {
267
- F: __dxlog_file,
268
- L: 489,
269
- S: this,
270
- C: (f, a) => f(...a)
592
+ const newNode = internal._constructNode({
593
+ id,
594
+ type,
595
+ data,
596
+ properties,
597
+ ...rest
271
598
  });
272
- this._registry.set(targetRx, {
273
- inbound: [
274
- ...target.inbound,
275
- edgeArg.source
276
- ],
277
- outbound: target.outbound
599
+ internal._registry.set(nodeAtom, newNode);
600
+ graph.onNodeChanged.emit({
601
+ id,
602
+ node: newNode
278
603
  });
279
604
  }
605
+ });
606
+ if (nodes) {
607
+ addNodesImpl(graph, nodes);
608
+ const _edges = nodes.map((node) => ({
609
+ source: id,
610
+ target: node.id
611
+ }));
612
+ addEdgesImpl(graph, _edges);
613
+ }
614
+ if (edges) {
615
+ todo();
616
+ }
617
+ return graph;
618
+ };
619
+ function addNode(graphOrNodeArg, nodeArg) {
620
+ if (nodeArg === void 0) {
621
+ const nodeArg2 = graphOrNodeArg;
622
+ return (graph) => addNodeImpl(graph, nodeArg2);
623
+ } else {
624
+ const graph = graphOrNodeArg;
625
+ return addNodeImpl(graph, nodeArg);
626
+ }
627
+ }
628
+ var removeNodesImpl = (graph, ids, edges = false) => {
629
+ Atom2.batch(() => {
630
+ ids.map((id) => removeNodeImpl(graph, id, edges));
631
+ });
632
+ return graph;
633
+ };
634
+ function removeNodes(graphOrIds, idsOrEdges, edges) {
635
+ if (Array.isArray(graphOrIds)) {
636
+ const ids = graphOrIds;
637
+ const edgesArg = typeof idsOrEdges === "boolean" ? idsOrEdges : false;
638
+ return (graph) => removeNodesImpl(graph, ids, edgesArg);
639
+ } else {
640
+ const graph = graphOrIds;
641
+ const ids = idsOrEdges;
642
+ const edgesArg = edges ?? false;
643
+ return removeNodesImpl(graph, ids, edgesArg);
644
+ }
645
+ }
646
+ var removeNodeImpl = (graph, id, edges = false) => {
647
+ const internal = getInternal(graph);
648
+ const nodeAtom = internal._node(id);
649
+ internal._registry.set(nodeAtom, Option.none());
650
+ graph.onNodeChanged.emit({
651
+ id,
652
+ node: Option.none()
653
+ });
654
+ if (edges) {
655
+ const { inbound, outbound } = internal._registry.get(internal._edges(id));
656
+ const edgesToRemove = [
657
+ ...inbound.map((source) => ({
658
+ source,
659
+ target: id
660
+ })),
661
+ ...outbound.map((target) => ({
662
+ source: id,
663
+ target
664
+ }))
665
+ ];
666
+ removeEdgesImpl(graph, edgesToRemove);
667
+ }
668
+ internal._onRemoveNode?.(id);
669
+ return graph;
670
+ };
671
+ function removeNode(graphOrId, idOrEdges, edges) {
672
+ if (typeof graphOrId === "string") {
673
+ const id = graphOrId;
674
+ const edgesArg = typeof idOrEdges === "boolean" ? idOrEdges : false;
675
+ return (graph) => removeNodeImpl(graph, id, edgesArg);
676
+ } else {
677
+ const graph = graphOrId;
678
+ const id = idOrEdges;
679
+ const edgesArg = edges ?? false;
680
+ return removeNodeImpl(graph, id, edgesArg);
681
+ }
682
+ }
683
+ var addEdgesImpl = (graph, edges) => {
684
+ Atom2.batch(() => {
685
+ edges.map((edge) => addEdgeImpl(graph, edge));
686
+ });
687
+ return graph;
688
+ };
689
+ function addEdges(graphOrEdges, edges) {
690
+ if (edges === void 0) {
691
+ const edges2 = graphOrEdges;
692
+ return (graph) => addEdgesImpl(graph, edges2);
693
+ } else {
694
+ const graph = graphOrEdges;
695
+ return addEdgesImpl(graph, edges);
280
696
  }
281
- removeEdges(edges, removeOrphans = false) {
282
- Rx.batch(() => {
283
- edges.map((edge) => this.removeEdge(edge, removeOrphans));
697
+ }
698
+ var addEdgeImpl = (graph, edgeArg) => {
699
+ const internal = getInternal(graph);
700
+ const sourceAtom = internal._edges(edgeArg.source);
701
+ const source = internal._registry.get(sourceAtom);
702
+ if (!source.outbound.includes(edgeArg.target)) {
703
+ log("add outbound edge", {
704
+ source: edgeArg.source,
705
+ target: edgeArg.target
706
+ }, {
707
+ F: __dxlog_file,
708
+ L: 1026,
709
+ S: void 0,
710
+ C: (f, a) => f(...a)
711
+ });
712
+ internal._registry.set(sourceAtom, {
713
+ inbound: source.inbound,
714
+ outbound: [
715
+ ...source.outbound,
716
+ edgeArg.target
717
+ ]
284
718
  });
285
719
  }
286
- removeEdge(edgeArg, removeOrphans = false) {
287
- const sourceRx = this._edges(edgeArg.source);
288
- const source = this._registry.get(sourceRx);
289
- if (source.outbound.includes(edgeArg.target)) {
290
- this._registry.set(sourceRx, {
291
- inbound: source.inbound,
292
- outbound: source.outbound.filter((id) => id !== edgeArg.target)
293
- });
294
- }
295
- const targetRx = this._edges(edgeArg.target);
296
- const target = this._registry.get(targetRx);
297
- if (target.inbound.includes(edgeArg.source)) {
298
- this._registry.set(targetRx, {
299
- inbound: target.inbound.filter((id) => id !== edgeArg.source),
300
- outbound: target.outbound
301
- });
302
- }
303
- if (removeOrphans) {
304
- const source2 = this._registry.get(sourceRx);
305
- const target2 = this._registry.get(targetRx);
306
- if (source2.outbound.length === 0 && source2.inbound.length === 0 && edgeArg.source !== ROOT_ID) {
307
- this.removeNodes([
308
- edgeArg.source
309
- ]);
310
- }
311
- if (target2.outbound.length === 0 && target2.inbound.length === 0 && edgeArg.target !== ROOT_ID) {
312
- this.removeNodes([
313
- edgeArg.target
314
- ]);
315
- }
316
- }
720
+ const targetAtom = internal._edges(edgeArg.target);
721
+ const target = internal._registry.get(targetAtom);
722
+ if (!target.inbound.includes(edgeArg.source)) {
723
+ log("add inbound edge", {
724
+ source: edgeArg.source,
725
+ target: edgeArg.target
726
+ }, {
727
+ F: __dxlog_file,
728
+ L: 1039,
729
+ S: void 0,
730
+ C: (f, a) => f(...a)
731
+ });
732
+ internal._registry.set(targetAtom, {
733
+ inbound: [
734
+ ...target.inbound,
735
+ edgeArg.source
736
+ ],
737
+ outbound: target.outbound
738
+ });
317
739
  }
318
- sortEdges(id, relation, order) {
319
- const edgesRx = this._edges(id);
320
- const edges = this._registry.get(edgesRx);
321
- const unsorted = edges[relation].filter((id2) => !order.includes(id2)) ?? [];
322
- const sorted = order.filter((id2) => edges[relation].includes(id2)) ?? [];
323
- edges[relation].splice(0, edges[relation].length, ...[
324
- ...sorted,
325
- ...unsorted
326
- ]);
327
- this._registry.set(edgesRx, edges);
740
+ return graph;
741
+ };
742
+ function addEdge(graphOrEdgeArg, edgeArg) {
743
+ if (edgeArg === void 0) {
744
+ const edgeArg2 = graphOrEdgeArg;
745
+ return (graph) => addEdgeImpl(graph, edgeArg2);
746
+ } else {
747
+ const graph = graphOrEdgeArg;
748
+ return addEdgeImpl(graph, edgeArg);
328
749
  }
329
- traverse({ visitor, source = ROOT_ID, relation = "outbound" }, path = []) {
330
- if (path.includes(source)) {
331
- return;
332
- }
333
- const node = this.getNodeOrThrow(source);
334
- const shouldContinue = visitor(node, [
335
- ...path,
336
- source
337
- ]);
338
- if (shouldContinue === false) {
339
- return;
340
- }
341
- Object.values(this.getConnections(source, relation)).forEach((child) => this.traverse({
342
- source: child.id,
343
- relation,
344
- visitor
345
- }, [
346
- ...path,
347
- source
348
- ]));
349
- }
350
- getPath({ source = "root", target }) {
351
- return Function.pipe(this.getNode(source), Option.flatMap((node) => {
352
- let found = Option.none();
353
- this.traverse({
354
- source: node.id,
355
- visitor: (node2, path) => {
356
- if (Option.isSome(found)) {
357
- return false;
358
- }
359
- if (node2.id === target) {
360
- found = Option.some(path);
361
- }
362
- }
363
- });
364
- return found;
365
- }));
750
+ }
751
+ var removeEdgesImpl = (graph, edges, removeOrphans = false) => {
752
+ Atom2.batch(() => {
753
+ edges.map((edge) => removeEdgeImpl(graph, edge, removeOrphans));
754
+ });
755
+ return graph;
756
+ };
757
+ function removeEdges(graphOrEdges, edgesOrRemoveOrphans, removeOrphans) {
758
+ if (Array.isArray(graphOrEdges)) {
759
+ const edges = graphOrEdges;
760
+ const removeOrphansArg = typeof edgesOrRemoveOrphans === "boolean" ? edgesOrRemoveOrphans : false;
761
+ return (graph) => removeEdgesImpl(graph, edges, removeOrphansArg);
762
+ } else {
763
+ const graph = graphOrEdges;
764
+ const edges = edgesOrRemoveOrphans;
765
+ const removeOrphansArg = removeOrphans ?? false;
766
+ return removeEdgesImpl(graph, edges, removeOrphansArg);
366
767
  }
367
- async waitForPath(params, { timeout = 5e3, interval = 500 } = {}) {
368
- const path = this.getPath(params);
369
- if (Option.isSome(path)) {
370
- return path.value;
371
- }
372
- const trigger = new Trigger();
373
- const i = setInterval(() => {
374
- const path2 = this.getPath(params);
375
- if (Option.isSome(path2)) {
376
- trigger.wake(path2.value);
377
- }
378
- }, interval);
379
- return trigger.wait({
380
- timeout
381
- }).finally(() => clearInterval(i));
768
+ }
769
+ var removeEdgeImpl = (graph, edgeArg, removeOrphans = false) => {
770
+ const internal = getInternal(graph);
771
+ const sourceAtom = internal._edges(edgeArg.source);
772
+ const source = internal._registry.get(sourceAtom);
773
+ if (source.outbound.includes(edgeArg.target)) {
774
+ internal._registry.set(sourceAtom, {
775
+ inbound: source.inbound,
776
+ outbound: source.outbound.filter((id) => id !== edgeArg.target)
777
+ });
382
778
  }
383
- /** @internal */
384
- _constructNode(node) {
385
- return Option.some({
386
- [graphSymbol]: this,
387
- data: null,
388
- properties: {},
389
- ...node
779
+ const targetAtom = internal._edges(edgeArg.target);
780
+ const target = internal._registry.get(targetAtom);
781
+ if (target.inbound.includes(edgeArg.source)) {
782
+ internal._registry.set(targetAtom, {
783
+ inbound: target.inbound.filter((id) => id !== edgeArg.source),
784
+ outbound: target.outbound
390
785
  });
391
786
  }
392
- constructor({ registry, nodes, edges, onInitialize, onExpand, onRemoveNode } = {}) {
393
- _define_property(this, "onNodeChanged", new Event());
394
- _define_property(this, "_onExpand", void 0);
395
- _define_property(this, "_onInitialize", void 0);
396
- _define_property(this, "_onRemoveNode", void 0);
397
- _define_property(this, "_registry", void 0);
398
- _define_property(this, "_expanded", Record.empty());
399
- _define_property(this, "_initialized", Record.empty());
400
- _define_property(this, "_initialEdges", Record.empty());
401
- _define_property(this, "_initialNodes", Record.fromEntries([
402
- [
403
- ROOT_ID,
404
- this._constructNode({
405
- id: ROOT_ID,
406
- type: ROOT_TYPE,
407
- data: null,
408
- properties: {}
409
- })
410
- ]
411
- ]));
412
- _define_property(this, "_node", Rx.family((id) => {
413
- const initial = Option.flatten(Record.get(this._initialNodes, id));
414
- return Rx.make(initial).pipe(Rx.keepAlive, Rx.withLabel(`graph:node:${id}`));
415
- }));
416
- _define_property(this, "_nodeOrThrow", Rx.family((id) => {
417
- return Rx.make((get2) => {
418
- const node = get2(this._node(id));
419
- invariant(Option.isSome(node), `Node not available: ${id}`, {
420
- F: __dxlog_file,
421
- L: 254,
422
- S: this,
423
- A: [
424
- "Option.isSome(node)",
425
- "`Node not available: ${id}`"
426
- ]
427
- });
428
- return node.value;
429
- });
430
- }));
431
- _define_property(this, "_edges", Rx.family((id) => {
432
- const initial = Record.get(this._initialEdges, id).pipe(Option.getOrElse(() => ({
433
- inbound: [],
434
- outbound: []
435
- })));
436
- return Rx.make(initial).pipe(Rx.keepAlive, Rx.withLabel(`graph:edges:${id}`));
437
- }));
438
- _define_property(this, "_connections", Rx.family((key) => {
439
- return Rx.make((get2) => {
440
- const [id, relation] = key.split("$");
441
- const edges2 = get2(this._edges(id));
442
- return edges2[relation].map((id2) => get2(this._node(id2))).filter(Option.isSome).map((o) => o.value);
443
- }).pipe(Rx.withLabel(`graph:connections:${key}`));
444
- }));
445
- _define_property(this, "_actions", Rx.family((id) => {
446
- return Rx.make((get2) => {
447
- return get2(this._connections(`${id}$outbound`)).filter((node) => node.type === ACTION_TYPE || node.type === ACTION_GROUP_TYPE);
448
- }).pipe(Rx.withLabel(`graph:actions:${id}`));
449
- }));
450
- _define_property(this, "_json", Rx.family((id) => {
451
- return Rx.make((get2) => {
452
- const toJSON = (node, seen = []) => {
453
- const nodes2 = get2(this.connections(node.id));
454
- const obj = {
455
- id: node.id,
456
- type: node.type
457
- };
458
- if (node.properties.label) {
459
- obj.label = node.properties.label;
460
- }
461
- if (nodes2.length) {
462
- obj.nodes = nodes2.map((n) => {
463
- const nextSeen = [
464
- ...seen,
465
- node.id
466
- ];
467
- return nextSeen.includes(n.id) ? void 0 : toJSON(n, nextSeen);
468
- }).filter(isNonNullable);
469
- }
470
- return obj;
471
- };
472
- const root = get2(this.nodeOrThrow(id));
473
- return toJSON(root);
474
- }).pipe(Rx.withLabel(`graph:json:${id}`));
475
- }));
476
- this._registry = registry ?? Registry.make();
477
- this._onInitialize = onInitialize;
478
- this._onExpand = onExpand;
479
- this._onRemoveNode = onRemoveNode;
480
- if (nodes) {
481
- nodes.forEach((node) => {
482
- Record.set(this._initialNodes, node.id, this._constructNode(node));
483
- });
787
+ if (removeOrphans) {
788
+ const source2 = internal._registry.get(sourceAtom);
789
+ const target2 = internal._registry.get(targetAtom);
790
+ if (source2.outbound.length === 0 && source2.inbound.length === 0 && edgeArg.source !== RootId) {
791
+ removeNodesImpl(graph, [
792
+ edgeArg.source
793
+ ]);
484
794
  }
485
- if (edges) {
486
- Object.entries(edges).forEach(([source, edges2]) => {
487
- Record.set(this._initialEdges, source, edges2);
488
- });
795
+ if (target2.outbound.length === 0 && target2.inbound.length === 0 && edgeArg.target !== RootId) {
796
+ removeNodesImpl(graph, [
797
+ edgeArg.target
798
+ ]);
489
799
  }
490
800
  }
801
+ return graph;
802
+ };
803
+ function removeEdge(graphOrEdgeArg, edgeArgOrRemoveOrphans, removeOrphans) {
804
+ if (edgeArgOrRemoveOrphans === void 0 || typeof edgeArgOrRemoveOrphans === "boolean" || "source" in graphOrEdgeArg) {
805
+ const edgeArg = graphOrEdgeArg;
806
+ const removeOrphansArg = typeof edgeArgOrRemoveOrphans === "boolean" ? edgeArgOrRemoveOrphans : false;
807
+ return (graph) => removeEdgeImpl(graph, edgeArg, removeOrphansArg);
808
+ } else {
809
+ const graph = graphOrEdgeArg;
810
+ const edgeArg = edgeArgOrRemoveOrphans;
811
+ const removeOrphansArg = removeOrphans ?? false;
812
+ return removeEdgeImpl(graph, edgeArg, removeOrphansArg);
813
+ }
814
+ }
815
+ var make = (params) => {
816
+ return new GraphImpl(params);
491
817
  };
492
818
 
493
819
  // src/graph-builder.ts
494
- import { Registry as Registry2, Rx as Rx2 } from "@effect-rx/rx-react";
495
- import { effect } from "@preact/signals-core";
496
- import * as Array from "effect/Array";
820
+ var graph_builder_exports = {};
821
+ __export(graph_builder_exports, {
822
+ GraphBuilderTypeId: () => GraphBuilderTypeId,
823
+ addExtension: () => addExtension,
824
+ createConnector: () => createConnector,
825
+ createExtension: () => createExtension,
826
+ createExtensionRaw: () => createExtensionRaw,
827
+ createTypeExtension: () => createTypeExtension,
828
+ destroy: () => destroy,
829
+ explore: () => explore,
830
+ flattenExtensions: () => flattenExtensions,
831
+ from: () => from,
832
+ make: () => make2,
833
+ removeExtension: () => removeExtension
834
+ });
835
+ import { Atom as Atom3, Registry as Registry2 } from "@effect-atom/atom-react";
836
+ import * as Array2 from "effect/Array";
837
+ import * as Effect from "effect/Effect";
497
838
  import * as Function2 from "effect/Function";
498
- import * as Option2 from "effect/Option";
839
+ import * as Option3 from "effect/Option";
840
+ import * as Pipeable2 from "effect/Pipeable";
499
841
  import * as Record2 from "effect/Record";
500
842
  import { log as log2 } from "@dxos/log";
501
843
  import { byPosition, getDebugName, isNode, isNonNullable as isNonNullable2 } from "@dxos/util";
502
844
 
503
- // src/node.ts
504
- var isGraphNode = (data) => data && typeof data === "object" && "id" in data && "properties" in data && data.properties ? typeof data.properties === "object" && "data" in data : false;
505
- var isAction = (data) => isGraphNode(data) ? typeof data.data === "function" && data.type === ACTION_TYPE : false;
506
- var actionGroupSymbol = Symbol("ActionGroup");
507
- var isActionGroup = (data) => isGraphNode(data) ? data.data === actionGroupSymbol && data.type === ACTION_GROUP_TYPE : false;
508
- var isActionLike = (data) => isAction(data) || isActionGroup(data);
509
-
510
- // src/graph-builder.ts
511
- function _define_property2(obj, key, value) {
512
- if (key in obj) {
513
- Object.defineProperty(obj, key, {
514
- value,
515
- enumerable: true,
516
- configurable: true,
517
- writable: true
518
- });
519
- } else {
520
- obj[key] = value;
845
+ // src/node-matcher.ts
846
+ var node_matcher_exports = {};
847
+ __export(node_matcher_exports, {
848
+ whenAll: () => whenAll,
849
+ whenAny: () => whenAny,
850
+ whenEchoObject: () => whenEchoObject,
851
+ whenEchoObjectMatches: () => whenEchoObjectMatches,
852
+ whenEchoType: () => whenEchoType,
853
+ whenEchoTypeMatches: () => whenEchoTypeMatches,
854
+ whenId: () => whenId,
855
+ whenNodeType: () => whenNodeType,
856
+ whenNot: () => whenNot,
857
+ whenRoot: () => whenRoot
858
+ });
859
+ import * as Option2 from "effect/Option";
860
+ import { Obj } from "@dxos/echo";
861
+ var whenRoot = (node) => node.id === RootId ? Option2.some(node) : Option2.none();
862
+ var whenId = (id) => (node) => node.id === id ? Option2.some(node) : Option2.none();
863
+ var whenNodeType = (type) => (node) => node.type === type ? Option2.some(node) : Option2.none();
864
+ var whenEchoType = (type) => (node) => Obj.instanceOf(type, node.data) ? Option2.some(node.data) : Option2.none();
865
+ var whenEchoObject = (node) => Obj.isObject(node.data) ? Option2.some(node.data) : Option2.none();
866
+ var whenAll = (...matchers) => (node) => {
867
+ for (const matcher of matchers) {
868
+ const result = matcher(node);
869
+ if (Option2.isNone(result)) {
870
+ return Option2.none();
871
+ }
521
872
  }
522
- return obj;
523
- }
524
- var __dxlog_file2 = "/__w/dxos/dxos/packages/sdk/app-graph/src/graph-builder.ts";
525
- var createExtension = (extension) => {
526
- const { id, position = "static", relation = "outbound", resolver: _resolver, connector: _connector, actions: _actions, actionGroups: _actionGroups } = extension;
527
- const getId = (key) => `${id}/${key}`;
528
- const resolver = _resolver && Rx2.family((id2) => _resolver(id2).pipe(Rx2.withLabel(`graph-builder:_resolver:${id2}`)));
529
- const connector = _connector && Rx2.family((node) => _connector(node).pipe(Rx2.withLabel(`graph-builder:_connector:${id}`)));
530
- const actionGroups = _actionGroups && Rx2.family((node) => _actionGroups(node).pipe(Rx2.withLabel(`graph-builder:_actionGroups:${id}`)));
531
- const actions = _actions && Rx2.family((node) => _actions(node).pipe(Rx2.withLabel(`graph-builder:_actions:${id}`)));
532
- return [
533
- resolver ? {
534
- id: getId("resolver"),
535
- position,
536
- resolver
537
- } : void 0,
538
- connector ? {
539
- id: getId("connector"),
540
- position,
541
- relation,
542
- connector: Rx2.family((node) => Rx2.make((get2) => {
543
- try {
544
- return get2(connector(node));
545
- } catch {
546
- log2.warn("Error in connector", {
547
- id: getId("connector"),
548
- node
549
- }, {
550
- F: __dxlog_file2,
551
- L: 112,
552
- S: void 0,
553
- C: (f, a) => f(...a)
554
- });
555
- return [];
556
- }
557
- }).pipe(Rx2.withLabel(`graph-builder:connector:${id}`)))
558
- } : void 0,
559
- actionGroups ? {
560
- id: getId("actionGroups"),
561
- position,
562
- relation: "outbound",
563
- connector: Rx2.family((node) => Rx2.make((get2) => {
564
- try {
565
- return get2(actionGroups(node)).map((arg) => ({
566
- ...arg,
567
- data: actionGroupSymbol,
568
- type: ACTION_GROUP_TYPE
569
- }));
570
- } catch {
571
- log2.warn("Error in actionGroups", {
572
- id: getId("actionGroups"),
573
- node
574
- }, {
575
- F: __dxlog_file2,
576
- L: 133,
577
- S: void 0,
578
- C: (f, a) => f(...a)
579
- });
580
- return [];
581
- }
582
- }).pipe(Rx2.withLabel(`graph-builder:connector:actionGroups:${id}`)))
583
- } : void 0,
584
- actions ? {
585
- id: getId("actions"),
586
- position,
587
- relation: "outbound",
588
- connector: Rx2.family((node) => Rx2.make((get2) => {
589
- try {
590
- return get2(actions(node)).map((arg) => ({
591
- ...arg,
592
- type: ACTION_TYPE
593
- }));
594
- } catch {
595
- log2.warn("Error in actions", {
596
- id: getId("actions"),
597
- node
598
- }, {
599
- F: __dxlog_file2,
600
- L: 150,
601
- S: void 0,
602
- C: (f, a) => f(...a)
603
- });
604
- return [];
605
- }
606
- }).pipe(Rx2.withLabel(`graph-builder:connector:actions:${id}`)))
607
- } : void 0
608
- ].filter(isNonNullable2);
873
+ return Option2.some(node);
609
874
  };
610
- var flattenExtensions = (extension, acc = []) => {
611
- if (Array.isArray(extension)) {
612
- return [
613
- ...acc,
614
- ...extension.flatMap((ext) => flattenExtensions(ext, acc))
615
- ];
616
- } else {
617
- return [
618
- ...acc,
619
- extension
620
- ];
875
+ var whenAny = (...matchers) => (node) => {
876
+ for (const matcher of matchers) {
877
+ const result = matcher(node);
878
+ if (Option2.isSome(result)) {
879
+ return Option2.some(node);
880
+ }
621
881
  }
882
+ return Option2.none();
622
883
  };
623
- var GraphBuilder = class _GraphBuilder {
624
- static from(pickle, registry) {
625
- if (!pickle) {
626
- return new _GraphBuilder({
627
- registry
628
- });
629
- }
630
- const { nodes, edges } = JSON.parse(pickle);
631
- return new _GraphBuilder({
632
- nodes,
633
- edges,
634
- registry
884
+ var whenEchoTypeMatches = (type) => (node) => Obj.instanceOf(type, node.data) ? Option2.some(node) : Option2.none();
885
+ var whenEchoObjectMatches = (node) => Obj.isObject(node.data) ? Option2.some(node) : Option2.none();
886
+ var whenNot = (matcher) => (node) => Option2.isNone(matcher(node)) ? Option2.some(node) : Option2.none();
887
+
888
+ // src/graph-builder.ts
889
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/sdk/app-graph/src/graph-builder.ts";
890
+ var GraphBuilderTypeId = Symbol.for("@dxos/app-graph/GraphBuilder");
891
+ var GraphBuilderImpl = class {
892
+ [GraphBuilderTypeId] = GraphBuilderTypeId;
893
+ pipe() {
894
+ return Pipeable2.pipeArguments(this, arguments);
895
+ }
896
+ // TODO(wittjosiah): Use Context.
897
+ _subscriptions = /* @__PURE__ */ new Map();
898
+ _extensions = Atom3.make(Record2.empty()).pipe(Atom3.keepAlive, Atom3.withLabel("graph-builder:extensions"));
899
+ _initialized = {};
900
+ _registry;
901
+ _graph;
902
+ constructor({ registry, ...params } = {}) {
903
+ this._registry = registry ?? Registry2.make();
904
+ const graph = make({
905
+ ...params,
906
+ registry: this._registry,
907
+ onExpand: (id, relation) => this._onExpand(id, relation),
908
+ onInitialize: (id) => this._onInitialize(id),
909
+ onRemoveNode: (id) => this._onRemoveNode(id)
635
910
  });
911
+ this._graph = graph;
636
912
  }
637
913
  get graph() {
638
914
  return this._graph;
@@ -640,56 +916,27 @@ var GraphBuilder = class _GraphBuilder {
640
916
  get extensions() {
641
917
  return this._extensions;
642
918
  }
643
- addExtension(extensions) {
644
- flattenExtensions(extensions).forEach((extension) => {
645
- const extensions2 = this._registry.get(this._extensions);
646
- this._registry.set(this._extensions, Record2.set(extensions2, extension.id, extension));
919
+ _resolvers = Atom3.family((id) => {
920
+ return Atom3.make((get2) => {
921
+ 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);
647
922
  });
648
- return this;
649
- }
650
- removeExtension(id) {
651
- const extensions = this._registry.get(this._extensions);
652
- this._registry.set(this._extensions, Record2.remove(extensions, id));
653
- return this;
654
- }
655
- async explore({ registry = Registry2.make(), source = ROOT_ID, relation = "outbound", visitor }, path = []) {
656
- if (path.includes(source)) {
657
- return;
658
- }
659
- if (!isNode()) {
660
- const { yieldOrContinue } = await import("main-thread-scheduling");
661
- await yieldOrContinue("idle");
662
- }
663
- const node = registry.get(this._graph.nodeOrThrow(source));
664
- const shouldContinue = await visitor(node, [
665
- ...path,
666
- node.id
667
- ]);
668
- if (shouldContinue === false) {
669
- return;
670
- }
671
- 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))));
672
- await Promise.all(nodes.map((nodeArg) => {
673
- registry.set(this._graph._node(nodeArg.id), this._graph._constructNode(nodeArg));
674
- return this.explore({
675
- registry,
676
- source: nodeArg.id,
677
- relation,
678
- visitor
679
- }, [
680
- ...path,
681
- node.id
682
- ]);
683
- }));
684
- if (registry !== this._registry) {
685
- registry.reset();
686
- registry.dispose();
687
- }
688
- }
689
- destroy() {
690
- this._subscriptions.forEach((unsubscribe) => unsubscribe());
691
- this._subscriptions.clear();
692
- }
923
+ });
924
+ _connectors = Atom3.family((key) => {
925
+ return Atom3.make((get2) => {
926
+ const [id, relation] = key.split("+");
927
+ const node = this._graph.node(id);
928
+ return Function2.pipe(
929
+ get2(this._extensions),
930
+ Record2.values,
931
+ // TODO(wittjosiah): Sort on write rather than read.
932
+ Array2.sortBy(byPosition),
933
+ Array2.filter(({ relation: _relation = "outbound" }) => _relation === relation),
934
+ Array2.map(({ connector }) => connector?.(node)),
935
+ Array2.filter(isNonNullable2),
936
+ Array2.flatMap((result) => get2(result))
937
+ );
938
+ }).pipe(Atom3.withLabel(`graph-builder:connectors:${key}`));
939
+ });
693
940
  _onExpand(id, relation) {
694
941
  log2("onExpand", {
695
942
  id,
@@ -697,7 +944,7 @@ var GraphBuilder = class _GraphBuilder {
697
944
  registry: getDebugName(this._registry)
698
945
  }, {
699
946
  F: __dxlog_file2,
700
- L: 327,
947
+ L: 176,
701
948
  S: this,
702
949
  C: (f, a) => f(...a)
703
950
  });
@@ -714,25 +961,25 @@ var GraphBuilder = class _GraphBuilder {
714
961
  removed
715
962
  }, {
716
963
  F: __dxlog_file2,
717
- L: 338,
964
+ L: 187,
718
965
  S: this,
719
966
  C: (f, a) => f(...a)
720
967
  });
721
968
  const update = () => {
722
- Rx2.batch(() => {
723
- this._graph.removeEdges(removed.map((target) => ({
969
+ Atom3.batch(() => {
970
+ removeEdges(this._graph, removed.map((target) => ({
724
971
  source: id,
725
972
  target
726
973
  })), true);
727
- this._graph.addNodes(nodes);
728
- this._graph.addEdges(nodes.map((node) => relation === "outbound" ? {
974
+ addNodes(this._graph, nodes);
975
+ addEdges(this._graph, nodes.map((node) => relation === "outbound" ? {
729
976
  source: id,
730
977
  target: node.id
731
978
  } : {
732
979
  source: node.id,
733
980
  target: id
734
981
  }));
735
- this._graph.sortEdges(id, relation, nodes.map(({ id: id2 }) => id2));
982
+ sortEdges(this._graph, id, relation, nodes.map(({ id: id2 }) => id2));
736
983
  });
737
984
  };
738
985
  if (typeof requestAnimationFrame === "function") {
@@ -751,23 +998,23 @@ var GraphBuilder = class _GraphBuilder {
751
998
  id
752
999
  }, {
753
1000
  F: __dxlog_file2,
754
- L: 375,
1001
+ L: 227,
755
1002
  S: this,
756
1003
  C: (f, a) => f(...a)
757
1004
  });
758
1005
  const resolver = this._resolvers(id);
759
1006
  const cancel = this._registry.subscribe(resolver, (node) => {
760
1007
  const trigger = this._initialized[id];
761
- Option2.match(node, {
1008
+ Option3.match(node, {
762
1009
  onSome: (node2) => {
763
- this._graph.addNodes([
1010
+ addNodes(this._graph, [
764
1011
  node2
765
1012
  ]);
766
1013
  trigger?.wake();
767
1014
  },
768
1015
  onNone: () => {
769
1016
  trigger?.wake();
770
- this._graph.removeNodes([
1017
+ removeNodes(this._graph, [
771
1018
  id
772
1019
  ]);
773
1020
  }
@@ -781,78 +1028,270 @@ var GraphBuilder = class _GraphBuilder {
781
1028
  this._subscriptions.get(id)?.();
782
1029
  this._subscriptions.delete(id);
783
1030
  }
784
- constructor({ registry, ...params } = {}) {
785
- _define_property2(this, "_subscriptions", /* @__PURE__ */ new Map());
786
- _define_property2(this, "_extensions", Rx2.make(Record2.empty()).pipe(Rx2.keepAlive, Rx2.withLabel("graph-builder:extensions")));
787
- _define_property2(this, "_initialized", {});
788
- _define_property2(this, "_registry", void 0);
789
- _define_property2(this, "_graph", void 0);
790
- _define_property2(this, "_resolvers", Rx2.family((id) => {
791
- return Rx2.make((get2) => {
792
- 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);
793
- });
794
- }));
795
- _define_property2(this, "_connectors", Rx2.family((key) => {
796
- return Rx2.make((get2) => {
797
- const [id, relation] = key.split("+");
798
- const node = this._graph.node(id);
799
- return Function2.pipe(
800
- get2(this._extensions),
801
- Record2.values,
802
- // TODO(wittjosiah): Sort on write rather than read.
803
- Array.sortBy(byPosition),
804
- Array.filter(({ relation: _relation = "outbound" }) => _relation === relation),
805
- Array.map(({ connector }) => connector?.(node)),
806
- Array.filter(isNonNullable2),
807
- Array.flatMap((result) => get2(result))
808
- );
809
- }).pipe(Rx2.withLabel(`graph-builder:connectors:${key}`));
810
- }));
811
- this._registry = registry ?? Registry2.make();
812
- this._graph = new Graph({
813
- ...params,
814
- registry: this._registry,
815
- onExpand: (id, relation) => this._onExpand(id, relation),
816
- onInitialize: (id) => this._onInitialize(id),
817
- onRemoveNode: (id) => this._onRemoveNode(id)
1031
+ };
1032
+ var make2 = (params) => {
1033
+ return new GraphBuilderImpl(params);
1034
+ };
1035
+ var from = (pickle, registry) => {
1036
+ if (!pickle) {
1037
+ return make2({
1038
+ registry
818
1039
  });
819
1040
  }
1041
+ const { nodes, edges } = JSON.parse(pickle);
1042
+ return make2({
1043
+ nodes,
1044
+ edges,
1045
+ registry
1046
+ });
820
1047
  };
821
- var rxFromSignal = (cb) => {
822
- return Rx2.make((get2) => {
823
- const dispose = effect(() => {
824
- get2.setSelf(cb());
825
- });
826
- get2.addFinalizer(() => dispose());
827
- return cb();
1048
+ var addExtensionImpl = (builder, extensions) => {
1049
+ const internal = builder;
1050
+ flattenExtensions(extensions).forEach((extension) => {
1051
+ const extensions2 = internal._registry.get(internal._extensions);
1052
+ internal._registry.set(internal._extensions, Record2.set(extensions2, extension.id, extension));
828
1053
  });
1054
+ return builder;
829
1055
  };
830
- var observableFamily = Rx2.family((observable) => {
831
- return Rx2.make((get2) => {
832
- const subscription = observable.subscribe((value) => get2.setSelf(value));
833
- get2.addFinalizer(() => subscription.unsubscribe());
834
- return observable.get();
1056
+ function addExtension(builderOrExtensions, extensions) {
1057
+ if (extensions === void 0) {
1058
+ const extensions2 = builderOrExtensions;
1059
+ return (builder) => addExtensionImpl(builder, extensions2);
1060
+ } else {
1061
+ const builder = builderOrExtensions;
1062
+ return addExtensionImpl(builder, extensions);
1063
+ }
1064
+ }
1065
+ var removeExtensionImpl = (builder, id) => {
1066
+ const internal = builder;
1067
+ const extensions = internal._registry.get(internal._extensions);
1068
+ internal._registry.set(internal._extensions, Record2.remove(extensions, id));
1069
+ return builder;
1070
+ };
1071
+ function removeExtension(builderOrId, id) {
1072
+ if (typeof builderOrId === "string") {
1073
+ const id2 = builderOrId;
1074
+ return (builder) => removeExtensionImpl(builder, id2);
1075
+ } else {
1076
+ const builder = builderOrId;
1077
+ return removeExtensionImpl(builder, id);
1078
+ }
1079
+ }
1080
+ var exploreImpl = async (builder, options, path = []) => {
1081
+ const internal = builder;
1082
+ const { registry = Registry2.make(), source = RootId, relation = "outbound", visitor } = options;
1083
+ if (path.includes(source)) {
1084
+ return;
1085
+ }
1086
+ if (!isNode()) {
1087
+ const { yieldOrContinue } = await import("main-thread-scheduling");
1088
+ await yieldOrContinue("idle");
1089
+ }
1090
+ const node = registry.get(internal._graph.nodeOrThrow(source));
1091
+ const shouldContinue = await visitor(node, [
1092
+ ...path,
1093
+ node.id
1094
+ ]);
1095
+ if (shouldContinue === false) {
1096
+ return;
1097
+ }
1098
+ const nodes = Object.values(internal._registry.get(internal._extensions)).filter((extension) => relation === (extension.relation ?? "outbound")).map((extension) => extension.connector).filter(isNonNullable2).flatMap((connector) => registry.get(connector(internal._graph.node(source))));
1099
+ await Promise.all(nodes.map((nodeArg) => {
1100
+ registry.set(internal._graph._node(nodeArg.id), internal._graph._constructNode(nodeArg));
1101
+ return exploreImpl(builder, {
1102
+ registry,
1103
+ source: nodeArg.id,
1104
+ relation,
1105
+ visitor
1106
+ }, [
1107
+ ...path,
1108
+ node.id
1109
+ ]);
1110
+ }));
1111
+ if (registry !== internal._registry) {
1112
+ registry.reset();
1113
+ registry.dispose();
1114
+ }
1115
+ };
1116
+ function explore(builderOrOptions, optionsOrPath, path) {
1117
+ if (typeof builderOrOptions === "object" && "visitor" in builderOrOptions) {
1118
+ const options = builderOrOptions;
1119
+ const path2 = Array2.isArray(optionsOrPath) ? optionsOrPath : void 0;
1120
+ return (builder) => exploreImpl(builder, options, path2);
1121
+ } else {
1122
+ const builder = builderOrOptions;
1123
+ const options = optionsOrPath;
1124
+ const pathArg = path ?? (Array2.isArray(optionsOrPath) ? optionsOrPath : void 0);
1125
+ return exploreImpl(builder, options, pathArg);
1126
+ }
1127
+ }
1128
+ var destroyImpl = (builder) => {
1129
+ const internal = builder;
1130
+ internal._subscriptions.forEach((unsubscribe) => unsubscribe());
1131
+ internal._subscriptions.clear();
1132
+ };
1133
+ function destroy(builder) {
1134
+ if (builder === void 0) {
1135
+ return (builder2) => destroyImpl(builder2);
1136
+ } else {
1137
+ return destroyImpl(builder);
1138
+ }
1139
+ }
1140
+ var createExtensionRaw = (extension) => {
1141
+ const { id, position = "static", relation = "outbound", resolver: _resolver, connector: _connector, actions: _actions, actionGroups: _actionGroups } = extension;
1142
+ const getId = (key) => `${id}/${key}`;
1143
+ const resolver = _resolver && Atom3.family((id2) => _resolver(id2).pipe(Atom3.withLabel(`graph-builder:_resolver:${id2}`)));
1144
+ const connector = _connector && Atom3.family((node) => _connector(node).pipe(Atom3.withLabel(`graph-builder:_connector:${id}`)));
1145
+ const actionGroups = _actionGroups && Atom3.family((node) => _actionGroups(node).pipe(Atom3.withLabel(`graph-builder:_actionGroups:${id}`)));
1146
+ const actions = _actions && Atom3.family((node) => _actions(node).pipe(Atom3.withLabel(`graph-builder:_actions:${id}`)));
1147
+ return [
1148
+ resolver ? {
1149
+ id: getId("resolver"),
1150
+ position,
1151
+ resolver
1152
+ } : void 0,
1153
+ connector ? {
1154
+ id: getId("connector"),
1155
+ position,
1156
+ relation,
1157
+ connector: Atom3.family((node) => Atom3.make((get2) => {
1158
+ try {
1159
+ return get2(connector(node));
1160
+ } catch (error) {
1161
+ log2.warn("Error in connector", {
1162
+ id: getId("connector"),
1163
+ node,
1164
+ error
1165
+ }, {
1166
+ F: __dxlog_file2,
1167
+ L: 509,
1168
+ S: void 0,
1169
+ C: (f, a) => f(...a)
1170
+ });
1171
+ return [];
1172
+ }
1173
+ }).pipe(Atom3.withLabel(`graph-builder:connector:${id}`)))
1174
+ } : void 0,
1175
+ actionGroups ? {
1176
+ id: getId("actionGroups"),
1177
+ position,
1178
+ relation: "outbound",
1179
+ connector: Atom3.family((node) => Atom3.make((get2) => {
1180
+ try {
1181
+ return get2(actionGroups(node)).map((arg) => ({
1182
+ ...arg,
1183
+ data: actionGroupSymbol,
1184
+ type: ActionGroupType
1185
+ }));
1186
+ } catch (error) {
1187
+ log2.warn("Error in actionGroups", {
1188
+ id: getId("actionGroups"),
1189
+ node,
1190
+ error
1191
+ }, {
1192
+ F: __dxlog_file2,
1193
+ L: 530,
1194
+ S: void 0,
1195
+ C: (f, a) => f(...a)
1196
+ });
1197
+ return [];
1198
+ }
1199
+ }).pipe(Atom3.withLabel(`graph-builder:connector:actionGroups:${id}`)))
1200
+ } : void 0,
1201
+ actions ? {
1202
+ id: getId("actions"),
1203
+ position,
1204
+ relation: "outbound",
1205
+ connector: Atom3.family((node) => Atom3.make((get2) => {
1206
+ try {
1207
+ return get2(actions(node)).map((arg) => ({
1208
+ ...arg,
1209
+ type: ActionType
1210
+ }));
1211
+ } catch (error) {
1212
+ log2.warn("Error in actions", {
1213
+ id: getId("actions"),
1214
+ node,
1215
+ error
1216
+ }, {
1217
+ F: __dxlog_file2,
1218
+ L: 547,
1219
+ S: void 0,
1220
+ C: (f, a) => f(...a)
1221
+ });
1222
+ return [];
1223
+ }
1224
+ }).pipe(Atom3.withLabel(`graph-builder:connector:actions:${id}`)))
1225
+ } : void 0
1226
+ ].filter(isNonNullable2);
1227
+ };
1228
+ var runEffectSyncWithFallback = (effect, context2, extensionId, fallback) => {
1229
+ return Effect.runSync(effect.pipe(Effect.provide(context2), Effect.catchAll((error) => {
1230
+ log2.warn("Extension failed", {
1231
+ extension: extensionId,
1232
+ error
1233
+ }, {
1234
+ F: __dxlog_file2,
1235
+ L: 590,
1236
+ S: void 0,
1237
+ C: (f, a) => f(...a)
1238
+ });
1239
+ return Effect.succeed(fallback);
1240
+ })));
1241
+ };
1242
+ var createExtension = (options) => Effect.map(Effect.context(), (context2) => {
1243
+ const { id, match: match3, actions, connector, resolver, relation, position } = options;
1244
+ const connectorExtension = connector ? createConnectorWithRuntime(id, match3, connector, context2) : void 0;
1245
+ 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) => ({
1246
+ ...action,
1247
+ // Attach captured context for action execution.
1248
+ _actionContext: context2
1249
+ }))), Option3.getOrElse(() => []))) : void 0;
1250
+ const resolverExtension = resolver ? (nodeId) => Atom3.make((get2) => runEffectSyncWithFallback(resolver(nodeId, get2), context2, id, null) ?? null) : void 0;
1251
+ return createExtensionRaw({
1252
+ id,
1253
+ relation,
1254
+ position,
1255
+ connector: connectorExtension,
1256
+ actions: actionsExtension,
1257
+ resolver: resolverExtension
835
1258
  });
836
1259
  });
837
- var rxFromObservable = (observable) => {
838
- return observableFamily(observable);
1260
+ var createConnector = (matcher, factory) => {
1261
+ return (node) => Atom3.make((get2) => Function2.pipe(get2(node), Option3.flatMap(matcher), Option3.map((data) => factory(data, get2)), Option3.getOrElse(() => [])));
1262
+ };
1263
+ var createConnectorWithRuntime = (extensionId, matcher, factory, context2) => {
1264
+ return (node) => Atom3.make((get2) => Function2.pipe(get2(node), Option3.flatMap(matcher), Option3.map((data) => runEffectSyncWithFallback(factory(data, get2), context2, extensionId, [])), Option3.getOrElse(() => [])));
1265
+ };
1266
+ var createTypeExtension = (options) => {
1267
+ const { id, type, actions, connector, relation, position } = options;
1268
+ return createExtension({
1269
+ id,
1270
+ match: whenEchoType(type),
1271
+ actions,
1272
+ connector,
1273
+ relation,
1274
+ position
1275
+ });
1276
+ };
1277
+ var flattenExtensions = (extension, acc = []) => {
1278
+ if (Array2.isArray(extension)) {
1279
+ return [
1280
+ ...acc,
1281
+ ...extension.flatMap((ext) => flattenExtensions(ext, acc))
1282
+ ];
1283
+ } else {
1284
+ return [
1285
+ ...acc,
1286
+ extension
1287
+ ];
1288
+ }
839
1289
  };
840
1290
  export {
841
- ACTION_GROUP_TYPE,
842
- ACTION_TYPE,
843
- Graph,
844
- GraphBuilder,
845
- ROOT_ID,
846
- ROOT_TYPE,
847
- actionGroupSymbol,
848
- createExtension,
849
- flattenExtensions,
850
- getGraph,
851
- isAction,
852
- isActionGroup,
853
- isActionLike,
854
- isGraphNode,
855
- rxFromObservable,
856
- rxFromSignal
1291
+ atoms_exports as CreateAtom,
1292
+ graph_exports as Graph,
1293
+ graph_builder_exports as GraphBuilder,
1294
+ node_exports as Node,
1295
+ node_matcher_exports as NodeMatcher
857
1296
  };
858
1297
  //# sourceMappingURL=index.mjs.map