@dxos/app-graph 0.8.4-main.a4bbb77 → 0.8.4-main.ae835ea

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.
@@ -2,32 +2,21 @@ import { createRequire } from 'node:module';const require = createRequire(import
2
2
 
3
3
  // src/graph.ts
4
4
  import { Registry, Rx } from "@effect-rx/rx-react";
5
- import { Option, Record, pipe } from "effect";
5
+ import * as Function from "effect/Function";
6
+ import * as Option from "effect/Option";
7
+ import * as Record from "effect/Record";
6
8
  import { Event, Trigger } from "@dxos/async";
7
9
  import { todo } from "@dxos/debug";
8
10
  import { invariant } from "@dxos/invariant";
9
11
  import { log } from "@dxos/log";
10
12
  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
13
  var __dxlog_file = "/__w/dxos/dxos/packages/sdk/app-graph/src/graph.ts";
25
14
  var graphSymbol = Symbol("graph");
26
15
  var getGraph = (node) => {
27
16
  const graph = node[graphSymbol];
28
17
  invariant(graph, "Node is not associated with a graph.", {
29
18
  F: __dxlog_file,
30
- L: 25,
19
+ L: 27,
31
20
  S: void 0,
32
21
  A: [
33
22
  "graph",
@@ -41,6 +30,108 @@ var ROOT_TYPE = "dxos.org/type/GraphRoot";
41
30
  var ACTION_TYPE = "dxos.org/type/GraphAction";
42
31
  var ACTION_GROUP_TYPE = "dxos.org/type/GraphActionGroup";
43
32
  var Graph = class {
33
+ onNodeChanged = new Event();
34
+ _onExpand;
35
+ _onInitialize;
36
+ _onRemoveNode;
37
+ _registry;
38
+ _expanded = Record.empty();
39
+ _initialized = Record.empty();
40
+ _initialEdges = Record.empty();
41
+ _initialNodes = Record.fromEntries([
42
+ [
43
+ ROOT_ID,
44
+ this._constructNode({
45
+ id: ROOT_ID,
46
+ type: ROOT_TYPE,
47
+ data: null,
48
+ properties: {}
49
+ })
50
+ ]
51
+ ]);
52
+ /** @internal */
53
+ _node = Rx.family((id) => {
54
+ const initial = Option.flatten(Record.get(this._initialNodes, id));
55
+ return Rx.make(initial).pipe(Rx.keepAlive, Rx.withLabel(`graph:node:${id}`));
56
+ });
57
+ _nodeOrThrow = Rx.family((id) => {
58
+ return Rx.make((get2) => {
59
+ const node = get2(this._node(id));
60
+ invariant(Option.isSome(node), `Node not available: ${id}`, {
61
+ F: __dxlog_file,
62
+ L: 254,
63
+ S: this,
64
+ A: [
65
+ "Option.isSome(node)",
66
+ "`Node not available: ${id}`"
67
+ ]
68
+ });
69
+ return node.value;
70
+ });
71
+ });
72
+ _edges = Rx.family((id) => {
73
+ const initial = Record.get(this._initialEdges, id).pipe(Option.getOrElse(() => ({
74
+ inbound: [],
75
+ outbound: []
76
+ })));
77
+ return Rx.make(initial).pipe(Rx.keepAlive, Rx.withLabel(`graph:edges:${id}`));
78
+ });
79
+ // NOTE: Currently the argument to the family needs to be referentially stable for the rx to be referentially stable.
80
+ // TODO(wittjosiah): Rx feature request, support for something akin to `ComplexMap` to allow for complex arguments.
81
+ _connections = Rx.family((key) => {
82
+ return Rx.make((get2) => {
83
+ const [id, relation] = key.split("$");
84
+ const edges = get2(this._edges(id));
85
+ return edges[relation].map((id2) => get2(this._node(id2))).filter(Option.isSome).map((o) => o.value);
86
+ }).pipe(Rx.withLabel(`graph:connections:${key}`));
87
+ });
88
+ _actions = Rx.family((id) => {
89
+ return Rx.make((get2) => {
90
+ return get2(this._connections(`${id}$outbound`)).filter((node) => node.type === ACTION_TYPE || node.type === ACTION_GROUP_TYPE);
91
+ }).pipe(Rx.withLabel(`graph:actions:${id}`));
92
+ });
93
+ _json = Rx.family((id) => {
94
+ return Rx.make((get2) => {
95
+ const toJSON = (node, seen = []) => {
96
+ const nodes = get2(this.connections(node.id));
97
+ const obj = {
98
+ id: node.id,
99
+ type: node.type
100
+ };
101
+ if (node.properties.label) {
102
+ obj.label = node.properties.label;
103
+ }
104
+ if (nodes.length) {
105
+ obj.nodes = nodes.map((n) => {
106
+ const nextSeen = [
107
+ ...seen,
108
+ node.id
109
+ ];
110
+ return nextSeen.includes(n.id) ? void 0 : toJSON(n, nextSeen);
111
+ }).filter(isNonNullable);
112
+ }
113
+ return obj;
114
+ };
115
+ const root = get2(this.nodeOrThrow(id));
116
+ return toJSON(root);
117
+ }).pipe(Rx.withLabel(`graph:json:${id}`));
118
+ });
119
+ constructor({ registry, nodes, edges, onInitialize, onExpand, onRemoveNode } = {}) {
120
+ this._registry = registry ?? Registry.make();
121
+ this._onInitialize = onInitialize;
122
+ this._onExpand = onExpand;
123
+ this._onRemoveNode = onRemoveNode;
124
+ if (nodes) {
125
+ nodes.forEach((node) => {
126
+ Record.set(this._initialNodes, node.id, this._constructNode(node));
127
+ });
128
+ }
129
+ if (edges) {
130
+ Object.entries(edges).forEach(([source, edges2]) => {
131
+ Record.set(this._initialEdges, source, edges2);
132
+ });
133
+ }
134
+ }
44
135
  toJSON(id = ROOT_ID) {
45
136
  return this._registry.get(this._json(id));
46
137
  }
@@ -87,7 +178,7 @@ var Graph = class {
87
178
  initialized
88
179
  }, {
89
180
  F: __dxlog_file,
90
- L: 384,
181
+ L: 386,
91
182
  S: this,
92
183
  C: (f, a) => f(...a)
93
184
  });
@@ -104,7 +195,7 @@ var Graph = class {
104
195
  expanded
105
196
  }, {
106
197
  F: __dxlog_file,
107
- L: 394,
198
+ L: 396,
108
199
  S: this,
109
200
  C: (f, a) => f(...a)
110
201
  });
@@ -134,7 +225,7 @@ var Graph = class {
134
225
  propertiesChanged
135
226
  }, {
136
227
  F: __dxlog_file,
137
- L: 416,
228
+ L: 418,
138
229
  S: this,
139
230
  C: (f, a) => f(...a)
140
231
  });
@@ -146,7 +237,7 @@ var Graph = class {
146
237
  properties
147
238
  }, {
148
239
  F: __dxlog_file,
149
- L: 418,
240
+ L: 420,
150
241
  S: this,
151
242
  C: (f, a) => f(...a)
152
243
  });
@@ -174,7 +265,7 @@ var Graph = class {
174
265
  properties
175
266
  }, {
176
267
  F: __dxlog_file,
177
- L: 425,
268
+ L: 427,
178
269
  S: this,
179
270
  C: (f, a) => f(...a)
180
271
  });
@@ -245,7 +336,7 @@ var Graph = class {
245
336
  target: edgeArg.target
246
337
  }, {
247
338
  F: __dxlog_file,
248
- L: 480,
339
+ L: 482,
249
340
  S: this,
250
341
  C: (f, a) => f(...a)
251
342
  });
@@ -265,7 +356,7 @@ var Graph = class {
265
356
  target: edgeArg.target
266
357
  }, {
267
358
  F: __dxlog_file,
268
- L: 487,
359
+ L: 489,
269
360
  S: this,
270
361
  C: (f, a) => f(...a)
271
362
  });
@@ -348,7 +439,7 @@ var Graph = class {
348
439
  ]));
349
440
  }
350
441
  getPath({ source = "root", target }) {
351
- return pipe(this.getNode(source), Option.flatMap((node) => {
442
+ return Function.pipe(this.getNode(source), Option.flatMap((node) => {
352
443
  let found = Option.none();
353
444
  this.traverse({
354
445
  source: node.id,
@@ -389,111 +480,15 @@ var Graph = class {
389
480
  ...node
390
481
  });
391
482
  }
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
483
  };
492
484
 
493
485
  // src/graph-builder.ts
494
486
  import { Registry as Registry2, Rx as Rx2 } from "@effect-rx/rx-react";
495
487
  import { effect } from "@preact/signals-core";
496
- import { Array, Option as Option2, Record as Record2, pipe as pipe2 } from "effect";
488
+ import * as Array from "effect/Array";
489
+ import * as Function2 from "effect/Function";
490
+ import * as Option2 from "effect/Option";
491
+ import * as Record2 from "effect/Record";
497
492
  import { log as log2 } from "@dxos/log";
498
493
  import { byPosition, getDebugName, isNode, isNonNullable as isNonNullable2 } from "@dxos/util";
499
494
 
@@ -505,19 +500,6 @@ var isActionGroup = (data) => isGraphNode(data) ? data.data === actionGroupSymbo
505
500
  var isActionLike = (data) => isAction(data) || isActionGroup(data);
506
501
 
507
502
  // 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
503
  var __dxlog_file2 = "/__w/dxos/dxos/packages/sdk/app-graph/src/graph-builder.ts";
522
504
  var createExtension = (extension) => {
523
505
  const { id, position = "static", relation = "outbound", resolver: _resolver, connector: _connector, actions: _actions, actionGroups: _actionGroups } = extension;
@@ -536,16 +518,16 @@ var createExtension = (extension) => {
536
518
  id: getId("connector"),
537
519
  position,
538
520
  relation,
539
- connector: Rx2.family((node) => Rx2.make((get) => {
521
+ connector: Rx2.family((node) => Rx2.make((get2) => {
540
522
  try {
541
- return get(connector(node));
523
+ return get2(connector(node));
542
524
  } catch {
543
525
  log2.warn("Error in connector", {
544
526
  id: getId("connector"),
545
527
  node
546
528
  }, {
547
529
  F: __dxlog_file2,
548
- L: 109,
530
+ L: 112,
549
531
  S: void 0,
550
532
  C: (f, a) => f(...a)
551
533
  });
@@ -557,9 +539,9 @@ var createExtension = (extension) => {
557
539
  id: getId("actionGroups"),
558
540
  position,
559
541
  relation: "outbound",
560
- connector: Rx2.family((node) => Rx2.make((get) => {
542
+ connector: Rx2.family((node) => Rx2.make((get2) => {
561
543
  try {
562
- return get(actionGroups(node)).map((arg) => ({
544
+ return get2(actionGroups(node)).map((arg) => ({
563
545
  ...arg,
564
546
  data: actionGroupSymbol,
565
547
  type: ACTION_GROUP_TYPE
@@ -570,7 +552,7 @@ var createExtension = (extension) => {
570
552
  node
571
553
  }, {
572
554
  F: __dxlog_file2,
573
- L: 130,
555
+ L: 133,
574
556
  S: void 0,
575
557
  C: (f, a) => f(...a)
576
558
  });
@@ -582,9 +564,9 @@ var createExtension = (extension) => {
582
564
  id: getId("actions"),
583
565
  position,
584
566
  relation: "outbound",
585
- connector: Rx2.family((node) => Rx2.make((get) => {
567
+ connector: Rx2.family((node) => Rx2.make((get2) => {
586
568
  try {
587
- return get(actions(node)).map((arg) => ({
569
+ return get2(actions(node)).map((arg) => ({
588
570
  ...arg,
589
571
  type: ACTION_TYPE
590
572
  }));
@@ -594,7 +576,7 @@ var createExtension = (extension) => {
594
576
  node
595
577
  }, {
596
578
  F: __dxlog_file2,
597
- L: 147,
579
+ L: 150,
598
580
  S: void 0,
599
581
  C: (f, a) => f(...a)
600
582
  });
@@ -618,6 +600,22 @@ var flattenExtensions = (extension, acc = []) => {
618
600
  }
619
601
  };
620
602
  var GraphBuilder = class _GraphBuilder {
603
+ // TODO(wittjosiah): Use Context.
604
+ _subscriptions = /* @__PURE__ */ new Map();
605
+ _extensions = Rx2.make(Record2.empty()).pipe(Rx2.keepAlive, Rx2.withLabel("graph-builder:extensions"));
606
+ _initialized = {};
607
+ _registry;
608
+ _graph;
609
+ constructor({ registry, ...params } = {}) {
610
+ this._registry = registry ?? Registry2.make();
611
+ this._graph = new Graph({
612
+ ...params,
613
+ registry: this._registry,
614
+ onExpand: (id, relation) => this._onExpand(id, relation),
615
+ onInitialize: (id) => this._onInitialize(id),
616
+ onRemoveNode: (id) => this._onRemoveNode(id)
617
+ });
618
+ }
621
619
  static from(pickle, registry) {
622
620
  if (!pickle) {
623
621
  return new _GraphBuilder({
@@ -687,6 +685,27 @@ var GraphBuilder = class _GraphBuilder {
687
685
  this._subscriptions.forEach((unsubscribe) => unsubscribe());
688
686
  this._subscriptions.clear();
689
687
  }
688
+ _resolvers = Rx2.family((id) => {
689
+ return Rx2.make((get2) => {
690
+ 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);
691
+ });
692
+ });
693
+ _connectors = Rx2.family((key) => {
694
+ return Rx2.make((get2) => {
695
+ const [id, relation] = key.split("+");
696
+ const node = this._graph.node(id);
697
+ return Function2.pipe(
698
+ get2(this._extensions),
699
+ Record2.values,
700
+ // TODO(wittjosiah): Sort on write rather than read.
701
+ Array.sortBy(byPosition),
702
+ Array.filter(({ relation: _relation = "outbound" }) => _relation === relation),
703
+ Array.map(({ connector }) => connector?.(node)),
704
+ Array.filter(isNonNullable2),
705
+ Array.flatMap((result) => get2(result))
706
+ );
707
+ }).pipe(Rx2.withLabel(`graph-builder:connectors:${key}`));
708
+ });
690
709
  _onExpand(id, relation) {
691
710
  log2("onExpand", {
692
711
  id,
@@ -694,7 +713,7 @@ var GraphBuilder = class _GraphBuilder {
694
713
  registry: getDebugName(this._registry)
695
714
  }, {
696
715
  F: __dxlog_file2,
697
- L: 324,
716
+ L: 327,
698
717
  S: this,
699
718
  C: (f, a) => f(...a)
700
719
  });
@@ -711,7 +730,7 @@ var GraphBuilder = class _GraphBuilder {
711
730
  removed
712
731
  }, {
713
732
  F: __dxlog_file2,
714
- L: 335,
733
+ L: 338,
715
734
  S: this,
716
735
  C: (f, a) => f(...a)
717
736
  });
@@ -748,7 +767,7 @@ var GraphBuilder = class _GraphBuilder {
748
767
  id
749
768
  }, {
750
769
  F: __dxlog_file2,
751
- L: 372,
770
+ L: 375,
752
771
  S: this,
753
772
  C: (f, a) => f(...a)
754
773
  });
@@ -778,56 +797,20 @@ var GraphBuilder = class _GraphBuilder {
778
797
  this._subscriptions.get(id)?.();
779
798
  this._subscriptions.delete(id);
780
799
  }
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
800
  };
818
801
  var rxFromSignal = (cb) => {
819
- return Rx2.make((get) => {
802
+ return Rx2.make((get2) => {
820
803
  const dispose = effect(() => {
821
- get.setSelf(cb());
804
+ get2.setSelf(cb());
822
805
  });
823
- get.addFinalizer(() => dispose());
806
+ get2.addFinalizer(() => dispose());
824
807
  return cb();
825
808
  });
826
809
  };
827
810
  var observableFamily = Rx2.family((observable) => {
828
- return Rx2.make((get) => {
829
- const subscription = observable.subscribe((value) => get.setSelf(value));
830
- get.addFinalizer(() => subscription.unsubscribe());
811
+ return Rx2.make((get2) => {
812
+ const subscription = observable.subscribe((value) => get2.setSelf(value));
813
+ get2.addFinalizer(() => subscription.unsubscribe());
831
814
  return observable.get();
832
815
  });
833
816
  });