@dxos/app-graph 0.8.4-main.dedc0f3 → 0.8.4-main.dfabb4ec29

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 (70) 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 +113 -67
  15. package/dist/types/src/graph-builder.d.ts.map +1 -1
  16. package/dist/types/src/graph.d.ts +188 -222
  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 +0 -1
  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 +52 -40
  40. package/src/atoms.ts +25 -0
  41. package/src/graph-builder.test.ts +1154 -144
  42. package/src/graph-builder.ts +738 -293
  43. package/src/graph.test.ts +451 -123
  44. package/src/graph.ts +1054 -403
  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 +82 -8
  49. package/src/scheduler.browser.ts +5 -0
  50. package/src/scheduler.ts +17 -0
  51. package/src/stories/EchoGraph.stories.tsx +167 -131
  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 -853
  57. package/dist/lib/browser/index.mjs.map +0 -7
  58. package/dist/lib/browser/meta.json +0 -1
  59. package/dist/lib/node-esm/index.mjs +0 -855
  60. package/dist/lib/node-esm/index.mjs.map +0 -7
  61. package/dist/lib/node-esm/meta.json +0 -1
  62. package/dist/types/src/experimental/graph-projections.test.d.ts +0 -25
  63. package/dist/types/src/experimental/graph-projections.test.d.ts.map +0 -1
  64. package/dist/types/src/signals-integration.test.d.ts +0 -2
  65. package/dist/types/src/signals-integration.test.d.ts.map +0 -1
  66. package/dist/types/src/testing.d.ts +0 -5
  67. package/dist/types/src/testing.d.ts.map +0 -1
  68. package/src/experimental/graph-projections.test.ts +0 -56
  69. package/src/signals-integration.test.ts +0 -218
  70. package/src/testing.ts +0 -20
@@ -1,855 +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
- 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
- }
24
- var __dxlog_file = "/__w/dxos/dxos/packages/sdk/app-graph/src/graph.ts";
25
- var graphSymbol = Symbol("graph");
26
- var getGraph = (node) => {
27
- const graph = node[graphSymbol];
28
- invariant(graph, "Node is not associated with a graph.", {
29
- F: __dxlog_file,
30
- L: 25,
31
- S: void 0,
32
- A: [
33
- "graph",
34
- "'Node is not associated with a graph.'"
35
- ]
36
- });
37
- return graph;
38
- };
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));
46
- }
47
- json(id = ROOT_ID) {
48
- return this._json(id);
49
- }
50
- node(id) {
51
- return this._node(id);
52
- }
53
- nodeOrThrow(id) {
54
- return this._nodeOrThrow(id);
55
- }
56
- connections(id, relation = "outbound") {
57
- return this._connections(`${id}$${relation}`);
58
- }
59
- actions(id) {
60
- return this._actions(id);
61
- }
62
- edges(id) {
63
- return this._edges(id);
64
- }
65
- get root() {
66
- return this.getNodeOrThrow(ROOT_ID);
67
- }
68
- getNode(id) {
69
- return this._registry.get(this.node(id));
70
- }
71
- getNodeOrThrow(id) {
72
- return this._registry.get(this.nodeOrThrow(id));
73
- }
74
- getConnections(id, relation = "outbound") {
75
- return this._registry.get(this.connections(id, relation));
76
- }
77
- getActions(id) {
78
- return this._registry.get(this.actions(id));
79
- }
80
- getEdges(id) {
81
- return this._registry.get(this.edges(id));
82
- }
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: 384,
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
- }
98
- }
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: 394,
108
- S: this,
109
- C: (f, a) => f(...a)
110
- });
111
- if (!expanded) {
112
- this._onExpand?.(id, relation);
113
- Record.set(this._expanded, key, true);
114
- }
115
- }
116
- addNodes(nodes) {
117
- Rx.batch(() => {
118
- nodes.map((node) => this.addNode(node));
119
- });
120
- }
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: 416,
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: 418,
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", {
171
- id,
172
- type,
173
- data,
174
- properties
175
- }, {
176
- F: __dxlog_file,
177
- L: 425,
178
- S: this,
179
- C: (f, a) => f(...a)
180
- });
181
- const newNode = this._constructNode({
182
- id,
183
- type,
184
- data,
185
- properties
186
- });
187
- this._registry.set(nodeRx, newNode);
188
- this.onNodeChanged.emit({
189
- id,
190
- node: newNode
191
- });
192
- }
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
246
- }, {
247
- F: __dxlog_file,
248
- L: 480,
249
- S: this,
250
- C: (f, a) => f(...a)
251
- });
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: 487,
269
- S: this,
270
- C: (f, a) => f(...a)
271
- });
272
- this._registry.set(targetRx, {
273
- inbound: [
274
- ...target.inbound,
275
- edgeArg.source
276
- ],
277
- outbound: target.outbound
278
- });
279
- }
280
- }
281
- removeEdges(edges, removeOrphans = false) {
282
- Rx.batch(() => {
283
- edges.map((edge) => this.removeEdge(edge, removeOrphans));
284
- });
285
- }
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
- }
317
- }
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);
328
- }
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 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
- }));
366
- }
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));
382
- }
383
- /** @internal */
384
- _constructNode(node) {
385
- return Option.some({
386
- [graphSymbol]: this,
387
- data: null,
388
- properties: {},
389
- ...node
390
- });
391
- }
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((get) => {
418
- const node = get(this._node(id));
419
- invariant(Option.isSome(node), `Node not available: ${id}`, {
420
- F: __dxlog_file,
421
- L: 252,
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((get) => {
440
- const [id, relation] = key.split("$");
441
- const edges2 = get(this._edges(id));
442
- return edges2[relation].map((id2) => get(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((get) => {
447
- return get(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((get) => {
452
- const toJSON = (node, seen = []) => {
453
- const nodes2 = get(this.connections(node.id));
454
- const obj = {
455
- id: node.id.length > 32 ? `${node.id.slice(0, 32)}...` : 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 = get(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
- });
484
- }
485
- if (edges) {
486
- Object.entries(edges).forEach(([source, edges2]) => {
487
- Record.set(this._initialEdges, source, edges2);
488
- });
489
- }
490
- }
491
- };
492
-
493
- // 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 { Array, Option as Option2, Record as Record2, pipe as pipe2 } from "effect";
497
- import { log as log2 } from "@dxos/log";
498
- import { byPosition, getDebugName, isNode, isNonNullable as isNonNullable2 } from "@dxos/util";
499
-
500
- // src/node.ts
501
- var isGraphNode = (data) => data && typeof data === "object" && "id" in data && "properties" in data && data.properties ? typeof data.properties === "object" && "data" in data : false;
502
- var isAction = (data) => isGraphNode(data) ? typeof data.data === "function" && data.type === ACTION_TYPE : false;
503
- var actionGroupSymbol = Symbol("ActionGroup");
504
- var isActionGroup = (data) => isGraphNode(data) ? data.data === actionGroupSymbol && data.type === ACTION_GROUP_TYPE : false;
505
- var isActionLike = (data) => isAction(data) || isActionGroup(data);
506
-
507
- // src/graph-builder.ts
508
- function _define_property2(obj, key, value) {
509
- if (key in obj) {
510
- Object.defineProperty(obj, key, {
511
- value,
512
- enumerable: true,
513
- configurable: true,
514
- writable: true
515
- });
516
- } else {
517
- obj[key] = value;
518
- }
519
- return obj;
520
- }
521
- var __dxlog_file2 = "/__w/dxos/dxos/packages/sdk/app-graph/src/graph-builder.ts";
522
- var createExtension = (extension) => {
523
- const { id, position = "static", relation = "outbound", resolver: _resolver, connector: _connector, actions: _actions, actionGroups: _actionGroups } = extension;
524
- const getId = (key) => `${id}/${key}`;
525
- const resolver = _resolver && Rx2.family((id2) => _resolver(id2).pipe(Rx2.withLabel(`graph-builder:_resolver:${id2}`)));
526
- const connector = _connector && Rx2.family((node) => _connector(node).pipe(Rx2.withLabel(`graph-builder:_connector:${id}`)));
527
- const actionGroups = _actionGroups && Rx2.family((node) => _actionGroups(node).pipe(Rx2.withLabel(`graph-builder:_actionGroups:${id}`)));
528
- const actions = _actions && Rx2.family((node) => _actions(node).pipe(Rx2.withLabel(`graph-builder:_actions:${id}`)));
529
- return [
530
- resolver ? {
531
- id: getId("resolver"),
532
- position,
533
- resolver
534
- } : void 0,
535
- connector ? {
536
- id: getId("connector"),
537
- position,
538
- relation,
539
- connector: Rx2.family((node) => Rx2.make((get) => {
540
- try {
541
- return get(connector(node));
542
- } catch {
543
- log2.warn("Error in connector", {
544
- id: getId("connector"),
545
- node
546
- }, {
547
- F: __dxlog_file2,
548
- L: 109,
549
- S: void 0,
550
- C: (f, a) => f(...a)
551
- });
552
- return [];
553
- }
554
- }).pipe(Rx2.withLabel(`graph-builder:connector:${id}`)))
555
- } : void 0,
556
- actionGroups ? {
557
- id: getId("actionGroups"),
558
- position,
559
- relation: "outbound",
560
- connector: Rx2.family((node) => Rx2.make((get) => {
561
- try {
562
- return get(actionGroups(node)).map((arg) => ({
563
- ...arg,
564
- data: actionGroupSymbol,
565
- type: ACTION_GROUP_TYPE
566
- }));
567
- } catch {
568
- log2.warn("Error in actionGroups", {
569
- id: getId("actionGroups"),
570
- node
571
- }, {
572
- F: __dxlog_file2,
573
- L: 130,
574
- S: void 0,
575
- C: (f, a) => f(...a)
576
- });
577
- return [];
578
- }
579
- }).pipe(Rx2.withLabel(`graph-builder:connector:actionGroups:${id}`)))
580
- } : void 0,
581
- actions ? {
582
- id: getId("actions"),
583
- position,
584
- relation: "outbound",
585
- connector: Rx2.family((node) => Rx2.make((get) => {
586
- try {
587
- return get(actions(node)).map((arg) => ({
588
- ...arg,
589
- type: ACTION_TYPE
590
- }));
591
- } catch {
592
- log2.warn("Error in actions", {
593
- id: getId("actions"),
594
- node
595
- }, {
596
- F: __dxlog_file2,
597
- L: 147,
598
- S: void 0,
599
- C: (f, a) => f(...a)
600
- });
601
- return [];
602
- }
603
- }).pipe(Rx2.withLabel(`graph-builder:connector:actions:${id}`)))
604
- } : void 0
605
- ].filter(isNonNullable2);
606
- };
607
- var flattenExtensions = (extension, acc = []) => {
608
- if (Array.isArray(extension)) {
609
- return [
610
- ...acc,
611
- ...extension.flatMap((ext) => flattenExtensions(ext, acc))
612
- ];
613
- } else {
614
- return [
615
- ...acc,
616
- extension
617
- ];
618
- }
619
- };
620
- var GraphBuilder = class _GraphBuilder {
621
- static from(pickle, registry) {
622
- if (!pickle) {
623
- return new _GraphBuilder({
624
- registry
625
- });
626
- }
627
- const { nodes, edges } = JSON.parse(pickle);
628
- return new _GraphBuilder({
629
- nodes,
630
- edges,
631
- registry
632
- });
633
- }
634
- get graph() {
635
- return this._graph;
636
- }
637
- get extensions() {
638
- return this._extensions;
639
- }
640
- addExtension(extensions) {
641
- flattenExtensions(extensions).forEach((extension) => {
642
- const extensions2 = this._registry.get(this._extensions);
643
- this._registry.set(this._extensions, Record2.set(extensions2, extension.id, extension));
644
- });
645
- return this;
646
- }
647
- removeExtension(id) {
648
- const extensions = this._registry.get(this._extensions);
649
- this._registry.set(this._extensions, Record2.remove(extensions, id));
650
- return this;
651
- }
652
- async explore({ registry = Registry2.make(), source = ROOT_ID, relation = "outbound", visitor }, path = []) {
653
- if (path.includes(source)) {
654
- return;
655
- }
656
- if (!isNode()) {
657
- const { yieldOrContinue } = await import("main-thread-scheduling");
658
- await yieldOrContinue("idle");
659
- }
660
- const node = registry.get(this._graph.nodeOrThrow(source));
661
- const shouldContinue = await visitor(node, [
662
- ...path,
663
- node.id
664
- ]);
665
- if (shouldContinue === false) {
666
- return;
667
- }
668
- 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))));
669
- await Promise.all(nodes.map((nodeArg) => {
670
- registry.set(this._graph._node(nodeArg.id), this._graph._constructNode(nodeArg));
671
- return this.explore({
672
- registry,
673
- source: nodeArg.id,
674
- relation,
675
- visitor
676
- }, [
677
- ...path,
678
- node.id
679
- ]);
680
- }));
681
- if (registry !== this._registry) {
682
- registry.reset();
683
- registry.dispose();
684
- }
685
- }
686
- destroy() {
687
- this._subscriptions.forEach((unsubscribe) => unsubscribe());
688
- this._subscriptions.clear();
689
- }
690
- _onExpand(id, relation) {
691
- log2("onExpand", {
692
- id,
693
- relation,
694
- registry: getDebugName(this._registry)
695
- }, {
696
- F: __dxlog_file2,
697
- L: 324,
698
- S: this,
699
- C: (f, a) => f(...a)
700
- });
701
- const connectors = this._connectors(`${id}+${relation}`);
702
- let previous = [];
703
- const cancel = this._registry.subscribe(connectors, (nodes) => {
704
- const ids = nodes.map((n) => n.id);
705
- const removed = previous.filter((id2) => !ids.includes(id2));
706
- previous = ids;
707
- log2("update", {
708
- id,
709
- relation,
710
- ids,
711
- removed
712
- }, {
713
- F: __dxlog_file2,
714
- L: 335,
715
- S: this,
716
- C: (f, a) => f(...a)
717
- });
718
- const update = () => {
719
- Rx2.batch(() => {
720
- this._graph.removeEdges(removed.map((target) => ({
721
- source: id,
722
- target
723
- })), true);
724
- this._graph.addNodes(nodes);
725
- this._graph.addEdges(nodes.map((node) => relation === "outbound" ? {
726
- source: id,
727
- target: node.id
728
- } : {
729
- source: node.id,
730
- target: id
731
- }));
732
- this._graph.sortEdges(id, relation, nodes.map(({ id: id2 }) => id2));
733
- });
734
- };
735
- if (typeof requestAnimationFrame === "function") {
736
- requestAnimationFrame(update);
737
- } else {
738
- update();
739
- }
740
- }, {
741
- immediate: true
742
- });
743
- this._subscriptions.set(id, cancel);
744
- }
745
- // TODO(wittjosiah): If the same node is added by a connector, the resolver should probably cancel itself?
746
- async _onInitialize(id) {
747
- log2("onInitialize", {
748
- id
749
- }, {
750
- F: __dxlog_file2,
751
- L: 372,
752
- S: this,
753
- C: (f, a) => f(...a)
754
- });
755
- const resolver = this._resolvers(id);
756
- const cancel = this._registry.subscribe(resolver, (node) => {
757
- const trigger = this._initialized[id];
758
- Option2.match(node, {
759
- onSome: (node2) => {
760
- this._graph.addNodes([
761
- node2
762
- ]);
763
- trigger?.wake();
764
- },
765
- onNone: () => {
766
- trigger?.wake();
767
- this._graph.removeNodes([
768
- id
769
- ]);
770
- }
771
- });
772
- }, {
773
- immediate: true
774
- });
775
- this._subscriptions.set(id, cancel);
776
- }
777
- _onRemoveNode(id) {
778
- this._subscriptions.get(id)?.();
779
- this._subscriptions.delete(id);
780
- }
781
- constructor({ registry, ...params } = {}) {
782
- _define_property2(this, "_subscriptions", /* @__PURE__ */ new Map());
783
- _define_property2(this, "_extensions", Rx2.make(Record2.empty()).pipe(Rx2.keepAlive, Rx2.withLabel("graph-builder:extensions")));
784
- _define_property2(this, "_initialized", {});
785
- _define_property2(this, "_registry", void 0);
786
- _define_property2(this, "_graph", void 0);
787
- _define_property2(this, "_resolvers", Rx2.family((id) => {
788
- return Rx2.make((get) => {
789
- 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);
790
- });
791
- }));
792
- _define_property2(this, "_connectors", Rx2.family((key) => {
793
- return Rx2.make((get) => {
794
- const [id, relation] = key.split("+");
795
- const node = this._graph.node(id);
796
- return pipe2(
797
- get(this._extensions),
798
- Record2.values,
799
- // TODO(wittjosiah): Sort on write rather than read.
800
- Array.sortBy(byPosition),
801
- Array.filter(({ relation: _relation = "outbound" }) => _relation === relation),
802
- Array.map(({ connector }) => connector?.(node)),
803
- Array.filter(isNonNullable2),
804
- Array.flatMap((result) => get(result))
805
- );
806
- }).pipe(Rx2.withLabel(`graph-builder:connectors:${key}`));
807
- }));
808
- this._registry = registry ?? Registry2.make();
809
- this._graph = new Graph({
810
- ...params,
811
- registry: this._registry,
812
- onExpand: (id, relation) => this._onExpand(id, relation),
813
- onInitialize: (id) => this._onInitialize(id),
814
- onRemoveNode: (id) => this._onRemoveNode(id)
815
- });
816
- }
817
- };
818
- var rxFromSignal = (cb) => {
819
- return Rx2.make((get) => {
820
- const dispose = effect(() => {
821
- get.setSelf(cb());
822
- });
823
- get.addFinalizer(() => dispose());
824
- return cb();
825
- });
826
- };
827
- var observableFamily = Rx2.family((observable) => {
828
- return Rx2.make((get) => {
829
- const subscription = observable.subscribe((value) => get.setSelf(value));
830
- get.addFinalizer(() => subscription.unsubscribe());
831
- return observable.get();
832
- });
833
- });
834
- var rxFromObservable = (observable) => {
835
- return observableFamily(observable);
836
- };
837
- export {
838
- ACTION_GROUP_TYPE,
839
- ACTION_TYPE,
840
- Graph,
841
- GraphBuilder,
842
- ROOT_ID,
843
- ROOT_TYPE,
844
- actionGroupSymbol,
845
- createExtension,
846
- flattenExtensions,
847
- getGraph,
848
- isAction,
849
- isActionGroup,
850
- isActionLike,
851
- isGraphNode,
852
- rxFromObservable,
853
- rxFromSignal
854
- };
855
- //# sourceMappingURL=index.mjs.map