@dxos/app-graph 0.8.3 → 0.8.4-main.03d5cd7b56

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