@dxos/app-graph 0.8.4-main.fd6878d → 0.8.4-staging.60fe92afc8

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 (72) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +1 -1
  3. package/dist/lib/neutral/chunk-7BYCDV55.mjs +1484 -0
  4. package/dist/lib/neutral/chunk-7BYCDV55.mjs.map +7 -0
  5. package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
  6. package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
  7. package/dist/lib/neutral/index.mjs +40 -0
  8. package/dist/lib/neutral/index.mjs.map +7 -0
  9. package/dist/lib/neutral/meta.json +1 -0
  10. package/dist/lib/neutral/scheduler.mjs +15 -0
  11. package/dist/lib/neutral/scheduler.mjs.map +7 -0
  12. package/dist/lib/neutral/testing/index.mjs +40 -0
  13. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  14. package/dist/types/src/atoms.d.ts +8 -0
  15. package/dist/types/src/atoms.d.ts.map +1 -0
  16. package/dist/types/src/graph-builder.d.ts +112 -67
  17. package/dist/types/src/graph-builder.d.ts.map +1 -1
  18. package/dist/types/src/graph.d.ts +188 -222
  19. package/dist/types/src/graph.d.ts.map +1 -1
  20. package/dist/types/src/index.d.ts +7 -3
  21. package/dist/types/src/index.d.ts.map +1 -1
  22. package/dist/types/src/node-matcher.d.ts +243 -0
  23. package/dist/types/src/node-matcher.d.ts.map +1 -0
  24. package/dist/types/src/node-matcher.test.d.ts +2 -0
  25. package/dist/types/src/node-matcher.test.d.ts.map +1 -0
  26. package/dist/types/src/node.d.ts +50 -5
  27. package/dist/types/src/node.d.ts.map +1 -1
  28. package/dist/types/src/scheduler.browser.d.ts +2 -0
  29. package/dist/types/src/scheduler.browser.d.ts.map +1 -0
  30. package/dist/types/src/scheduler.d.ts +8 -0
  31. package/dist/types/src/scheduler.d.ts.map +1 -0
  32. package/dist/types/src/stories/EchoGraph.stories.d.ts +8 -10
  33. package/dist/types/src/stories/EchoGraph.stories.d.ts.map +1 -1
  34. package/dist/types/src/testing/index.d.ts +2 -0
  35. package/dist/types/src/testing/index.d.ts.map +1 -0
  36. package/dist/types/src/testing/setup-graph-builder.d.ts +31 -0
  37. package/dist/types/src/testing/setup-graph-builder.d.ts.map +1 -0
  38. package/dist/types/src/util.d.ts +40 -0
  39. package/dist/types/src/util.d.ts.map +1 -0
  40. package/dist/types/tsconfig.tsbuildinfo +1 -1
  41. package/package.json +52 -41
  42. package/src/atoms.ts +25 -0
  43. package/src/graph-builder.test.ts +1186 -147
  44. package/src/graph-builder.ts +752 -292
  45. package/src/graph.test.ts +451 -123
  46. package/src/graph.ts +1054 -403
  47. package/src/index.ts +10 -3
  48. package/src/node-matcher.test.ts +301 -0
  49. package/src/node-matcher.ts +313 -0
  50. package/src/node.ts +82 -8
  51. package/src/scheduler.browser.ts +5 -0
  52. package/src/scheduler.ts +17 -0
  53. package/src/stories/EchoGraph.stories.tsx +172 -135
  54. package/src/stories/Tree.tsx +1 -1
  55. package/src/testing/index.ts +5 -0
  56. package/src/testing/setup-graph-builder.ts +41 -0
  57. package/src/util.ts +101 -0
  58. package/dist/lib/browser/index.mjs +0 -831
  59. package/dist/lib/browser/index.mjs.map +0 -7
  60. package/dist/lib/browser/meta.json +0 -1
  61. package/dist/lib/node-esm/index.mjs +0 -833
  62. package/dist/lib/node-esm/index.mjs.map +0 -7
  63. package/dist/lib/node-esm/meta.json +0 -1
  64. package/dist/types/src/experimental/graph-projections.test.d.ts +0 -25
  65. package/dist/types/src/experimental/graph-projections.test.d.ts.map +0 -1
  66. package/dist/types/src/signals-integration.test.d.ts +0 -2
  67. package/dist/types/src/signals-integration.test.d.ts.map +0 -1
  68. package/dist/types/src/testing.d.ts +0 -5
  69. package/dist/types/src/testing.d.ts.map +0 -1
  70. package/src/experimental/graph-projections.test.ts +0 -56
  71. package/src/signals-integration.test.ts +0 -218
  72. package/src/testing.ts +0 -20
@@ -1,833 +0,0 @@
1
- import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
-
3
- // src/graph.ts
4
- import { Registry, Rx } from "@effect-rx/rx-react";
5
- import { Option, Record, pipe } from "effect";
6
- import { Event, Trigger } from "@dxos/async";
7
- import { todo } from "@dxos/debug";
8
- import { invariant } from "@dxos/invariant";
9
- import { log } from "@dxos/log";
10
- import { isNonNullable } from "@dxos/util";
11
- var __dxlog_file = "/__w/dxos/dxos/packages/sdk/app-graph/src/graph.ts";
12
- var graphSymbol = Symbol("graph");
13
- var getGraph = (node) => {
14
- const graph = node[graphSymbol];
15
- invariant(graph, "Node is not associated with a graph.", {
16
- F: __dxlog_file,
17
- L: 25,
18
- S: void 0,
19
- A: [
20
- "graph",
21
- "'Node is not associated with a graph.'"
22
- ]
23
- });
24
- return graph;
25
- };
26
- var ROOT_ID = "root";
27
- var ROOT_TYPE = "dxos.org/type/GraphRoot";
28
- var ACTION_TYPE = "dxos.org/type/GraphAction";
29
- var ACTION_GROUP_TYPE = "dxos.org/type/GraphActionGroup";
30
- var Graph = class {
31
- onNodeChanged = new Event();
32
- _onExpand;
33
- _onInitialize;
34
- _onRemoveNode;
35
- _registry;
36
- _expanded = Record.empty();
37
- _initialized = Record.empty();
38
- _initialEdges = Record.empty();
39
- _initialNodes = Record.fromEntries([
40
- [
41
- ROOT_ID,
42
- this._constructNode({
43
- id: ROOT_ID,
44
- type: ROOT_TYPE,
45
- data: null,
46
- properties: {}
47
- })
48
- ]
49
- ]);
50
- /** @internal */
51
- _node = Rx.family((id) => {
52
- const initial = Option.flatten(Record.get(this._initialNodes, id));
53
- return Rx.make(initial).pipe(Rx.keepAlive, Rx.withLabel(`graph:node:${id}`));
54
- });
55
- _nodeOrThrow = Rx.family((id) => {
56
- return Rx.make((get) => {
57
- const node = get(this._node(id));
58
- invariant(Option.isSome(node), `Node not available: ${id}`, {
59
- F: __dxlog_file,
60
- L: 252,
61
- S: this,
62
- A: [
63
- "Option.isSome(node)",
64
- "`Node not available: ${id}`"
65
- ]
66
- });
67
- return node.value;
68
- });
69
- });
70
- _edges = Rx.family((id) => {
71
- const initial = Record.get(this._initialEdges, id).pipe(Option.getOrElse(() => ({
72
- inbound: [],
73
- outbound: []
74
- })));
75
- return Rx.make(initial).pipe(Rx.keepAlive, Rx.withLabel(`graph:edges:${id}`));
76
- });
77
- // NOTE: Currently the argument to the family needs to be referentially stable for the rx to be referentially stable.
78
- // TODO(wittjosiah): Rx feature request, support for something akin to `ComplexMap` to allow for complex arguments.
79
- _connections = Rx.family((key) => {
80
- return Rx.make((get) => {
81
- const [id, relation] = key.split("$");
82
- const edges = get(this._edges(id));
83
- return edges[relation].map((id2) => get(this._node(id2))).filter(Option.isSome).map((o) => o.value);
84
- }).pipe(Rx.withLabel(`graph:connections:${key}`));
85
- });
86
- _actions = Rx.family((id) => {
87
- return Rx.make((get) => {
88
- return get(this._connections(`${id}$outbound`)).filter((node) => node.type === ACTION_TYPE || node.type === ACTION_GROUP_TYPE);
89
- }).pipe(Rx.withLabel(`graph:actions:${id}`));
90
- });
91
- _json = Rx.family((id) => {
92
- return Rx.make((get) => {
93
- const toJSON = (node, seen = []) => {
94
- const nodes = get(this.connections(node.id));
95
- const obj = {
96
- id: node.id.length > 32 ? `${node.id.slice(0, 32)}...` : node.id,
97
- type: node.type
98
- };
99
- if (node.properties.label) {
100
- obj.label = node.properties.label;
101
- }
102
- if (nodes.length) {
103
- obj.nodes = nodes.map((n) => {
104
- const nextSeen = [
105
- ...seen,
106
- node.id
107
- ];
108
- return nextSeen.includes(n.id) ? void 0 : toJSON(n, nextSeen);
109
- }).filter(isNonNullable);
110
- }
111
- return obj;
112
- };
113
- const root = get(this.nodeOrThrow(id));
114
- return toJSON(root);
115
- }).pipe(Rx.withLabel(`graph:json:${id}`));
116
- });
117
- constructor({ registry, nodes, edges, onInitialize, onExpand, onRemoveNode } = {}) {
118
- this._registry = registry ?? Registry.make();
119
- this._onInitialize = onInitialize;
120
- this._onExpand = onExpand;
121
- this._onRemoveNode = onRemoveNode;
122
- if (nodes) {
123
- nodes.forEach((node) => {
124
- Record.set(this._initialNodes, node.id, this._constructNode(node));
125
- });
126
- }
127
- if (edges) {
128
- Object.entries(edges).forEach(([source, edges2]) => {
129
- Record.set(this._initialEdges, source, edges2);
130
- });
131
- }
132
- }
133
- toJSON(id = ROOT_ID) {
134
- return this._registry.get(this._json(id));
135
- }
136
- json(id = ROOT_ID) {
137
- return this._json(id);
138
- }
139
- node(id) {
140
- return this._node(id);
141
- }
142
- nodeOrThrow(id) {
143
- return this._nodeOrThrow(id);
144
- }
145
- connections(id, relation = "outbound") {
146
- return this._connections(`${id}$${relation}`);
147
- }
148
- actions(id) {
149
- return this._actions(id);
150
- }
151
- edges(id) {
152
- return this._edges(id);
153
- }
154
- get root() {
155
- return this.getNodeOrThrow(ROOT_ID);
156
- }
157
- getNode(id) {
158
- return this._registry.get(this.node(id));
159
- }
160
- getNodeOrThrow(id) {
161
- return this._registry.get(this.nodeOrThrow(id));
162
- }
163
- getConnections(id, relation = "outbound") {
164
- return this._registry.get(this.connections(id, relation));
165
- }
166
- getActions(id) {
167
- return this._registry.get(this.actions(id));
168
- }
169
- getEdges(id) {
170
- return this._registry.get(this.edges(id));
171
- }
172
- async initialize(id) {
173
- const initialized = Record.get(this._initialized, id).pipe(Option.getOrElse(() => false));
174
- log("initialize", {
175
- id,
176
- initialized
177
- }, {
178
- F: __dxlog_file,
179
- L: 384,
180
- S: this,
181
- C: (f, a) => f(...a)
182
- });
183
- if (!initialized) {
184
- await this._onInitialize?.(id);
185
- Record.set(this._initialized, id, true);
186
- }
187
- }
188
- expand(id, relation = "outbound") {
189
- const key = `${id}$${relation}`;
190
- const expanded = Record.get(this._expanded, key).pipe(Option.getOrElse(() => false));
191
- log("expand", {
192
- key,
193
- expanded
194
- }, {
195
- F: __dxlog_file,
196
- L: 394,
197
- S: this,
198
- C: (f, a) => f(...a)
199
- });
200
- if (!expanded) {
201
- this._onExpand?.(id, relation);
202
- Record.set(this._expanded, key, true);
203
- }
204
- }
205
- addNodes(nodes) {
206
- Rx.batch(() => {
207
- nodes.map((node) => this.addNode(node));
208
- });
209
- }
210
- addNode({ nodes, edges, ...nodeArg }) {
211
- const { id, type, data = null, properties = {} } = nodeArg;
212
- const nodeRx = this._node(id);
213
- const node = this._registry.get(nodeRx);
214
- Option.match(node, {
215
- onSome: (node2) => {
216
- const typeChanged = node2.type !== type;
217
- const dataChanged = node2.data !== data;
218
- const propertiesChanged = Object.keys(properties).some((key) => node2.properties[key] !== properties[key]);
219
- log("existing node", {
220
- id,
221
- typeChanged,
222
- dataChanged,
223
- propertiesChanged
224
- }, {
225
- F: __dxlog_file,
226
- L: 416,
227
- S: this,
228
- C: (f, a) => f(...a)
229
- });
230
- if (typeChanged || dataChanged || propertiesChanged) {
231
- log("updating node", {
232
- id,
233
- type,
234
- data,
235
- properties
236
- }, {
237
- F: __dxlog_file,
238
- L: 418,
239
- S: this,
240
- C: (f, a) => f(...a)
241
- });
242
- const newNode = Option.some({
243
- ...node2,
244
- type,
245
- data,
246
- properties: {
247
- ...node2.properties,
248
- ...properties
249
- }
250
- });
251
- this._registry.set(nodeRx, newNode);
252
- this.onNodeChanged.emit({
253
- id,
254
- node: newNode
255
- });
256
- }
257
- },
258
- onNone: () => {
259
- log("new node", {
260
- id,
261
- type,
262
- data,
263
- properties
264
- }, {
265
- F: __dxlog_file,
266
- L: 425,
267
- S: this,
268
- C: (f, a) => f(...a)
269
- });
270
- const newNode = this._constructNode({
271
- id,
272
- type,
273
- data,
274
- properties
275
- });
276
- this._registry.set(nodeRx, newNode);
277
- this.onNodeChanged.emit({
278
- id,
279
- node: newNode
280
- });
281
- }
282
- });
283
- if (nodes) {
284
- this.addNodes(nodes);
285
- const _edges = nodes.map((node2) => ({
286
- source: id,
287
- target: node2.id
288
- }));
289
- this.addEdges(_edges);
290
- }
291
- if (edges) {
292
- todo();
293
- }
294
- }
295
- removeNodes(ids, edges = false) {
296
- Rx.batch(() => {
297
- ids.map((id) => this.removeNode(id, edges));
298
- });
299
- }
300
- removeNode(id, edges = false) {
301
- const nodeRx = this._node(id);
302
- this._registry.set(nodeRx, Option.none());
303
- this.onNodeChanged.emit({
304
- id,
305
- node: Option.none()
306
- });
307
- if (edges) {
308
- const { inbound, outbound } = this._registry.get(this._edges(id));
309
- const edges2 = [
310
- ...inbound.map((source) => ({
311
- source,
312
- target: id
313
- })),
314
- ...outbound.map((target) => ({
315
- source: id,
316
- target
317
- }))
318
- ];
319
- this.removeEdges(edges2);
320
- }
321
- this._onRemoveNode?.(id);
322
- }
323
- addEdges(edges) {
324
- Rx.batch(() => {
325
- edges.map((edge) => this.addEdge(edge));
326
- });
327
- }
328
- addEdge(edgeArg) {
329
- const sourceRx = this._edges(edgeArg.source);
330
- const source = this._registry.get(sourceRx);
331
- if (!source.outbound.includes(edgeArg.target)) {
332
- log("add outbound edge", {
333
- source: edgeArg.source,
334
- target: edgeArg.target
335
- }, {
336
- F: __dxlog_file,
337
- L: 480,
338
- S: this,
339
- C: (f, a) => f(...a)
340
- });
341
- this._registry.set(sourceRx, {
342
- inbound: source.inbound,
343
- outbound: [
344
- ...source.outbound,
345
- edgeArg.target
346
- ]
347
- });
348
- }
349
- const targetRx = this._edges(edgeArg.target);
350
- const target = this._registry.get(targetRx);
351
- if (!target.inbound.includes(edgeArg.source)) {
352
- log("add inbound edge", {
353
- source: edgeArg.source,
354
- target: edgeArg.target
355
- }, {
356
- F: __dxlog_file,
357
- L: 487,
358
- S: this,
359
- C: (f, a) => f(...a)
360
- });
361
- this._registry.set(targetRx, {
362
- inbound: [
363
- ...target.inbound,
364
- edgeArg.source
365
- ],
366
- outbound: target.outbound
367
- });
368
- }
369
- }
370
- removeEdges(edges, removeOrphans = false) {
371
- Rx.batch(() => {
372
- edges.map((edge) => this.removeEdge(edge, removeOrphans));
373
- });
374
- }
375
- removeEdge(edgeArg, removeOrphans = false) {
376
- const sourceRx = this._edges(edgeArg.source);
377
- const source = this._registry.get(sourceRx);
378
- if (source.outbound.includes(edgeArg.target)) {
379
- this._registry.set(sourceRx, {
380
- inbound: source.inbound,
381
- outbound: source.outbound.filter((id) => id !== edgeArg.target)
382
- });
383
- }
384
- const targetRx = this._edges(edgeArg.target);
385
- const target = this._registry.get(targetRx);
386
- if (target.inbound.includes(edgeArg.source)) {
387
- this._registry.set(targetRx, {
388
- inbound: target.inbound.filter((id) => id !== edgeArg.source),
389
- outbound: target.outbound
390
- });
391
- }
392
- if (removeOrphans) {
393
- const source2 = this._registry.get(sourceRx);
394
- const target2 = this._registry.get(targetRx);
395
- if (source2.outbound.length === 0 && source2.inbound.length === 0 && edgeArg.source !== ROOT_ID) {
396
- this.removeNodes([
397
- edgeArg.source
398
- ]);
399
- }
400
- if (target2.outbound.length === 0 && target2.inbound.length === 0 && edgeArg.target !== ROOT_ID) {
401
- this.removeNodes([
402
- edgeArg.target
403
- ]);
404
- }
405
- }
406
- }
407
- sortEdges(id, relation, order) {
408
- const edgesRx = this._edges(id);
409
- const edges = this._registry.get(edgesRx);
410
- const unsorted = edges[relation].filter((id2) => !order.includes(id2)) ?? [];
411
- const sorted = order.filter((id2) => edges[relation].includes(id2)) ?? [];
412
- edges[relation].splice(0, edges[relation].length, ...[
413
- ...sorted,
414
- ...unsorted
415
- ]);
416
- this._registry.set(edgesRx, edges);
417
- }
418
- traverse({ visitor, source = ROOT_ID, relation = "outbound" }, path = []) {
419
- if (path.includes(source)) {
420
- return;
421
- }
422
- const node = this.getNodeOrThrow(source);
423
- const shouldContinue = visitor(node, [
424
- ...path,
425
- source
426
- ]);
427
- if (shouldContinue === false) {
428
- return;
429
- }
430
- Object.values(this.getConnections(source, relation)).forEach((child) => this.traverse({
431
- source: child.id,
432
- relation,
433
- visitor
434
- }, [
435
- ...path,
436
- source
437
- ]));
438
- }
439
- getPath({ source = "root", target }) {
440
- return pipe(this.getNode(source), Option.flatMap((node) => {
441
- let found = Option.none();
442
- this.traverse({
443
- source: node.id,
444
- visitor: (node2, path) => {
445
- if (Option.isSome(found)) {
446
- return false;
447
- }
448
- if (node2.id === target) {
449
- found = Option.some(path);
450
- }
451
- }
452
- });
453
- return found;
454
- }));
455
- }
456
- async waitForPath(params, { timeout = 5e3, interval = 500 } = {}) {
457
- const path = this.getPath(params);
458
- if (Option.isSome(path)) {
459
- return path.value;
460
- }
461
- const trigger = new Trigger();
462
- const i = setInterval(() => {
463
- const path2 = this.getPath(params);
464
- if (Option.isSome(path2)) {
465
- trigger.wake(path2.value);
466
- }
467
- }, interval);
468
- return trigger.wait({
469
- timeout
470
- }).finally(() => clearInterval(i));
471
- }
472
- /** @internal */
473
- _constructNode(node) {
474
- return Option.some({
475
- [graphSymbol]: this,
476
- data: null,
477
- properties: {},
478
- ...node
479
- });
480
- }
481
- };
482
-
483
- // src/graph-builder.ts
484
- import { Registry as Registry2, Rx as Rx2 } from "@effect-rx/rx-react";
485
- import { effect } from "@preact/signals-core";
486
- import { Array, Option as Option2, Record as Record2, pipe as pipe2 } from "effect";
487
- import { log as log2 } from "@dxos/log";
488
- import { byPosition, getDebugName, isNode, isNonNullable as isNonNullable2 } from "@dxos/util";
489
-
490
- // src/node.ts
491
- var isGraphNode = (data) => data && typeof data === "object" && "id" in data && "properties" in data && data.properties ? typeof data.properties === "object" && "data" in data : false;
492
- var isAction = (data) => isGraphNode(data) ? typeof data.data === "function" && data.type === ACTION_TYPE : false;
493
- var actionGroupSymbol = Symbol("ActionGroup");
494
- var isActionGroup = (data) => isGraphNode(data) ? data.data === actionGroupSymbol && data.type === ACTION_GROUP_TYPE : false;
495
- var isActionLike = (data) => isAction(data) || isActionGroup(data);
496
-
497
- // src/graph-builder.ts
498
- var __dxlog_file2 = "/__w/dxos/dxos/packages/sdk/app-graph/src/graph-builder.ts";
499
- var createExtension = (extension) => {
500
- const { id, position = "static", relation = "outbound", resolver: _resolver, connector: _connector, actions: _actions, actionGroups: _actionGroups } = extension;
501
- const getId = (key) => `${id}/${key}`;
502
- const resolver = _resolver && Rx2.family((id2) => _resolver(id2).pipe(Rx2.withLabel(`graph-builder:_resolver:${id2}`)));
503
- const connector = _connector && Rx2.family((node) => _connector(node).pipe(Rx2.withLabel(`graph-builder:_connector:${id}`)));
504
- const actionGroups = _actionGroups && Rx2.family((node) => _actionGroups(node).pipe(Rx2.withLabel(`graph-builder:_actionGroups:${id}`)));
505
- const actions = _actions && Rx2.family((node) => _actions(node).pipe(Rx2.withLabel(`graph-builder:_actions:${id}`)));
506
- return [
507
- resolver ? {
508
- id: getId("resolver"),
509
- position,
510
- resolver
511
- } : void 0,
512
- connector ? {
513
- id: getId("connector"),
514
- position,
515
- relation,
516
- connector: Rx2.family((node) => Rx2.make((get) => {
517
- try {
518
- return get(connector(node));
519
- } catch {
520
- log2.warn("Error in connector", {
521
- id: getId("connector"),
522
- node
523
- }, {
524
- F: __dxlog_file2,
525
- L: 109,
526
- S: void 0,
527
- C: (f, a) => f(...a)
528
- });
529
- return [];
530
- }
531
- }).pipe(Rx2.withLabel(`graph-builder:connector:${id}`)))
532
- } : void 0,
533
- actionGroups ? {
534
- id: getId("actionGroups"),
535
- position,
536
- relation: "outbound",
537
- connector: Rx2.family((node) => Rx2.make((get) => {
538
- try {
539
- return get(actionGroups(node)).map((arg) => ({
540
- ...arg,
541
- data: actionGroupSymbol,
542
- type: ACTION_GROUP_TYPE
543
- }));
544
- } catch {
545
- log2.warn("Error in actionGroups", {
546
- id: getId("actionGroups"),
547
- node
548
- }, {
549
- F: __dxlog_file2,
550
- L: 130,
551
- S: void 0,
552
- C: (f, a) => f(...a)
553
- });
554
- return [];
555
- }
556
- }).pipe(Rx2.withLabel(`graph-builder:connector:actionGroups:${id}`)))
557
- } : void 0,
558
- actions ? {
559
- id: getId("actions"),
560
- position,
561
- relation: "outbound",
562
- connector: Rx2.family((node) => Rx2.make((get) => {
563
- try {
564
- return get(actions(node)).map((arg) => ({
565
- ...arg,
566
- type: ACTION_TYPE
567
- }));
568
- } catch {
569
- log2.warn("Error in actions", {
570
- id: getId("actions"),
571
- node
572
- }, {
573
- F: __dxlog_file2,
574
- L: 147,
575
- S: void 0,
576
- C: (f, a) => f(...a)
577
- });
578
- return [];
579
- }
580
- }).pipe(Rx2.withLabel(`graph-builder:connector:actions:${id}`)))
581
- } : void 0
582
- ].filter(isNonNullable2);
583
- };
584
- var flattenExtensions = (extension, acc = []) => {
585
- if (Array.isArray(extension)) {
586
- return [
587
- ...acc,
588
- ...extension.flatMap((ext) => flattenExtensions(ext, acc))
589
- ];
590
- } else {
591
- return [
592
- ...acc,
593
- extension
594
- ];
595
- }
596
- };
597
- var GraphBuilder = class _GraphBuilder {
598
- // TODO(wittjosiah): Use Context.
599
- _subscriptions = /* @__PURE__ */ new Map();
600
- _extensions = Rx2.make(Record2.empty()).pipe(Rx2.keepAlive, Rx2.withLabel("graph-builder:extensions"));
601
- _initialized = {};
602
- _registry;
603
- _graph;
604
- constructor({ registry, ...params } = {}) {
605
- this._registry = registry ?? Registry2.make();
606
- this._graph = new Graph({
607
- ...params,
608
- registry: this._registry,
609
- onExpand: (id, relation) => this._onExpand(id, relation),
610
- onInitialize: (id) => this._onInitialize(id),
611
- onRemoveNode: (id) => this._onRemoveNode(id)
612
- });
613
- }
614
- static from(pickle, registry) {
615
- if (!pickle) {
616
- return new _GraphBuilder({
617
- registry
618
- });
619
- }
620
- const { nodes, edges } = JSON.parse(pickle);
621
- return new _GraphBuilder({
622
- nodes,
623
- edges,
624
- registry
625
- });
626
- }
627
- get graph() {
628
- return this._graph;
629
- }
630
- get extensions() {
631
- return this._extensions;
632
- }
633
- addExtension(extensions) {
634
- flattenExtensions(extensions).forEach((extension) => {
635
- const extensions2 = this._registry.get(this._extensions);
636
- this._registry.set(this._extensions, Record2.set(extensions2, extension.id, extension));
637
- });
638
- return this;
639
- }
640
- removeExtension(id) {
641
- const extensions = this._registry.get(this._extensions);
642
- this._registry.set(this._extensions, Record2.remove(extensions, id));
643
- return this;
644
- }
645
- async explore({ registry = Registry2.make(), source = ROOT_ID, relation = "outbound", visitor }, path = []) {
646
- if (path.includes(source)) {
647
- return;
648
- }
649
- if (!isNode()) {
650
- const { yieldOrContinue } = await import("main-thread-scheduling");
651
- await yieldOrContinue("idle");
652
- }
653
- const node = registry.get(this._graph.nodeOrThrow(source));
654
- const shouldContinue = await visitor(node, [
655
- ...path,
656
- node.id
657
- ]);
658
- if (shouldContinue === false) {
659
- return;
660
- }
661
- 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))));
662
- await Promise.all(nodes.map((nodeArg) => {
663
- registry.set(this._graph._node(nodeArg.id), this._graph._constructNode(nodeArg));
664
- return this.explore({
665
- registry,
666
- source: nodeArg.id,
667
- relation,
668
- visitor
669
- }, [
670
- ...path,
671
- node.id
672
- ]);
673
- }));
674
- if (registry !== this._registry) {
675
- registry.reset();
676
- registry.dispose();
677
- }
678
- }
679
- destroy() {
680
- this._subscriptions.forEach((unsubscribe) => unsubscribe());
681
- this._subscriptions.clear();
682
- }
683
- _resolvers = Rx2.family((id) => {
684
- return Rx2.make((get) => {
685
- return pipe2(get(this._extensions), Record2.values, Array.sortBy(byPosition), Array.map(({ resolver }) => resolver), Array.filter(isNonNullable2), Array.map((resolver) => get(resolver(id))), Array.filter(isNonNullable2), Array.head);
686
- });
687
- });
688
- _connectors = Rx2.family((key) => {
689
- return Rx2.make((get) => {
690
- const [id, relation] = key.split("+");
691
- const node = this._graph.node(id);
692
- return pipe2(
693
- get(this._extensions),
694
- Record2.values,
695
- // TODO(wittjosiah): Sort on write rather than read.
696
- Array.sortBy(byPosition),
697
- Array.filter(({ relation: _relation = "outbound" }) => _relation === relation),
698
- Array.map(({ connector }) => connector?.(node)),
699
- Array.filter(isNonNullable2),
700
- Array.flatMap((result) => get(result))
701
- );
702
- }).pipe(Rx2.withLabel(`graph-builder:connectors:${key}`));
703
- });
704
- _onExpand(id, relation) {
705
- log2("onExpand", {
706
- id,
707
- relation,
708
- registry: getDebugName(this._registry)
709
- }, {
710
- F: __dxlog_file2,
711
- L: 324,
712
- S: this,
713
- C: (f, a) => f(...a)
714
- });
715
- const connectors = this._connectors(`${id}+${relation}`);
716
- let previous = [];
717
- const cancel = this._registry.subscribe(connectors, (nodes) => {
718
- const ids = nodes.map((n) => n.id);
719
- const removed = previous.filter((id2) => !ids.includes(id2));
720
- previous = ids;
721
- log2("update", {
722
- id,
723
- relation,
724
- ids,
725
- removed
726
- }, {
727
- F: __dxlog_file2,
728
- L: 335,
729
- S: this,
730
- C: (f, a) => f(...a)
731
- });
732
- const update = () => {
733
- Rx2.batch(() => {
734
- this._graph.removeEdges(removed.map((target) => ({
735
- source: id,
736
- target
737
- })), true);
738
- this._graph.addNodes(nodes);
739
- this._graph.addEdges(nodes.map((node) => relation === "outbound" ? {
740
- source: id,
741
- target: node.id
742
- } : {
743
- source: node.id,
744
- target: id
745
- }));
746
- this._graph.sortEdges(id, relation, nodes.map(({ id: id2 }) => id2));
747
- });
748
- };
749
- if (typeof requestAnimationFrame === "function") {
750
- requestAnimationFrame(update);
751
- } else {
752
- update();
753
- }
754
- }, {
755
- immediate: true
756
- });
757
- this._subscriptions.set(id, cancel);
758
- }
759
- // TODO(wittjosiah): If the same node is added by a connector, the resolver should probably cancel itself?
760
- async _onInitialize(id) {
761
- log2("onInitialize", {
762
- id
763
- }, {
764
- F: __dxlog_file2,
765
- L: 372,
766
- S: this,
767
- C: (f, a) => f(...a)
768
- });
769
- const resolver = this._resolvers(id);
770
- const cancel = this._registry.subscribe(resolver, (node) => {
771
- const trigger = this._initialized[id];
772
- Option2.match(node, {
773
- onSome: (node2) => {
774
- this._graph.addNodes([
775
- node2
776
- ]);
777
- trigger?.wake();
778
- },
779
- onNone: () => {
780
- trigger?.wake();
781
- this._graph.removeNodes([
782
- id
783
- ]);
784
- }
785
- });
786
- }, {
787
- immediate: true
788
- });
789
- this._subscriptions.set(id, cancel);
790
- }
791
- _onRemoveNode(id) {
792
- this._subscriptions.get(id)?.();
793
- this._subscriptions.delete(id);
794
- }
795
- };
796
- var rxFromSignal = (cb) => {
797
- return Rx2.make((get) => {
798
- const dispose = effect(() => {
799
- get.setSelf(cb());
800
- });
801
- get.addFinalizer(() => dispose());
802
- return cb();
803
- });
804
- };
805
- var observableFamily = Rx2.family((observable) => {
806
- return Rx2.make((get) => {
807
- const subscription = observable.subscribe((value) => get.setSelf(value));
808
- get.addFinalizer(() => subscription.unsubscribe());
809
- return observable.get();
810
- });
811
- });
812
- var rxFromObservable = (observable) => {
813
- return observableFamily(observable);
814
- };
815
- export {
816
- ACTION_GROUP_TYPE,
817
- ACTION_TYPE,
818
- Graph,
819
- GraphBuilder,
820
- ROOT_ID,
821
- ROOT_TYPE,
822
- actionGroupSymbol,
823
- createExtension,
824
- flattenExtensions,
825
- getGraph,
826
- isAction,
827
- isActionGroup,
828
- isActionLike,
829
- isGraphNode,
830
- rxFromObservable,
831
- rxFromSignal
832
- };
833
- //# sourceMappingURL=index.mjs.map