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

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 (71) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +1 -1
  3. package/dist/lib/neutral/chunk-32XXJE6M.mjs +1477 -0
  4. package/dist/lib/neutral/chunk-32XXJE6M.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 -66
  17. package/dist/types/src/graph-builder.d.ts.map +1 -1
  18. package/dist/types/src/graph.d.ts +187 -221
  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 +244 -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.map +1 -1
  33. package/dist/types/src/testing/index.d.ts +2 -0
  34. package/dist/types/src/testing/index.d.ts.map +1 -0
  35. package/dist/types/src/testing/setup-graph-builder.d.ts +31 -0
  36. package/dist/types/src/testing/setup-graph-builder.d.ts.map +1 -0
  37. package/dist/types/src/util.d.ts +40 -0
  38. package/dist/types/src/util.d.ts.map +1 -0
  39. package/dist/types/tsconfig.tsbuildinfo +1 -1
  40. package/package.json +53 -40
  41. package/src/atoms.ts +25 -0
  42. package/src/graph-builder.test.ts +1146 -137
  43. package/src/graph-builder.ts +731 -289
  44. package/src/graph.test.ts +450 -122
  45. package/src/graph.ts +1050 -401
  46. package/src/index.ts +10 -3
  47. package/src/node-matcher.test.ts +301 -0
  48. package/src/node-matcher.ts +314 -0
  49. package/src/node.ts +82 -8
  50. package/src/scheduler.browser.ts +5 -0
  51. package/src/scheduler.ts +17 -0
  52. package/src/stories/EchoGraph.stories.tsx +160 -123
  53. package/src/stories/Tree.tsx +1 -1
  54. package/src/testing/index.ts +5 -0
  55. package/src/testing/setup-graph-builder.ts +41 -0
  56. package/src/util.ts +101 -0
  57. package/dist/lib/browser/index.mjs +0 -858
  58. package/dist/lib/browser/index.mjs.map +0 -7
  59. package/dist/lib/browser/meta.json +0 -1
  60. package/dist/lib/node-esm/index.mjs +0 -860
  61. package/dist/lib/node-esm/index.mjs.map +0 -7
  62. package/dist/lib/node-esm/meta.json +0 -1
  63. package/dist/types/src/experimental/graph-projections.test.d.ts +0 -25
  64. package/dist/types/src/experimental/graph-projections.test.d.ts.map +0 -1
  65. package/dist/types/src/signals-integration.test.d.ts +0 -2
  66. package/dist/types/src/signals-integration.test.d.ts.map +0 -1
  67. package/dist/types/src/testing.d.ts +0 -5
  68. package/dist/types/src/testing.d.ts.map +0 -1
  69. package/src/experimental/graph-projections.test.ts +0 -56
  70. package/src/signals-integration.test.ts +0 -218
  71. package/src/testing.ts +0 -20
@@ -1,858 +0,0 @@
1
- // src/graph.ts
2
- import { Registry, Rx } from "@effect-rx/rx-react";
3
- import * as Function from "effect/Function";
4
- import * as Option from "effect/Option";
5
- import * as Record from "effect/Record";
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: 27,
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: 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
- }
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: 396,
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: 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", {
171
- id,
172
- type,
173
- data,
174
- properties
175
- }, {
176
- F: __dxlog_file,
177
- L: 427,
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: 482,
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: 489,
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 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
- }));
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((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
- });
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 * as Array from "effect/Array";
497
- import * as Function2 from "effect/Function";
498
- import * as Option2 from "effect/Option";
499
- import * as Record2 from "effect/Record";
500
- import { log as log2 } from "@dxos/log";
501
- import { byPosition, getDebugName, isNode, isNonNullable as isNonNullable2 } from "@dxos/util";
502
-
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;
521
- }
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);
609
- };
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
- ];
621
- }
622
- };
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
635
- });
636
- }
637
- get graph() {
638
- return this._graph;
639
- }
640
- get extensions() {
641
- return this._extensions;
642
- }
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));
647
- });
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
- }
693
- _onExpand(id, relation) {
694
- log2("onExpand", {
695
- id,
696
- relation,
697
- registry: getDebugName(this._registry)
698
- }, {
699
- F: __dxlog_file2,
700
- L: 327,
701
- S: this,
702
- C: (f, a) => f(...a)
703
- });
704
- const connectors = this._connectors(`${id}+${relation}`);
705
- let previous = [];
706
- const cancel = this._registry.subscribe(connectors, (nodes) => {
707
- const ids = nodes.map((n) => n.id);
708
- const removed = previous.filter((id2) => !ids.includes(id2));
709
- previous = ids;
710
- log2("update", {
711
- id,
712
- relation,
713
- ids,
714
- removed
715
- }, {
716
- F: __dxlog_file2,
717
- L: 338,
718
- S: this,
719
- C: (f, a) => f(...a)
720
- });
721
- const update = () => {
722
- Rx2.batch(() => {
723
- this._graph.removeEdges(removed.map((target) => ({
724
- source: id,
725
- target
726
- })), true);
727
- this._graph.addNodes(nodes);
728
- this._graph.addEdges(nodes.map((node) => relation === "outbound" ? {
729
- source: id,
730
- target: node.id
731
- } : {
732
- source: node.id,
733
- target: id
734
- }));
735
- this._graph.sortEdges(id, relation, nodes.map(({ id: id2 }) => id2));
736
- });
737
- };
738
- if (typeof requestAnimationFrame === "function") {
739
- requestAnimationFrame(update);
740
- } else {
741
- update();
742
- }
743
- }, {
744
- immediate: true
745
- });
746
- this._subscriptions.set(id, cancel);
747
- }
748
- // TODO(wittjosiah): If the same node is added by a connector, the resolver should probably cancel itself?
749
- async _onInitialize(id) {
750
- log2("onInitialize", {
751
- id
752
- }, {
753
- F: __dxlog_file2,
754
- L: 375,
755
- S: this,
756
- C: (f, a) => f(...a)
757
- });
758
- const resolver = this._resolvers(id);
759
- const cancel = this._registry.subscribe(resolver, (node) => {
760
- const trigger = this._initialized[id];
761
- Option2.match(node, {
762
- onSome: (node2) => {
763
- this._graph.addNodes([
764
- node2
765
- ]);
766
- trigger?.wake();
767
- },
768
- onNone: () => {
769
- trigger?.wake();
770
- this._graph.removeNodes([
771
- id
772
- ]);
773
- }
774
- });
775
- }, {
776
- immediate: true
777
- });
778
- this._subscriptions.set(id, cancel);
779
- }
780
- _onRemoveNode(id) {
781
- this._subscriptions.get(id)?.();
782
- this._subscriptions.delete(id);
783
- }
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)
818
- });
819
- }
820
- };
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();
828
- });
829
- };
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();
835
- });
836
- });
837
- var rxFromObservable = (observable) => {
838
- return observableFamily(observable);
839
- };
840
- 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
857
- };
858
- //# sourceMappingURL=index.mjs.map