@dxos/app-graph 0.8.4-main.f9ba587 → 0.8.4-main.fcc0d83b33

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 (61) hide show
  1. package/dist/lib/browser/chunk-ZPU7IO6U.mjs +1469 -0
  2. package/dist/lib/browser/chunk-ZPU7IO6U.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +27 -767
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +39 -0
  7. package/dist/lib/browser/testing/index.mjs.map +7 -0
  8. package/dist/lib/node-esm/chunk-2WLCW7IW.mjs +1470 -0
  9. package/dist/lib/node-esm/chunk-2WLCW7IW.mjs.map +7 -0
  10. package/dist/lib/node-esm/index.mjs +27 -768
  11. package/dist/lib/node-esm/index.mjs.map +4 -4
  12. package/dist/lib/node-esm/meta.json +1 -1
  13. package/dist/lib/node-esm/testing/index.mjs +40 -0
  14. package/dist/lib/node-esm/testing/index.mjs.map +7 -0
  15. package/dist/types/src/atoms.d.ts +8 -0
  16. package/dist/types/src/atoms.d.ts.map +1 -0
  17. package/dist/types/src/graph-builder.d.ts +117 -60
  18. package/dist/types/src/graph-builder.d.ts.map +1 -1
  19. package/dist/types/src/graph.d.ts +188 -218
  20. package/dist/types/src/graph.d.ts.map +1 -1
  21. package/dist/types/src/index.d.ts +7 -3
  22. package/dist/types/src/index.d.ts.map +1 -1
  23. package/dist/types/src/node-matcher.d.ts +244 -0
  24. package/dist/types/src/node-matcher.d.ts.map +1 -0
  25. package/dist/types/src/node-matcher.test.d.ts +2 -0
  26. package/dist/types/src/node-matcher.test.d.ts.map +1 -0
  27. package/dist/types/src/node.d.ts +50 -5
  28. package/dist/types/src/node.d.ts.map +1 -1
  29. package/dist/types/src/stories/EchoGraph.stories.d.ts +8 -10
  30. package/dist/types/src/stories/EchoGraph.stories.d.ts.map +1 -1
  31. package/dist/types/src/testing/index.d.ts +2 -0
  32. package/dist/types/src/testing/index.d.ts.map +1 -0
  33. package/dist/types/src/testing/setup-graph-builder.d.ts +31 -0
  34. package/dist/types/src/testing/setup-graph-builder.d.ts.map +1 -0
  35. package/dist/types/src/util.d.ts +39 -0
  36. package/dist/types/src/util.d.ts.map +1 -0
  37. package/dist/types/tsconfig.tsbuildinfo +1 -1
  38. package/package.json +43 -40
  39. package/src/atoms.ts +25 -0
  40. package/src/graph-builder.test.ts +1067 -126
  41. package/src/graph-builder.ts +734 -264
  42. package/src/graph.test.ts +451 -123
  43. package/src/graph.ts +1057 -407
  44. package/src/index.ts +10 -3
  45. package/src/node-matcher.test.ts +301 -0
  46. package/src/node-matcher.ts +314 -0
  47. package/src/node.ts +83 -7
  48. package/src/stories/EchoGraph.stories.tsx +180 -140
  49. package/src/stories/Tree.tsx +1 -1
  50. package/src/testing/index.ts +5 -0
  51. package/src/testing/setup-graph-builder.ts +41 -0
  52. package/src/util.ts +99 -0
  53. package/dist/types/src/experimental/graph-projections.test.d.ts +0 -25
  54. package/dist/types/src/experimental/graph-projections.test.d.ts.map +0 -1
  55. package/dist/types/src/signals-integration.test.d.ts +0 -2
  56. package/dist/types/src/signals-integration.test.d.ts.map +0 -1
  57. package/dist/types/src/testing.d.ts +0 -5
  58. package/dist/types/src/testing.d.ts.map +0 -1
  59. package/src/experimental/graph-projections.test.ts +0 -56
  60. package/src/signals-integration.test.ts +0 -218
  61. package/src/testing.ts +0 -20
@@ -1,778 +1,38 @@
1
- // 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 = "/__w/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
- };
1
+ import {
2
+ __export,
3
+ getParentId,
4
+ getSegmentId,
5
+ graph_builder_exports,
6
+ graph_exports,
7
+ node_exports,
8
+ node_matcher_exports,
9
+ qualifyId
10
+ } from "./chunk-ZPU7IO6U.mjs";
468
11
 
469
- // 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
- // 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
- // src/graph-builder.ts
484
- var __dxlog_file2 = "/__w/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) => {
12
+ // src/atoms.ts
13
+ var atoms_exports = {};
14
+ __export(atoms_exports, {
15
+ fromObservable: () => fromObservable
16
+ });
17
+ import { Atom } from "@effect-atom/atom-react";
18
+ var observableFamily = Atom.family((observable) => {
19
+ return Atom.make((get) => {
752
20
  const subscription = observable.subscribe((value) => get.setSelf(value));
753
21
  get.addFinalizer(() => subscription.unsubscribe());
754
22
  return observable.get();
755
23
  });
756
24
  });
757
- var rxFromObservable = (observable) => {
25
+ var fromObservable = (observable) => {
758
26
  return observableFamily(observable);
759
27
  };
760
28
  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
29
+ atoms_exports as CreateAtom,
30
+ graph_exports as Graph,
31
+ graph_builder_exports as GraphBuilder,
32
+ node_exports as Node,
33
+ node_matcher_exports as NodeMatcher,
34
+ getParentId,
35
+ getSegmentId,
36
+ qualifyId
777
37
  };
778
38
  //# sourceMappingURL=index.mjs.map