@dxos/app-graph 0.8.4-main.fffef41 → 0.8.4-staging.60fe92afc8

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-7BYCDV55.mjs +1484 -0
  4. package/dist/lib/neutral/chunk-7BYCDV55.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 +110 -65
  17. package/dist/types/src/graph-builder.d.ts.map +1 -1
  18. package/dist/types/src/graph.d.ts +179 -213
  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 +243 -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 +52 -40
  41. package/src/atoms.ts +25 -0
  42. package/src/graph-builder.test.ts +1153 -115
  43. package/src/graph-builder.ts +740 -285
  44. package/src/graph.test.ts +448 -120
  45. package/src/graph.ts +1022 -413
  46. package/src/index.ts +10 -3
  47. package/src/node-matcher.test.ts +301 -0
  48. package/src/node-matcher.ts +313 -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 +150 -121
  53. package/src/stories/Tree.tsx +2 -2
  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 -836
  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 -838
  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 -219
  71. package/src/testing.ts +0 -20
package/src/graph.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  import { Atom, Registry } from '@effect-atom/atom-react';
6
6
  import * as Function from 'effect/Function';
7
7
  import * as Option from 'effect/Option';
8
+ import * as Pipeable from 'effect/Pipeable';
8
9
  import * as Record from 'effect/Record';
9
10
 
10
11
  import { Event, Trigger } from '@dxos/async';
@@ -13,35 +14,33 @@ import { invariant } from '@dxos/invariant';
13
14
  import { log } from '@dxos/log';
14
15
  import { type MakeOptional, isNonNullable } from '@dxos/util';
15
16
 
16
- import { type Action, type ActionGroup, type Node, type NodeArg, type Relation } from './node';
17
+ import * as Node from './node';
18
+ import { normalizeRelation, primaryKey, primaryParts, secondaryKey, secondaryParts, shallowEqual } from './util';
17
19
 
18
20
  const graphSymbol = Symbol('graph');
21
+
19
22
  type DeepWriteable<T> = {
20
23
  -readonly [K in keyof T]: T[K] extends object ? DeepWriteable<T[K]> : T[K];
21
24
  };
22
- type NodeInternal = DeepWriteable<Node> & { [graphSymbol]: Graph };
25
+
26
+ type NodeInternal = DeepWriteable<Node.Node> & { [graphSymbol]: GraphImpl };
23
27
 
24
28
  /**
25
29
  * Get the Graph a Node is currently associated with.
26
30
  */
27
- export const getGraph = (node: Node): Graph => {
31
+ export const getGraph = (node: Node.Node): Graph => {
28
32
  const graph = (node as NodeInternal)[graphSymbol];
29
33
  invariant(graph, 'Node is not associated with a graph.');
30
- return graph;
34
+ return graph as Graph;
31
35
  };
32
36
 
33
- export const ROOT_ID = 'root';
34
- export const ROOT_TYPE = 'dxos.org/type/GraphRoot';
35
- export const ACTION_TYPE = 'dxos.org/type/GraphAction';
36
- export const ACTION_GROUP_TYPE = 'dxos.org/type/GraphActionGroup';
37
-
38
37
  export type GraphTraversalOptions = {
39
38
  /**
40
39
  * A callback which is called for each node visited during traversal.
41
40
  *
42
41
  * If the callback returns `false`, traversal is stops recursing.
43
42
  */
44
- visitor: (node: Node, path: string[]) => boolean | void;
43
+ visitor: (node: Node.Node, path: string[]) => boolean | void;
45
44
 
46
45
  /**
47
46
  * The node to start traversing from.
@@ -50,205 +49,111 @@ export type GraphTraversalOptions = {
50
49
  */
51
50
  source?: string;
52
51
 
53
- /**
54
- * The relation to traverse graph edges.
55
- *
56
- * @default 'outbound'
57
- */
58
- relation?: Relation;
52
+ /** The relation(s) to traverse graph edges. */
53
+ relation: Node.RelationInput | Node.RelationInput[];
59
54
  };
60
55
 
61
- export type GraphParams = {
56
+ export type GraphProps = {
62
57
  registry?: Registry.Registry;
63
- nodes?: MakeOptional<Node, 'data' | 'cacheable'>[];
58
+ nodes?: MakeOptional<Node.Node, 'data' | 'cacheable'>[];
64
59
  edges?: Record<string, Edges>;
65
- onExpand?: Graph['_onExpand'];
66
- onInitialize?: Graph['_onInitialize'];
67
- onRemoveNode?: Graph['_onRemoveNode'];
60
+ onExpand?: (id: string, relation: Node.Relation) => void;
61
+ onInitialize?: (id: string) => Promise<void>;
62
+ onRemoveNode?: (id: string) => void;
68
63
  };
69
64
 
70
- export type Edge = { source: string; target: string };
71
- export type Edges = { inbound: string[]; outbound: string[] };
65
+ export type Edge = { source: string; target: string; relation: Node.RelationInput };
66
+ export type Edges = Record<string, string[]>;
67
+
68
+ /**
69
+ * Identifier denoting a Graph.
70
+ */
71
+ export const GraphTypeId: unique symbol = Symbol.for('@dxos/app-graph/Graph');
72
+ export type GraphTypeId = typeof GraphTypeId;
72
73
 
73
- export interface ReadableGraph {
74
+ /**
75
+ * Identifier for the graph kind discriminator.
76
+ */
77
+ export const GraphKind: unique symbol = Symbol.for('@dxos/app-graph/GraphKind');
78
+ export type GraphKind = typeof GraphKind;
79
+
80
+ export type GraphKindType = 'readable' | 'expandable' | 'writable';
81
+
82
+ export interface BaseGraph extends Pipeable.Pipeable {
83
+ readonly [GraphTypeId]: GraphTypeId;
84
+ readonly [GraphKind]: GraphKindType;
74
85
  /**
75
86
  * Event emitted when a node is changed.
76
87
  */
77
- onNodeChanged: Event<{ id: string; node: Option.Option<Node> }>;
78
-
88
+ readonly onNodeChanged: Event<{ id: string; node: Option.Option<Node.Node> }>;
79
89
  /**
80
- * Convert the graph to a JSON object.
90
+ * Get the atom key for the JSON representation of the graph.
81
91
  */
82
- toJSON(id?: string): object;
83
-
84
92
  json(id?: string): Atom.Atom<any>;
85
-
86
93
  /**
87
94
  * Get the atom key for the node with the given id.
88
95
  */
89
- node(id: string): Atom.Atom<Option.Option<Node>>;
90
-
96
+ node(id: string): Atom.Atom<Option.Option<Node.Node>>;
91
97
  /**
92
98
  * Get the atom key for the node with the given id.
93
99
  */
94
- nodeOrThrow(id: string): Atom.Atom<Node>;
95
-
100
+ nodeOrThrow(id: string): Atom.Atom<Node.Node>;
96
101
  /**
97
102
  * Get the atom key for the connections of the node with the given id.
98
103
  */
99
- connections(id: string, relation?: Relation): Atom.Atom<Node[]>;
100
-
104
+ connections(id: string, relation: Node.RelationInput): Atom.Atom<Node.Node[]>;
101
105
  /**
102
106
  * Get the atom key for the actions of the node with the given id.
103
107
  */
104
- actions(id: string): Atom.Atom<(Action | ActionGroup)[]>;
105
-
108
+ actions(id: string): Atom.Atom<(Node.Action | Node.ActionGroup)[]>;
106
109
  /**
107
110
  * Get the atom key for the edges of the node with the given id.
108
111
  */
109
112
  edges(id: string): Atom.Atom<Edges>;
110
-
111
- /**
112
- * Alias for `getNodeOrThrow(ROOT_ID)`.
113
- */
114
- get root(): Node;
115
-
116
- /**
117
- * Get the node with the given id from the graph's registry.
118
- */
119
- getNode(id: string): Option.Option<Node>;
120
-
121
- /**
122
- * Get the node with the given id from the graph's registry.
123
- *
124
- * @throws If the node is Option.none().
125
- */
126
- getNodeOrThrow(id: string): Node;
127
-
128
- /**
129
- * Get all nodes connected to the node with the given id by the given relation from the graph's registry.
130
- */
131
- getConnections(id: string, relation?: Relation): Node[];
132
-
133
- /**
134
- * Get all actions connected to the node with the given id from the graph's registry.
135
- */
136
- getActions(id: string): Node[];
137
-
138
- /**
139
- * Get the edges from the node with the given id from the graph's registry.
140
- */
141
- getEdges(id: string): Edges;
142
-
143
- /**
144
- * Recursive depth-first traversal of the graph.
145
- *
146
- * @param options.node The node to start traversing from.
147
- * @param options.relation The relation to traverse graph edges.
148
- * @param options.visitor A callback which is called for each node visited during traversal.
149
- */
150
- traverse(options: GraphTraversalOptions, path?: string[]): void;
151
-
152
- /**
153
- * Get the path between two nodes in the graph.
154
- */
155
- getPath(params: { source?: string; target: string }): Option.Option<string[]>;
156
-
157
- /**
158
- * Wait for the path between two nodes in the graph to be established.
159
- */
160
- waitForPath(
161
- params: { source?: string; target: string },
162
- options?: { timeout?: number; interval?: number },
163
- ): Promise<string[]>;
164
113
  }
165
114
 
166
- export interface ExpandableGraph extends ReadableGraph {
167
- /**
168
- * Initialize a node in the graph.
169
- *
170
- * Fires the `onInitialize` callback to provide initial data for a node.
171
- */
172
- initialize(id: string): Promise<void>;
173
-
174
- /**
175
- * Expand a node in the graph.
176
- *
177
- * Fires the `onExpand` callback to add connections to the node.
178
- */
179
- expand(id: string, relation?: Relation): void;
180
-
181
- /**
182
- * Sort the edges of the node with the given id.
183
- */
184
- sortEdges(id: string, relation: Relation, order: string[]): void;
185
- }
186
-
187
- export interface WritableGraph extends ExpandableGraph {
188
- /**
189
- * Add nodes to the graph.
190
- */
191
- addNodes(nodes: NodeArg<any, Record<string, any>>[]): void;
192
-
193
- /**
194
- * Add a node to the graph.
195
- */
196
- addNode(node: NodeArg<any, Record<string, any>>): void;
197
-
198
- /**
199
- * Remove nodes from the graph.
200
- */
201
- removeNodes(ids: string[], edges?: boolean): void;
202
-
203
- /**
204
- * Remove a node from the graph.
205
- */
206
- removeNode(id: string, edges?: boolean): void;
207
-
208
- /**
209
- * Add edges to the graph.
210
- */
211
- addEdges(edges: Edge[]): void;
212
-
213
- /**
214
- * Add an edge to the graph.
215
- */
216
- addEdge(edge: Edge): void;
115
+ export type ReadableGraph = BaseGraph & { readonly [GraphKind]: 'readable' | 'expandable' | 'writable' };
116
+ export type ExpandableGraph = BaseGraph & { readonly [GraphKind]: 'expandable' | 'writable' };
117
+ export type WritableGraph = BaseGraph & { readonly [GraphKind]: 'writable' };
217
118
 
218
- /**
219
- * Remove edges from the graph.
220
- */
221
- removeEdges(edges: Edge[], removeOrphans?: boolean): void;
222
-
223
- /**
224
- * Remove an edge from the graph.
225
- */
226
- removeEdge(edge: Edge, removeOrphans?: boolean): void;
227
- }
119
+ /**
120
+ * Graph interface.
121
+ */
122
+ export type Graph = WritableGraph;
228
123
 
229
124
  /**
230
125
  * The Graph represents the user interface information architecture of the application constructed via plugins.
126
+ * @internal
231
127
  */
232
- export class Graph implements WritableGraph {
128
+ class GraphImpl implements WritableGraph {
129
+ readonly [GraphTypeId]: GraphTypeId = GraphTypeId;
130
+ readonly [GraphKind] = 'writable' as const;
131
+
132
+ pipe() {
133
+ // eslint-disable-next-line prefer-rest-params
134
+ return Pipeable.pipeArguments(this, arguments);
135
+ }
136
+
233
137
  readonly onNodeChanged = new Event<{
234
138
  id: string;
235
- node: Option.Option<Node>;
139
+ node: Option.Option<Node.Node>;
236
140
  }>();
237
141
 
238
- private readonly _onExpand?: (id: string, relation: Relation) => void;
239
- private readonly _onInitialize?: (id: string) => Promise<void>;
240
- private readonly _onRemoveNode?: (id: string) => void;
142
+ readonly _onExpand?: GraphProps['onExpand'];
143
+ readonly _onInitialize?: GraphProps['onInitialize'];
144
+ readonly _onRemoveNode?: GraphProps['onRemoveNode'];
241
145
 
242
- private readonly _registry: Registry.Registry;
243
- private readonly _expanded = Record.empty<string, boolean>();
244
- private readonly _initialized = Record.empty<string, boolean>();
245
- private readonly _initialEdges = Record.empty<string, Edges>();
246
- private readonly _initialNodes = Record.fromEntries([
146
+ readonly _registry: Registry.Registry;
147
+ readonly _expanded = Record.empty<string, boolean>();
148
+ readonly _pendingExpands = new Set<string>();
149
+ readonly _initialized = Record.empty<string, boolean>();
150
+ readonly _initialEdges = Record.empty<string, Edges>();
151
+ readonly _initialNodes = Record.fromEntries([
247
152
  [
248
- ROOT_ID,
153
+ Node.RootId,
249
154
  this._constructNode({
250
- id: ROOT_ID,
251
- type: ROOT_TYPE,
155
+ id: Node.RootId,
156
+ type: Node.RootType,
252
157
  data: null,
253
158
  properties: {},
254
159
  }),
@@ -256,12 +161,12 @@ export class Graph implements WritableGraph {
256
161
  ]);
257
162
 
258
163
  /** @internal */
259
- readonly _node = Atom.family<string, Atom.Writable<Option.Option<Node>>>((id) => {
164
+ readonly _node = Atom.family<string, Atom.Writable<Option.Option<Node.Node>>>((id) => {
260
165
  const initial = Option.flatten(Record.get(this._initialNodes, id));
261
- return Atom.make<Option.Option<Node>>(initial).pipe(Atom.keepAlive, Atom.withLabel(`graph:node:${id}`));
166
+ return Atom.make<Option.Option<Node.Node>>(initial).pipe(Atom.keepAlive, Atom.withLabel(`graph:node:${id}`));
262
167
  });
263
168
 
264
- private readonly _nodeOrThrow = Atom.family<string, Atom.Atom<Node>>((id) => {
169
+ readonly _nodeOrThrow = Atom.family<string, Atom.Atom<Node.Node>>((id) => {
265
170
  return Atom.make((get) => {
266
171
  const node = get(this._node(id));
267
172
  invariant(Option.isSome(node), `Node not available: ${id}`);
@@ -269,36 +174,43 @@ export class Graph implements WritableGraph {
269
174
  });
270
175
  });
271
176
 
272
- private readonly _edges = Atom.family<string, Atom.Writable<Edges>>((id) => {
273
- const initial = Record.get(this._initialEdges, id).pipe(Option.getOrElse(() => ({ inbound: [], outbound: [] })));
177
+ readonly _edges = Atom.family<string, Atom.Writable<Edges>>((id) => {
178
+ const initial = Record.get(this._initialEdges, id).pipe(Option.getOrElse(() => ({}) as Edges));
274
179
  return Atom.make<Edges>(initial).pipe(Atom.keepAlive, Atom.withLabel(`graph:edges:${id}`));
275
180
  });
276
181
 
277
182
  // NOTE: Currently the argument to the family needs to be referentially stable for the atom to be referentially stable.
278
183
  // TODO(wittjosiah): Atom feature request, support for something akin to `ComplexMap` to allow for complex arguments.
279
- private readonly _connections = Atom.family<string, Atom.Atom<Node[]>>((key) => {
184
+ readonly _connections = Atom.family<string, Atom.Atom<Node.Node[]>>((key) => {
280
185
  return Atom.make((get) => {
281
- const [id, relation] = key.split('$');
186
+ const parts = key ? primaryParts(key) : [];
187
+ // Empty id (e.g. from `useConnections(graph, undefined, ...)`) yields a key like `\u0001child\u0002outbound`,
188
+ // which has 2 parts but an empty id — treat as no connections rather than throwing.
189
+ if (parts.length < 2 || !parts[0]) {
190
+ return [];
191
+ }
192
+ const { id, relation } = relationFromConnectionKey(key);
282
193
  const edges = get(this._edges(id));
283
- return edges[relation as Relation]
194
+ return (edges[relationKey(relation)] ?? [])
284
195
  .map((id) => get(this._node(id)))
285
196
  .filter(Option.isSome)
286
197
  .map((o) => o.value);
287
198
  }).pipe(Atom.withLabel(`graph:connections:${key}`));
288
199
  });
289
200
 
290
- private readonly _actions = Atom.family<string, Atom.Atom<(Action | ActionGroup)[]>>((id) => {
201
+ readonly _actions = Atom.family<string, Atom.Atom<(Node.Action | Node.ActionGroup)[]>>((id) => {
291
202
  return Atom.make((get) => {
292
- return get(this._connections(`${id}$outbound`)).filter(
293
- (node) => node.type === ACTION_TYPE || node.type === ACTION_GROUP_TYPE,
294
- );
203
+ if (!id) {
204
+ return [];
205
+ }
206
+ return get(this._connections(connectionKey(id, Node.actionRelation()))) as (Node.Action | Node.ActionGroup)[];
295
207
  }).pipe(Atom.withLabel(`graph:actions:${id}`));
296
208
  });
297
209
 
298
- private readonly _json = Atom.family<string, Atom.Atom<any>>((id) => {
210
+ readonly _json = Atom.family<string, Atom.Atom<any>>((id) => {
299
211
  return Atom.make((get) => {
300
- const toJSON = (node: Node, seen: string[] = []): any => {
301
- const nodes = get(this.connections(node.id));
212
+ const toJSON = (node: Node.Node, seen: string[] = []): any => {
213
+ const nodes = get(this._connections(connectionKey(node.id, 'child')));
302
214
  const obj: Record<string, any> = {
303
215
  id: node.id,
304
216
  type: node.type,
@@ -308,7 +220,7 @@ export class Graph implements WritableGraph {
308
220
  }
309
221
  if (nodes.length) {
310
222
  obj.nodes = nodes
311
- .map((n) => {
223
+ .map((n: Node.Node) => {
312
224
  // Break cycles.
313
225
  const nextSeen = [...seen, node.id];
314
226
  return nextSeen.includes(n.id) ? undefined : toJSON(n, nextSeen);
@@ -318,12 +230,12 @@ export class Graph implements WritableGraph {
318
230
  return obj;
319
231
  };
320
232
 
321
- const root = get(this.nodeOrThrow(id));
233
+ const root = get(this._nodeOrThrow(id));
322
234
  return toJSON(root);
323
235
  }).pipe(Atom.withLabel(`graph:json:${id}`));
324
236
  });
325
237
 
326
- constructor({ registry, nodes, edges, onInitialize, onExpand, onRemoveNode }: GraphParams = {}) {
238
+ constructor({ registry, nodes, edges, onInitialize, onExpand, onRemoveNode }: GraphProps = {}) {
327
239
  this._registry = registry ?? Registry.make();
328
240
  this._onInitialize = onInitialize;
329
241
  this._onExpand = onExpand;
@@ -342,303 +254,1000 @@ export class Graph implements WritableGraph {
342
254
  }
343
255
  }
344
256
 
345
- toJSON(id = ROOT_ID) {
346
- return this._registry.get(this._json(id));
257
+ json(id = Node.RootId): Atom.Atom<any> {
258
+ return jsonImpl(this, id);
347
259
  }
348
260
 
349
- json(id = ROOT_ID) {
350
- return this._json(id);
261
+ node(id: string): Atom.Atom<Option.Option<Node.Node>> {
262
+ return nodeImpl(this, id);
351
263
  }
352
264
 
353
- node(id: string): Atom.Atom<Option.Option<Node>> {
354
- return this._node(id);
265
+ nodeOrThrow(id: string): Atom.Atom<Node.Node> {
266
+ return nodeOrThrowImpl(this, id);
355
267
  }
356
268
 
357
- nodeOrThrow(id: string): Atom.Atom<Node> {
358
- return this._nodeOrThrow(id);
269
+ connections(id: string, relation: Node.RelationInput): Atom.Atom<Node.Node[]> {
270
+ return connectionsImpl(this, id, relation);
359
271
  }
360
272
 
361
- connections(id: string, relation: Relation = 'outbound'): Atom.Atom<Node[]> {
362
- return this._connections(`${id}$${relation}`);
273
+ actions(id: string): Atom.Atom<(Node.Action | Node.ActionGroup)[]> {
274
+ return actionsImpl(this, id);
363
275
  }
364
276
 
365
- actions(id: string) {
366
- return this._actions(id);
277
+ edges(id: string): Atom.Atom<Edges> {
278
+ return edgesImpl(this, id);
367
279
  }
368
280
 
369
- edges(id: string): Atom.Atom<Edges> {
370
- return this._edges(id);
281
+ /** @internal */
282
+ _constructNode(node: Node.NodeArg<any>): Option.Option<Node.Node> {
283
+ return Option.some({
284
+ [graphSymbol]: this,
285
+ data: null,
286
+ properties: {},
287
+ ...node,
288
+ });
371
289
  }
290
+ }
291
+
292
+ /**
293
+ * Internal helper to access GraphImpl internals.
294
+ * @internal
295
+ */
296
+ const getInternal = (graph: BaseGraph): GraphImpl => {
297
+ return graph as unknown as GraphImpl;
298
+ };
299
+
300
+ /**
301
+ * Convert the graph to a JSON object.
302
+ */
303
+ export const toJSON = (graph: BaseGraph, id = Node.RootId): object => {
304
+ const internal = getInternal(graph);
305
+ return internal._registry.get(internal._json(id));
306
+ };
307
+
308
+ /**
309
+ * Implementation helper for json.
310
+ */
311
+ const jsonImpl = (graph: BaseGraph, id = Node.RootId): Atom.Atom<any> => {
312
+ const internal = getInternal(graph);
313
+ return internal._json(id);
314
+ };
315
+
316
+ /**
317
+ * Implementation helper for node.
318
+ */
319
+ const nodeImpl = (graph: BaseGraph, id: string): Atom.Atom<Option.Option<Node.Node>> => {
320
+ const internal = getInternal(graph);
321
+ return internal._node(id);
322
+ };
323
+
324
+ /**
325
+ * Implementation helper for nodeOrThrow.
326
+ */
327
+ const nodeOrThrowImpl = (graph: BaseGraph, id: string): Atom.Atom<Node.Node> => {
328
+ const internal = getInternal(graph);
329
+ return internal._nodeOrThrow(id);
330
+ };
331
+
332
+ /**
333
+ * Implementation helper for connections.
334
+ */
335
+ const connectionsImpl = (graph: BaseGraph, id: string, relation: Node.RelationInput): Atom.Atom<Node.Node[]> => {
336
+ const internal = getInternal(graph);
337
+ return internal._connections(connectionKey(id, relation));
338
+ };
339
+
340
+ /**
341
+ * Implementation helper for actions.
342
+ */
343
+ const actionsImpl = (graph: BaseGraph, id: string): Atom.Atom<(Node.Action | Node.ActionGroup)[]> => {
344
+ const internal = getInternal(graph);
345
+ return internal._actions(id);
346
+ };
347
+
348
+ /**
349
+ * Implementation helper for edges.
350
+ */
351
+ const edgesImpl = (graph: BaseGraph, id: string): Atom.Atom<Edges> => {
352
+ const internal = getInternal(graph);
353
+ return internal._edges(id);
354
+ };
355
+
356
+ /**
357
+ * Implementation helper for getNode.
358
+ */
359
+ const getNodeImpl = (graph: BaseGraph, id: string): Option.Option<Node.Node> => {
360
+ const internal = getInternal(graph);
361
+ return internal._registry.get(nodeImpl(graph, id));
362
+ };
372
363
 
373
- get root() {
374
- return this.getNodeOrThrow(ROOT_ID);
364
+ /**
365
+ * Get the node with the given id from the graph's registry.
366
+ */
367
+ export function getNode(graph: BaseGraph, id: string): Option.Option<Node.Node>;
368
+ export function getNode(id: string): (graph: BaseGraph) => Option.Option<Node.Node>;
369
+ export function getNode(
370
+ graphOrId: BaseGraph | string,
371
+ id?: string,
372
+ ): Option.Option<Node.Node> | ((graph: BaseGraph) => Option.Option<Node.Node>) {
373
+ if (typeof graphOrId === 'string') {
374
+ // Curried: getNode(id)
375
+ const id = graphOrId;
376
+ return (graph: BaseGraph) => getNodeImpl(graph, id);
377
+ } else {
378
+ // Direct: getNode(graph, id)
379
+ const graph = graphOrId;
380
+ return getNodeImpl(graph, id!);
375
381
  }
382
+ }
383
+
384
+ /**
385
+ * Implementation helper for getNodeOrThrow.
386
+ */
387
+ const getNodeOrThrowImpl = (graph: BaseGraph, id: string): Node.Node => {
388
+ const internal = getInternal(graph);
389
+ return internal._registry.get(nodeOrThrowImpl(graph, id));
390
+ };
376
391
 
377
- getNode(id: string): Option.Option<Node> {
378
- return this._registry.get(this.node(id));
392
+ /**
393
+ * Get the node with the given id from the graph's registry.
394
+ *
395
+ * @throws If the node is Option.none().
396
+ */
397
+ export function getNodeOrThrow(graph: BaseGraph, id: string): Node.Node;
398
+ export function getNodeOrThrow(id: string): (graph: BaseGraph) => Node.Node;
399
+ export function getNodeOrThrow(
400
+ graphOrId: BaseGraph | string,
401
+ id?: string,
402
+ ): Node.Node | ((graph: BaseGraph) => Node.Node) {
403
+ if (typeof graphOrId === 'string') {
404
+ // Curried: getNodeOrThrow(id)
405
+ const id = graphOrId;
406
+ return (graph: BaseGraph) => getNodeOrThrowImpl(graph, id);
407
+ } else {
408
+ // Direct: getNodeOrThrow(graph, id)
409
+ const graph = graphOrId;
410
+ return getNodeOrThrowImpl(graph, id!);
379
411
  }
412
+ }
380
413
 
381
- getNodeOrThrow(id: string): Node {
382
- return this._registry.get(this.nodeOrThrow(id));
414
+ /**
415
+ * Get the root node of the graph.
416
+ * This is an alias for `getNodeOrThrow(graph, ROOT_ID)`.
417
+ */
418
+ export function getRoot(graph: BaseGraph): Node.Node {
419
+ return getNodeOrThrowImpl(graph, Node.RootId);
420
+ }
421
+
422
+ /**
423
+ * Implementation helper for getConnections.
424
+ */
425
+ const getConnectionsImpl = (graph: BaseGraph, id: string, relation: Node.RelationInput): Node.Node[] => {
426
+ const internal = getInternal(graph);
427
+ return internal._registry.get(connectionsImpl(graph, id, relation));
428
+ };
429
+
430
+ /**
431
+ * Get all nodes connected to the node with the given id by the given relation from the graph's registry.
432
+ */
433
+ export function getConnections(graph: BaseGraph, id: string, relation: Node.RelationInput): Node.Node[];
434
+ export function getConnections(id: string, relation: Node.RelationInput): (graph: BaseGraph) => Node.Node[];
435
+ export function getConnections(
436
+ graphOrId: BaseGraph | string,
437
+ idOrRelation: string | Node.RelationInput,
438
+ relation?: Node.RelationInput,
439
+ ): Node.Node[] | ((graph: BaseGraph) => Node.Node[]) {
440
+ if (typeof graphOrId === 'string') {
441
+ // Curried: getConnections(id, relation)
442
+ const id = graphOrId;
443
+ const rel = idOrRelation as Node.RelationInput;
444
+ return (graph: BaseGraph) => getConnectionsImpl(graph, id, rel);
445
+ } else {
446
+ // Direct: getConnections(graph, id, relation)
447
+ const graph = graphOrId;
448
+ const id = idOrRelation as string;
449
+ invariant(relation !== undefined, 'Relation is required.');
450
+ const rel = relation;
451
+ return getConnectionsImpl(graph, id, rel);
383
452
  }
453
+ }
384
454
 
385
- getConnections(id: string, relation: Relation = 'outbound'): Node[] {
386
- return this._registry.get(this.connections(id, relation));
455
+ /**
456
+ * Implementation helper for getActions.
457
+ */
458
+ const getActionsImpl = (graph: BaseGraph, id: string): Node.Node[] => {
459
+ const internal = getInternal(graph);
460
+ return internal._registry.get(actionsImpl(graph, id));
461
+ };
462
+
463
+ /**
464
+ * Get all actions connected to the node with the given id from the graph's registry.
465
+ */
466
+ export function getActions(graph: BaseGraph, id: string): Node.Node[];
467
+ export function getActions(id: string): (graph: BaseGraph) => Node.Node[];
468
+ export function getActions(
469
+ graphOrId: BaseGraph | string,
470
+ id?: string,
471
+ ): Node.Node[] | ((graph: BaseGraph) => Node.Node[]) {
472
+ if (typeof graphOrId === 'string') {
473
+ // Curried: getActions(id)
474
+ const id = graphOrId;
475
+ return (graph: BaseGraph) => getActionsImpl(graph, id);
476
+ } else {
477
+ // Direct: getActions(graph, id)
478
+ const graph = graphOrId;
479
+ return getActionsImpl(graph, id!);
387
480
  }
481
+ }
388
482
 
389
- getActions(id: string): Node[] {
390
- return this._registry.get(this.actions(id));
483
+ /**
484
+ * Implementation helper for getEdges.
485
+ */
486
+ const getEdgesImpl = (graph: BaseGraph, id: string): Edges => {
487
+ const internal = getInternal(graph);
488
+ return internal._registry.get(edgesImpl(graph, id));
489
+ };
490
+
491
+ /**
492
+ * Get the edges from the node with the given id from the graph's registry.
493
+ */
494
+ export function getEdges(graph: BaseGraph, id: string): Edges;
495
+ export function getEdges(id: string): (graph: BaseGraph) => Edges;
496
+ export function getEdges(graphOrId: BaseGraph | string, id?: string): Edges | ((graph: BaseGraph) => Edges) {
497
+ if (typeof graphOrId === 'string') {
498
+ // Curried: getEdges(id)
499
+ const id = graphOrId;
500
+ return (graph: BaseGraph) => getEdgesImpl(graph, id);
501
+ } else {
502
+ // Direct: getEdges(graph, id)
503
+ const graph = graphOrId;
504
+ return getEdgesImpl(graph, id!);
391
505
  }
506
+ }
392
507
 
393
- getEdges(id: string): Edges {
394
- return this._registry.get(this.edges(id));
508
+ /**
509
+ * Recursive depth-first traversal of the graph.
510
+ */
511
+ /**
512
+ * Implementation helper for traverse.
513
+ */
514
+ const traverseImpl = (graph: BaseGraph, options: GraphTraversalOptions, path: string[] = []): void => {
515
+ const { visitor, source = Node.RootId, relation } = options;
516
+ // Break cycles.
517
+ if (path.includes(source)) {
518
+ return;
395
519
  }
396
520
 
397
- async initialize(id: string) {
398
- const initialized = Record.get(this._initialized, id).pipe(Option.getOrElse(() => false));
399
- log('initialize', { id, initialized });
400
- if (!initialized) {
401
- await this._onInitialize?.(id);
402
- Record.set(this._initialized, id, true);
403
- }
521
+ const node = getNodeOrThrow(graph, source);
522
+ const shouldContinue = visitor(node, [...path, source]);
523
+ if (shouldContinue === false) {
524
+ return;
404
525
  }
405
526
 
406
- expand(id: string, relation: Relation = 'outbound'): void {
407
- const key = `${id}$${relation}`;
408
- const expanded = Record.get(this._expanded, key).pipe(Option.getOrElse(() => false));
409
- log('expand', { key, expanded });
410
- if (!expanded) {
411
- this._onExpand?.(id, relation);
412
- Record.set(this._expanded, key, true);
527
+ const relations = Array.isArray(relation) ? relation : [relation];
528
+ const seen = new Set<string>();
529
+ for (const rel of relations) {
530
+ for (const connected of getConnections(graph, source, rel)) {
531
+ if (!seen.has(connected.id)) {
532
+ seen.add(connected.id);
533
+ traverseImpl(graph, { source: connected.id, relation, visitor }, [...path, source]);
534
+ }
413
535
  }
414
536
  }
537
+ };
415
538
 
416
- addNodes(nodes: NodeArg<any, Record<string, any>>[]): void {
417
- Atom.batch(() => {
418
- nodes.map((node) => this.addNode(node));
419
- });
539
+ /**
540
+ * Traverse the graph with the given options.
541
+ */
542
+ export function traverse(graph: BaseGraph, options: GraphTraversalOptions, path?: string[]): void;
543
+ export function traverse(options: GraphTraversalOptions, path?: string[]): (graph: BaseGraph) => void;
544
+ export function traverse(
545
+ graphOrOptions: BaseGraph | GraphTraversalOptions,
546
+ optionsOrPath?: GraphTraversalOptions | string[],
547
+ path?: string[],
548
+ ): void | ((graph: BaseGraph) => void) {
549
+ if (typeof graphOrOptions === 'object' && 'visitor' in graphOrOptions) {
550
+ // Curried: traverse(options, path?)
551
+ const options = graphOrOptions as GraphTraversalOptions;
552
+ const pathArg = Array.isArray(optionsOrPath) ? optionsOrPath : undefined;
553
+ return (graph: BaseGraph) => traverseImpl(graph, options, pathArg);
554
+ } else {
555
+ // Direct: traverse(graph, options, path?)
556
+ const graph = graphOrOptions as BaseGraph;
557
+ const options = optionsOrPath as GraphTraversalOptions;
558
+ const pathArg = path ?? (Array.isArray(optionsOrPath) ? optionsOrPath : undefined);
559
+ return traverseImpl(graph, options, pathArg);
420
560
  }
561
+ }
421
562
 
422
- addNode({ nodes, edges, ...nodeArg }: NodeArg<any, Record<string, any>>): void {
423
- const { id, type, data = null, properties = {} } = nodeArg;
424
- const nodeAtom = this._node(id);
425
- const node = this._registry.get(nodeAtom);
426
- Option.match(node, {
427
- onSome: (node) => {
428
- const typeChanged = node.type !== type;
429
- const dataChanged = node.data !== data;
430
- const propertiesChanged = Object.keys(properties).some((key) => node.properties[key] !== properties[key]);
431
- log('existing node', {
432
- id,
433
- typeChanged,
434
- dataChanged,
435
- propertiesChanged,
436
- });
437
- if (typeChanged || dataChanged || propertiesChanged) {
438
- log('updating node', { id, type, data, properties });
439
- const newNode = Option.some({
440
- ...node,
441
- type,
442
- data,
443
- properties: { ...node.properties, ...properties },
444
- });
445
- this._registry.set(nodeAtom, newNode);
446
- this.onNodeChanged.emit({ id, node: newNode });
447
- }
448
- },
449
- onNone: () => {
450
- log('new node', { id, type, data, properties });
451
- const newNode = this._constructNode({ id, type, data, properties });
452
- this._registry.set(nodeAtom, newNode);
453
- this.onNodeChanged.emit({ id, node: newNode });
454
- },
455
- });
563
+ /**
564
+ * Implementation helper for getPath.
565
+ */
566
+ const getPathImpl = (graph: BaseGraph, params: { source?: string; target: string }): Option.Option<string[]> => {
567
+ return Function.pipe(
568
+ getNode(graph, params.source ?? 'root'),
569
+ Option.flatMap((node) => {
570
+ let found: Option.Option<string[]> = Option.none();
571
+ traverseImpl(graph, {
572
+ source: node.id,
573
+ relation: 'child',
574
+ visitor: (node, path) => {
575
+ if (Option.isSome(found)) {
576
+ return false;
577
+ }
578
+
579
+ if (node.id === params.target) {
580
+ found = Option.some(path);
581
+ }
582
+ },
583
+ });
456
584
 
457
- if (nodes) {
458
- // Atom.batch(() => {
459
- this.addNodes(nodes);
460
- const _edges = nodes.map((node) => ({ source: id, target: node.id }));
461
- this.addEdges(_edges);
462
- // });
463
- }
585
+ return found;
586
+ }),
587
+ );
588
+ };
464
589
 
465
- if (edges) {
466
- todo();
590
+ /**
591
+ * Get the path between two nodes in the graph.
592
+ */
593
+ export function getPath(graph: BaseGraph, params: { source?: string; target: string }): Option.Option<string[]>;
594
+ export function getPath(params: { source?: string; target: string }): (graph: BaseGraph) => Option.Option<string[]>;
595
+ export function getPath(
596
+ graphOrParams: BaseGraph | { source?: string; target: string },
597
+ params?: { source?: string; target: string },
598
+ ): Option.Option<string[]> | ((graph: BaseGraph) => Option.Option<string[]>) {
599
+ if (params === undefined && typeof graphOrParams === 'object' && 'target' in graphOrParams) {
600
+ // Curried: getPath(params)
601
+ const params = graphOrParams as { source?: string; target: string };
602
+ return (graph: BaseGraph) => getPathImpl(graph, params);
603
+ } else {
604
+ // Direct: getPath(graph, params)
605
+ const graph = graphOrParams as BaseGraph;
606
+ return getPathImpl(graph, params!);
607
+ }
608
+ }
609
+
610
+ /**
611
+ * Implementation helper for waitForPath.
612
+ */
613
+ const waitForPathImpl = (
614
+ graph: BaseGraph,
615
+ params: { source?: string; target: string },
616
+ options?: { timeout?: number; interval?: number },
617
+ ): Promise<string[]> => {
618
+ const { timeout = 5_000, interval = 500 } = options ?? {};
619
+ const path = getPathImpl(graph, params);
620
+ if (Option.isSome(path)) {
621
+ return Promise.resolve(path.value);
622
+ }
623
+
624
+ const trigger = new Trigger<string[]>();
625
+ const i = setInterval(() => {
626
+ const path = getPathImpl(graph, params);
627
+ if (Option.isSome(path)) {
628
+ trigger.wake(path.value);
467
629
  }
630
+ }, interval);
631
+
632
+ return trigger.wait({ timeout }).finally(() => clearInterval(i));
633
+ };
634
+
635
+ /**
636
+ * Wait for the path between two nodes in the graph to be established.
637
+ */
638
+ export function waitForPath(
639
+ graph: BaseGraph,
640
+ params: { source?: string; target: string },
641
+ options?: { timeout?: number; interval?: number },
642
+ ): Promise<string[]>;
643
+ export function waitForPath(
644
+ params: { source?: string; target: string },
645
+ options?: { timeout?: number; interval?: number },
646
+ ): (graph: BaseGraph) => Promise<string[]>;
647
+ export function waitForPath(
648
+ graphOrParams: BaseGraph | { source?: string; target: string },
649
+ paramsOrOptions?: { source?: string; target: string } | { timeout?: number; interval?: number },
650
+ options?: { timeout?: number; interval?: number },
651
+ ): Promise<string[]> | ((graph: BaseGraph) => Promise<string[]>) {
652
+ if (typeof graphOrParams === 'object' && 'target' in graphOrParams) {
653
+ // Curried: waitForPath(params, options?)
654
+ const params = graphOrParams as { source?: string; target: string };
655
+ const opts = typeof paramsOrOptions === 'object' && !('target' in paramsOrOptions) ? paramsOrOptions : undefined;
656
+ return (graph: BaseGraph) => waitForPathImpl(graph, params, opts);
657
+ } else {
658
+ // Direct: waitForPath(graph, params, options?)
659
+ const graph = graphOrParams as BaseGraph;
660
+ const params = paramsOrOptions as { source?: string; target: string };
661
+ return waitForPathImpl(graph, params, options);
468
662
  }
663
+ }
469
664
 
470
- removeNodes(ids: string[], edges = false): void {
471
- Atom.batch(() => {
472
- ids.map((id) => this.removeNode(id, edges));
473
- });
665
+ /**
666
+ * Implementation helper for initialize.
667
+ */
668
+ const initializeImpl = async <T extends ExpandableGraph | WritableGraph>(graph: T, id: string): Promise<T> => {
669
+ const internal = getInternal(graph);
670
+ const initialized = Record.get(internal._initialized, id).pipe(Option.getOrElse(() => false));
671
+ log('initialize', { id, initialized });
672
+ if (!initialized) {
673
+ Record.set(internal._initialized, id, true);
674
+ await internal._onInitialize?.(id);
474
675
  }
676
+ return graph;
677
+ };
475
678
 
476
- removeNode(id: string, edges = false): void {
477
- const nodeAtom = this._node(id);
478
- // TODO(wittjosiah): Is there a way to mark these atom values for garbage collection?
479
- this._registry.set(nodeAtom, Option.none());
480
- this.onNodeChanged.emit({ id, node: Option.none() });
481
- // TODO(wittjosiah): Reset expanded and initialized flags?
679
+ /**
680
+ * Initialize a node in the graph.
681
+ *
682
+ * Fires the `onInitialize` callback to provide initial data for a node.
683
+ */
684
+ export function initialize<T extends ExpandableGraph | WritableGraph>(graph: T, id: string): Promise<T>;
685
+ export function initialize(id: string): <T extends ExpandableGraph | WritableGraph>(graph: T) => Promise<T>;
686
+ export function initialize<T extends ExpandableGraph | WritableGraph>(
687
+ graphOrId: T | string,
688
+ id?: string,
689
+ ): Promise<T> | (<T extends ExpandableGraph | WritableGraph>(graph: T) => Promise<T>) {
690
+ if (typeof graphOrId === 'string') {
691
+ // Curried: initialize(id)
692
+ const id = graphOrId;
693
+ return <T extends ExpandableGraph | WritableGraph>(graph: T) => initializeImpl(graph, id);
694
+ } else {
695
+ // Direct: initialize(graph, id)
696
+ const graph = graphOrId;
697
+ return initializeImpl(graph, id!);
698
+ }
699
+ }
482
700
 
483
- if (edges) {
484
- const { inbound, outbound } = this._registry.get(this._edges(id));
485
- const edges = [
486
- ...inbound.map((source) => ({ source, target: id })),
487
- ...outbound.map((target) => ({ source: id, target })),
488
- ];
489
- this.removeEdges(edges);
490
- }
701
+ /**
702
+ * Implementation helper for expand.
703
+ * If the node does not exist yet, the expand is recorded as pending and applied when the node is added.
704
+ */
705
+ const expandImpl = <T extends ExpandableGraph | WritableGraph>(
706
+ graph: T,
707
+ id: string,
708
+ relation: Node.RelationInput,
709
+ ): T => {
710
+ const internal = getInternal(graph);
711
+ const normalizedRelation = normalizeRelation(relation);
712
+ const key = primaryKey(id, relationKey(normalizedRelation));
713
+ const nodeOpt = internal._registry.get(internal._node(id));
714
+ if (Option.isNone(nodeOpt)) {
715
+ // Node not yet in graph: record expand to run when the node is added.
716
+ internal._pendingExpands.add(key);
717
+ log('expand', { key, deferred: true });
718
+ return graph;
719
+ }
491
720
 
492
- this._onRemoveNode?.(id);
721
+ const expanded = Record.get(internal._expanded, key).pipe(Option.getOrElse(() => false));
722
+ log('expand', { key, expanded });
723
+ if (!expanded) {
724
+ Record.set(internal._expanded, key, true);
725
+ internal._onExpand?.(id, normalizedRelation);
493
726
  }
727
+ return graph;
728
+ };
494
729
 
495
- addEdges(edges: Edge[]): void {
496
- Atom.batch(() => {
497
- edges.map((edge) => this.addEdge(edge));
498
- });
730
+ /**
731
+ * Expand a node in the graph.
732
+ *
733
+ * Fires the `onExpand` callback to add connections to the node.
734
+ */
735
+ export function expand<T extends ExpandableGraph | WritableGraph>(
736
+ graph: T,
737
+ id: string,
738
+ relation: Node.RelationInput,
739
+ ): T;
740
+ export function expand(
741
+ id: string,
742
+ relation: Node.RelationInput,
743
+ ): <T extends ExpandableGraph | WritableGraph>(graph: T) => T;
744
+ export function expand<T extends ExpandableGraph | WritableGraph>(
745
+ graphOrId: T | string,
746
+ idOrRelation: string | Node.RelationInput,
747
+ relation?: Node.RelationInput,
748
+ ): T | (<T extends ExpandableGraph | WritableGraph>(graph: T) => T) {
749
+ if (typeof graphOrId === 'string') {
750
+ // Curried: expand(id, relation)
751
+ const id = graphOrId;
752
+ const rel = idOrRelation as Node.RelationInput;
753
+ return <T extends ExpandableGraph | WritableGraph>(graph: T) => expandImpl(graph, id, rel);
754
+ } else {
755
+ // Direct: expand(graph, id, relation)
756
+ const graph = graphOrId;
757
+ const id = idOrRelation as string;
758
+ invariant(relation !== undefined, 'Relation is required.');
759
+ const rel = relation;
760
+ return expandImpl(graph, id, rel);
499
761
  }
762
+ }
500
763
 
501
- addEdge(edgeArg: Edge): void {
502
- const sourceAtom = this._edges(edgeArg.source);
503
- const source = this._registry.get(sourceAtom);
504
- if (!source.outbound.includes(edgeArg.target)) {
505
- log('add outbound edge', {
506
- source: edgeArg.source,
507
- target: edgeArg.target,
508
- });
509
- this._registry.set(sourceAtom, {
510
- inbound: source.inbound,
511
- outbound: [...source.outbound, edgeArg.target],
512
- });
513
- }
764
+ /**
765
+ * Implementation helper for sortEdges.
766
+ */
767
+ const sortEdgesImpl = <T extends ExpandableGraph | WritableGraph>(
768
+ graph: T,
769
+ id: string,
770
+ relation: Node.RelationInput,
771
+ order: string[],
772
+ ): T => {
773
+ const internal = getInternal(graph);
774
+ const edgesAtom = internal._edges(id);
775
+ const edges = internal._registry.get(edgesAtom);
776
+ const relationId = relationKey(relation);
777
+ const current = edges[relationId] ?? [];
778
+ const unsorted = current.filter((id) => !order.includes(id));
779
+ const sorted = order.filter((id) => current.includes(id));
780
+ const newOrder = [...sorted, ...unsorted];
781
+ if (newOrder.length === current.length && newOrder.every((id, i) => id === current[i])) {
782
+ return graph;
783
+ }
784
+ internal._registry.set(edgesAtom, {
785
+ ...edges,
786
+ [relationId]: newOrder,
787
+ });
788
+ return graph;
789
+ };
514
790
 
515
- const targetAtom = this._edges(edgeArg.target);
516
- const target = this._registry.get(targetAtom);
517
- if (!target.inbound.includes(edgeArg.source)) {
518
- log('add inbound edge', {
519
- source: edgeArg.source,
520
- target: edgeArg.target,
521
- });
522
- this._registry.set(targetAtom, {
523
- inbound: [...target.inbound, edgeArg.source],
524
- outbound: target.outbound,
525
- });
526
- }
791
+ /**
792
+ * Sort the edges of the node with the given id.
793
+ */
794
+ export function sortEdges<T extends ExpandableGraph | WritableGraph>(
795
+ graph: T,
796
+ id: string,
797
+ relation: Node.RelationInput,
798
+ order: string[],
799
+ ): T;
800
+ export function sortEdges(
801
+ id: string,
802
+ relation: Node.RelationInput,
803
+ order: string[],
804
+ ): <T extends ExpandableGraph | WritableGraph>(graph: T) => T;
805
+ export function sortEdges<T extends ExpandableGraph | WritableGraph>(
806
+ graphOrId: T | string,
807
+ idOrRelation?: string | Node.RelationInput,
808
+ relationOrOrder?: Node.RelationInput | string[],
809
+ order?: string[],
810
+ ): T | (<T extends ExpandableGraph | WritableGraph>(graph: T) => T) {
811
+ if (typeof graphOrId === 'string') {
812
+ // Curried: sortEdges(id, relation, order)
813
+ const id = graphOrId;
814
+ const relation = idOrRelation as Node.RelationInput;
815
+ const order = relationOrOrder as string[];
816
+ return <T extends ExpandableGraph | WritableGraph>(graph: T) => sortEdgesImpl(graph, id, relation, order);
817
+ } else {
818
+ // Direct: sortEdges(graph, id, relation, order)
819
+ const graph = graphOrId;
820
+ const id = idOrRelation as string;
821
+ const relation = relationOrOrder as Node.RelationInput;
822
+ return sortEdgesImpl(graph, id, relation, order!);
527
823
  }
824
+ }
528
825
 
529
- removeEdges(edges: Edge[], removeOrphans = false): void {
530
- Atom.batch(() => {
531
- edges.map((edge) => this.removeEdge(edge, removeOrphans));
532
- });
826
+ /**
827
+ * Implementation helper for addNodes.
828
+ */
829
+ const addNodesImpl = <T extends WritableGraph>(graph: T, nodes: Node.NodeArg<any, Record<string, any>>[]): T => {
830
+ Atom.batch(() => {
831
+ nodes.map((node) => addNodeImpl(graph, node));
832
+ });
833
+ return graph;
834
+ };
835
+
836
+ /**
837
+ * Add nodes to the graph.
838
+ */
839
+ export function addNodes<T extends WritableGraph>(graph: T, nodes: Node.NodeArg<any, Record<string, any>>[]): T;
840
+ export function addNodes(nodes: Node.NodeArg<any, Record<string, any>>[]): <T extends WritableGraph>(graph: T) => T;
841
+ export function addNodes<T extends WritableGraph>(
842
+ graphOrNodes: T | Node.NodeArg<any, Record<string, any>>[],
843
+ nodes?: Node.NodeArg<any, Record<string, any>>[],
844
+ ): T | (<T extends WritableGraph>(graph: T) => T) {
845
+ if (nodes === undefined) {
846
+ // Curried: addNodes(nodes)
847
+ const nodes = graphOrNodes as Node.NodeArg<any, Record<string, any>>[];
848
+ return <T extends WritableGraph>(graph: T) => addNodesImpl(graph, nodes);
849
+ } else {
850
+ // Direct: addNodes(graph, nodes)
851
+ const graph = graphOrNodes as T;
852
+ return addNodesImpl(graph, nodes);
533
853
  }
854
+ }
534
855
 
535
- removeEdge(edgeArg: Edge, removeOrphans = false): void {
536
- const sourceAtom = this._edges(edgeArg.source);
537
- const source = this._registry.get(sourceAtom);
538
- if (source.outbound.includes(edgeArg.target)) {
539
- this._registry.set(sourceAtom, {
540
- inbound: source.inbound,
541
- outbound: source.outbound.filter((id) => id !== edgeArg.target),
856
+ /**
857
+ * Implementation helper for addNode.
858
+ */
859
+ const addNodeImpl = <T extends WritableGraph>(graph: T, nodeArg: Node.NodeArg<any, Record<string, any>>): T => {
860
+ const internal = getInternal(graph);
861
+ // Extract known NodeArg fields, preserve any extra fields (like _actionContext) in rest.
862
+ const {
863
+ nodes,
864
+ edges,
865
+ id,
866
+ type,
867
+ data = null,
868
+ properties = {},
869
+ ...rest
870
+ } = nodeArg as Node.NodeArg<any> & {
871
+ _actionContext?: Node.ActionContext;
872
+ };
873
+ const nodeAtom = internal._node(id);
874
+ const existingNode = internal._registry.get(nodeAtom);
875
+ Option.match(existingNode, {
876
+ onSome: (existing) => {
877
+ const typeChanged = existing.type !== type;
878
+ const dataChanged = !shallowEqual(existing.data, data);
879
+ const propertiesChanged = Object.keys(properties).some((key) => existing.properties[key] !== properties[key]);
880
+ log('existing node', {
881
+ id,
882
+ typeChanged,
883
+ dataChanged,
884
+ propertiesChanged,
542
885
  });
543
- }
886
+ if (typeChanged || dataChanged || propertiesChanged) {
887
+ log('updating node', { id, type, data, properties });
888
+ const newNode = Option.some({
889
+ ...existing,
890
+ ...rest,
891
+ type,
892
+ data,
893
+ properties: { ...existing.properties, ...properties },
894
+ });
895
+ internal._registry.set(nodeAtom, newNode);
896
+ graph.onNodeChanged.emit({ id, node: newNode });
897
+ }
898
+ },
899
+ onNone: () => {
900
+ log('new node', { id, type, data, properties });
901
+ const newNode = internal._constructNode({ id, type, data, properties, ...rest });
902
+ internal._registry.set(nodeAtom, newNode);
903
+ graph.onNodeChanged.emit({ id, node: newNode });
904
+
905
+ // Apply any expands that were deferred because this node did not exist yet.
906
+ const toApply = [...internal._pendingExpands].filter((k) => primaryParts(k)[0] === id);
907
+ for (const pendingKey of toApply) {
908
+ internal._pendingExpands.delete(pendingKey);
909
+ const relation = relationFromKey(primaryParts(pendingKey)[1]);
910
+ Record.set(internal._expanded, pendingKey, true);
911
+ internal._onExpand?.(id, relation);
912
+ }
913
+ },
914
+ });
544
915
 
545
- const targetAtom = this._edges(edgeArg.target);
546
- const target = this._registry.get(targetAtom);
547
- if (target.inbound.includes(edgeArg.source)) {
548
- this._registry.set(targetAtom, {
549
- inbound: target.inbound.filter((id) => id !== edgeArg.source),
550
- outbound: target.outbound,
551
- });
552
- }
916
+ if (nodes) {
917
+ addNodesImpl(graph, nodes);
918
+ const _edges = nodes.map((node) => ({ source: id, target: node.id, relation: 'child' as const }));
919
+ addEdgesImpl(graph, _edges);
920
+ }
553
921
 
554
- if (removeOrphans) {
555
- const source = this._registry.get(sourceAtom);
556
- const target = this._registry.get(targetAtom);
557
- if (source.outbound.length === 0 && source.inbound.length === 0 && edgeArg.source !== ROOT_ID) {
558
- this.removeNodes([edgeArg.source]);
559
- }
560
- if (target.outbound.length === 0 && target.inbound.length === 0 && edgeArg.target !== ROOT_ID) {
561
- this.removeNodes([edgeArg.target]);
922
+ if (edges) {
923
+ todo();
924
+ }
925
+ return graph;
926
+ };
927
+
928
+ /**
929
+ * Add a node to the graph.
930
+ */
931
+ export function addNode<T extends WritableGraph>(graph: T, nodeArg: Node.NodeArg<any, Record<string, any>>): T;
932
+ export function addNode(nodeArg: Node.NodeArg<any, Record<string, any>>): <T extends WritableGraph>(graph: T) => T;
933
+ export function addNode<T extends WritableGraph>(
934
+ graphOrNodeArg: T | Node.NodeArg<any, Record<string, any>>,
935
+ nodeArg?: Node.NodeArg<any, Record<string, any>>,
936
+ ): T | (<T extends WritableGraph>(graph: T) => T) {
937
+ if (nodeArg === undefined) {
938
+ // Curried: addNode(nodeArg)
939
+ const nodeArg = graphOrNodeArg as Node.NodeArg<any, Record<string, any>>;
940
+ return <T extends WritableGraph>(graph: T) => addNodeImpl(graph, nodeArg);
941
+ } else {
942
+ // Direct: addNode(graph, nodeArg)
943
+ const graph = graphOrNodeArg as T;
944
+ return addNodeImpl(graph, nodeArg);
945
+ }
946
+ }
947
+
948
+ /**
949
+ * Implementation helper for removeNodes.
950
+ */
951
+ const removeNodesImpl = <T extends WritableGraph>(graph: T, ids: string[], edges = false): T => {
952
+ Atom.batch(() => {
953
+ ids.map((id) => removeNodeImpl(graph, id, edges));
954
+ });
955
+ return graph;
956
+ };
957
+
958
+ /**
959
+ * Remove nodes from the graph.
960
+ */
961
+ export function removeNodes<T extends WritableGraph>(graph: T, ids: string[], edges?: boolean): T;
962
+ export function removeNodes(ids: string[], edges?: boolean): <T extends WritableGraph>(graph: T) => T;
963
+ export function removeNodes<T extends WritableGraph>(
964
+ graphOrIds: T | string[],
965
+ idsOrEdges?: string[] | boolean,
966
+ edges?: boolean,
967
+ ): T | (<T extends WritableGraph>(graph: T) => T) {
968
+ if (Array.isArray(graphOrIds)) {
969
+ // Curried: removeNodes(ids, edges?)
970
+ const ids = graphOrIds;
971
+ const edgesArg = typeof idsOrEdges === 'boolean' ? idsOrEdges : false;
972
+ return <T extends WritableGraph>(graph: T) => removeNodesImpl(graph, ids, edgesArg);
973
+ } else {
974
+ // Direct: removeNodes(graph, ids, edges?)
975
+ const graph = graphOrIds;
976
+ const ids = idsOrEdges as string[];
977
+ const edgesArg = edges ?? false;
978
+ return removeNodesImpl(graph, ids, edgesArg);
979
+ }
980
+ }
981
+
982
+ /**
983
+ * Implementation helper for removeNode.
984
+ */
985
+ const removeNodeImpl = <T extends WritableGraph>(graph: T, id: string, edges = false): T => {
986
+ const internal = getInternal(graph);
987
+ const nodeAtom = internal._node(id);
988
+ // TODO(wittjosiah): Is there a way to mark these atom values for garbage collection?
989
+ internal._registry.set(nodeAtom, Option.none());
990
+ graph.onNodeChanged.emit({ id, node: Option.none() });
991
+ // TODO(wittjosiah): Reset expanded and initialized flags?
992
+
993
+ if (edges) {
994
+ const nodeEdges = internal._registry.get(internal._edges(id));
995
+ const edgesToRemove: Edge[] = [];
996
+ for (const [relationKeyValue, relatedIds] of Object.entries(nodeEdges)) {
997
+ const relation = relationFromKey(relationKeyValue);
998
+ const isInboundRelation = relation.direction === 'inbound';
999
+ for (const relatedId of relatedIds) {
1000
+ if (isInboundRelation) {
1001
+ // Inbound edge lists store source node IDs; reconstruct the canonical outbound edge.
1002
+ edgesToRemove.push({ source: relatedId, target: id, relation: inverseRelation(relation) });
1003
+ } else {
1004
+ edgesToRemove.push({ source: id, target: relatedId, relation });
1005
+ }
562
1006
  }
563
1007
  }
1008
+ removeEdgesImpl(graph, edgesToRemove);
564
1009
  }
565
1010
 
566
- sortEdges(id: string, relation: Relation, order: string[]): void {
567
- const edgesAtom = this._edges(id);
568
- const edges = this._registry.get(edgesAtom);
569
- const unsorted = edges[relation].filter((id) => !order.includes(id)) ?? [];
570
- const sorted = order.filter((id) => edges[relation].includes(id)) ?? [];
571
- edges[relation].splice(0, edges[relation].length, ...[...sorted, ...unsorted]);
572
- this._registry.set(edgesAtom, edges);
1011
+ internal._onRemoveNode?.(id);
1012
+ return graph;
1013
+ };
1014
+
1015
+ /**
1016
+ * Remove a node from the graph.
1017
+ */
1018
+ export function removeNode<T extends WritableGraph>(graph: T, id: string, edges?: boolean): T;
1019
+ export function removeNode(id: string, edges?: boolean): <T extends WritableGraph>(graph: T) => T;
1020
+ export function removeNode<T extends WritableGraph>(
1021
+ graphOrId: T | string,
1022
+ idOrEdges?: string | boolean,
1023
+ edges?: boolean,
1024
+ ): T | (<T extends WritableGraph>(graph: T) => T) {
1025
+ if (typeof graphOrId === 'string') {
1026
+ // Curried: removeNode(id, edges?)
1027
+ const id = graphOrId;
1028
+ const edgesArg = typeof idOrEdges === 'boolean' ? idOrEdges : false;
1029
+ return <T extends WritableGraph>(graph: T) => removeNodeImpl(graph, id, edgesArg);
1030
+ } else {
1031
+ // Direct: removeNode(graph, id, edges?)
1032
+ const graph = graphOrId;
1033
+ const id = idOrEdges as string;
1034
+ const edgesArg = edges ?? false;
1035
+ return removeNodeImpl(graph, id, edgesArg);
573
1036
  }
1037
+ }
574
1038
 
575
- traverse({ visitor, source = ROOT_ID, relation = 'outbound' }: GraphTraversalOptions, path: string[] = []): void {
576
- // Break cycles.
577
- if (path.includes(source)) {
578
- return;
579
- }
1039
+ /**
1040
+ * Implementation helper for addEdges.
1041
+ */
1042
+ const addEdgesImpl = <T extends WritableGraph>(graph: T, edges: Edge[]): T => {
1043
+ Atom.batch(() => {
1044
+ edges.map((edge) => addEdgeImpl(graph, edge));
1045
+ });
1046
+ return graph;
1047
+ };
580
1048
 
581
- const node = this.getNodeOrThrow(source);
582
- const shouldContinue = visitor(node, [...path, source]);
583
- if (shouldContinue === false) {
584
- return;
585
- }
1049
+ /**
1050
+ * Add edges to the graph.
1051
+ */
1052
+ export function addEdges<T extends WritableGraph>(graph: T, edges: Edge[]): T;
1053
+ export function addEdges(edges: Edge[]): <T extends WritableGraph>(graph: T) => T;
1054
+ export function addEdges<T extends WritableGraph>(
1055
+ graphOrEdges: T | Edge[],
1056
+ edges?: Edge[],
1057
+ ): T | (<T extends WritableGraph>(graph: T) => T) {
1058
+ if (edges === undefined) {
1059
+ // Curried: addEdges(edges)
1060
+ const edges = graphOrEdges as Edge[];
1061
+ return <T extends WritableGraph>(graph: T) => addEdgesImpl(graph, edges);
1062
+ } else {
1063
+ // Direct: addEdges(graph, edges)
1064
+ const graph = graphOrEdges as T;
1065
+ return addEdgesImpl(graph, edges);
1066
+ }
1067
+ }
586
1068
 
587
- Object.values(this.getConnections(source, relation)).forEach((child) =>
588
- this.traverse({ source: child.id, relation, visitor }, [...path, source]),
589
- );
590
- }
591
-
592
- getPath({ source = 'root', target }: { source?: string; target: string }): Option.Option<string[]> {
593
- return Function.pipe(
594
- this.getNode(source),
595
- Option.flatMap((node) => {
596
- let found: Option.Option<string[]> = Option.none();
597
- this.traverse({
598
- source: node.id,
599
- visitor: (node, path) => {
600
- if (Option.isSome(found)) {
601
- return false;
602
- }
603
-
604
- if (node.id === target) {
605
- found = Option.some(path);
606
- }
607
- },
608
- });
1069
+ /**
1070
+ * Implementation helper for addEdge.
1071
+ */
1072
+ const addEdgeImpl = <T extends WritableGraph>(graph: T, edgeArg: Edge): T => {
1073
+ const relation = normalizeRelation(edgeArg.relation);
1074
+ const relationId = relationKey(relation);
1075
+ const inverse = inverseRelation(relation);
1076
+ const inverseId = relationKey(inverse);
1077
+ const internal = getInternal(graph);
1078
+
1079
+ const sourceAtom = internal._edges(edgeArg.source);
1080
+ const source = internal._registry.get(sourceAtom);
1081
+ const sourceList = source[relationId] ?? [];
1082
+ if (!sourceList.includes(edgeArg.target)) {
1083
+ log('add edge', { source: edgeArg.source, target: edgeArg.target, relation: relationId });
1084
+ internal._registry.set(sourceAtom, { ...source, [relationId]: [...sourceList, edgeArg.target] });
1085
+ }
609
1086
 
610
- return found;
611
- }),
612
- );
1087
+ const targetAtom = internal._edges(edgeArg.target);
1088
+ const target = internal._registry.get(targetAtom);
1089
+ const targetList = target[inverseId] ?? [];
1090
+ if (!targetList.includes(edgeArg.source)) {
1091
+ log('add inverse edge', { source: edgeArg.source, target: edgeArg.target, relation: inverseId });
1092
+ internal._registry.set(targetAtom, { ...target, [inverseId]: [...targetList, edgeArg.source] });
613
1093
  }
614
1094
 
615
- async waitForPath(
616
- params: { source?: string; target: string },
617
- { timeout = 5_000, interval = 500 }: { timeout?: number; interval?: number } = {},
618
- ): Promise<string[]> {
619
- const path = this.getPath(params);
620
- if (Option.isSome(path)) {
621
- return path.value;
622
- }
1095
+ return graph;
1096
+ };
623
1097
 
624
- const trigger = new Trigger<string[]>();
625
- const i = setInterval(() => {
626
- const path = this.getPath(params);
627
- if (Option.isSome(path)) {
628
- trigger.wake(path.value);
629
- }
630
- }, interval);
1098
+ /**
1099
+ * Add an edge to the graph.
1100
+ */
1101
+ export function addEdge<T extends WritableGraph>(graph: T, edgeArg: Edge): T;
1102
+ export function addEdge(edgeArg: Edge): <T extends WritableGraph>(graph: T) => T;
1103
+ export function addEdge<T extends WritableGraph>(
1104
+ graphOrEdgeArg: T | Edge,
1105
+ edgeArg?: Edge,
1106
+ ): T | (<T extends WritableGraph>(graph: T) => T) {
1107
+ if (edgeArg === undefined) {
1108
+ // Curried: addEdge(edgeArg)
1109
+ const edgeArg = graphOrEdgeArg as Edge;
1110
+ return <T extends WritableGraph>(graph: T) => addEdgeImpl(graph, edgeArg);
1111
+ } else {
1112
+ // Direct: addEdge(graph, edgeArg)
1113
+ const graph = graphOrEdgeArg as T;
1114
+ return addEdgeImpl(graph, edgeArg);
1115
+ }
1116
+ }
631
1117
 
632
- return trigger.wait({ timeout }).finally(() => clearInterval(i));
1118
+ /**
1119
+ * Implementation helper for removeEdges.
1120
+ */
1121
+ const removeEdgesImpl = <T extends WritableGraph>(graph: T, edges: Edge[], removeOrphans = false): T => {
1122
+ Atom.batch(() => {
1123
+ edges.map((edge) => removeEdgeImpl(graph, edge, removeOrphans));
1124
+ });
1125
+ return graph;
1126
+ };
1127
+
1128
+ /**
1129
+ * Remove edges from the graph.
1130
+ */
1131
+ export function removeEdges<T extends WritableGraph>(graph: T, edges: Edge[], removeOrphans?: boolean): T;
1132
+ export function removeEdges(edges: Edge[], removeOrphans?: boolean): <T extends WritableGraph>(graph: T) => T;
1133
+ export function removeEdges<T extends WritableGraph>(
1134
+ graphOrEdges: T | Edge[],
1135
+ edgesOrRemoveOrphans?: Edge[] | boolean,
1136
+ removeOrphans?: boolean,
1137
+ ): T | (<T extends WritableGraph>(graph: T) => T) {
1138
+ if (Array.isArray(graphOrEdges)) {
1139
+ // Curried: removeEdges(edges, removeOrphans?)
1140
+ const edges = graphOrEdges;
1141
+ const removeOrphansArg = typeof edgesOrRemoveOrphans === 'boolean' ? edgesOrRemoveOrphans : false;
1142
+ return <T extends WritableGraph>(graph: T) => removeEdgesImpl(graph, edges, removeOrphansArg);
1143
+ } else {
1144
+ // Direct: removeEdges(graph, edges, removeOrphans?)
1145
+ const graph = graphOrEdges;
1146
+ const edges = edgesOrRemoveOrphans as Edge[];
1147
+ const removeOrphansArg = removeOrphans ?? false;
1148
+ return removeEdgesImpl(graph, edges, removeOrphansArg);
633
1149
  }
1150
+ }
634
1151
 
635
- /** @internal */
636
- _constructNode(node: NodeArg<any>): Option.Option<Node> {
637
- return Option.some({
638
- [graphSymbol]: this,
639
- data: null,
640
- properties: {},
641
- ...node,
642
- });
1152
+ /**
1153
+ * Implementation helper for removeEdge.
1154
+ */
1155
+ const removeEdgeImpl = <T extends WritableGraph>(graph: T, edgeArg: Edge, removeOrphans = false): T => {
1156
+ const relation = normalizeRelation(edgeArg.relation);
1157
+ const relationId = relationKey(relation);
1158
+ const inverse = inverseRelation(relation);
1159
+ const inverseId = relationKey(inverse);
1160
+ const internal = getInternal(graph);
1161
+
1162
+ const sourceAtom = internal._edges(edgeArg.source);
1163
+ const source = internal._registry.get(sourceAtom);
1164
+ const sourceList = source[relationId] ?? [];
1165
+ if (sourceList.includes(edgeArg.target)) {
1166
+ internal._registry.set(sourceAtom, { ...source, [relationId]: sourceList.filter((id) => id !== edgeArg.target) });
1167
+ }
1168
+
1169
+ const targetAtom = internal._edges(edgeArg.target);
1170
+ const target = internal._registry.get(targetAtom);
1171
+ const targetList = target[inverseId] ?? [];
1172
+ if (targetList.includes(edgeArg.source)) {
1173
+ internal._registry.set(targetAtom, { ...target, [inverseId]: targetList.filter((id) => id !== edgeArg.source) });
1174
+ }
1175
+
1176
+ if (removeOrphans) {
1177
+ const sourceAfter = internal._registry.get(sourceAtom);
1178
+ const targetAfter = internal._registry.get(targetAtom);
1179
+ const isEmpty = (edges: Edges) => Object.values(edges).every((ids) => ids.length === 0);
1180
+ if (isEmpty(sourceAfter) && edgeArg.source !== Node.RootId) {
1181
+ removeNodesImpl(graph, [edgeArg.source]);
1182
+ }
1183
+ if (isEmpty(targetAfter) && edgeArg.target !== Node.RootId) {
1184
+ removeNodesImpl(graph, [edgeArg.target]);
1185
+ }
1186
+ }
1187
+ return graph;
1188
+ };
1189
+
1190
+ /**
1191
+ * Remove an edge from the graph.
1192
+ */
1193
+ export function removeEdge<T extends WritableGraph>(graph: T, edgeArg: Edge, removeOrphans?: boolean): T;
1194
+ export function removeEdge(edgeArg: Edge, removeOrphans?: boolean): <T extends WritableGraph>(graph: T) => T;
1195
+ export function removeEdge<T extends WritableGraph>(
1196
+ graphOrEdgeArg: T | Edge,
1197
+ edgeArgOrRemoveOrphans?: Edge | boolean,
1198
+ removeOrphans?: boolean,
1199
+ ): T | (<T extends WritableGraph>(graph: T) => T) {
1200
+ if (
1201
+ edgeArgOrRemoveOrphans === undefined ||
1202
+ typeof edgeArgOrRemoveOrphans === 'boolean' ||
1203
+ 'source' in graphOrEdgeArg
1204
+ ) {
1205
+ // Curried: removeEdge(edgeArg, removeOrphans?)
1206
+ const edgeArg = graphOrEdgeArg as Edge;
1207
+ const removeOrphansArg = typeof edgeArgOrRemoveOrphans === 'boolean' ? edgeArgOrRemoveOrphans : false;
1208
+ return <T extends WritableGraph>(graph: T) => removeEdgeImpl(graph, edgeArg, removeOrphansArg);
1209
+ } else {
1210
+ // Direct: removeEdge(graph, edgeArg, removeOrphans?)
1211
+ const graph = graphOrEdgeArg as T;
1212
+ const edgeArg = edgeArgOrRemoveOrphans as Edge;
1213
+ const removeOrphansArg = removeOrphans ?? false;
1214
+ return removeEdgeImpl(graph, edgeArg, removeOrphansArg);
643
1215
  }
644
1216
  }
1217
+
1218
+ /**
1219
+ * Creates a new Graph instance.
1220
+ */
1221
+ export const make = (params?: GraphProps): Graph => {
1222
+ return new GraphImpl(params);
1223
+ };
1224
+
1225
+ //
1226
+ // Utilities
1227
+ //
1228
+
1229
+ export const relationKey = (relation: Node.RelationInput): string => {
1230
+ const normalized = normalizeRelation(relation);
1231
+ return secondaryKey(normalized.kind, normalized.direction);
1232
+ };
1233
+
1234
+ export const relationFromKey = (encoded: string): Node.Relation => {
1235
+ const parts = secondaryParts(encoded);
1236
+ invariant(parts.length === 2 && parts[0].length > 0 && parts[1].length > 0, `Invalid relation key: ${encoded}`);
1237
+ const [kind, directionRaw] = parts;
1238
+ invariant(directionRaw === 'outbound' || directionRaw === 'inbound', `Invalid relation direction: ${directionRaw}`);
1239
+ return Node.relation(kind, directionRaw);
1240
+ };
1241
+
1242
+ const connectionKey = (id: string, relation: Node.RelationInput): string => primaryKey(id, relationKey(relation));
1243
+
1244
+ const relationFromConnectionKey = (key: string): { id: string; relation: Node.Relation } => {
1245
+ const [id, encodedRelation] = primaryParts(key);
1246
+ invariant(id && encodedRelation, `Invalid connection key: ${key}`);
1247
+ return { id, relation: relationFromKey(encodedRelation) };
1248
+ };
1249
+
1250
+ const inverseRelation = (relation: Node.RelationInput): Node.Relation => {
1251
+ const normalized = normalizeRelation(relation);
1252
+ return Node.relation(normalized.kind, normalized.direction === 'outbound' ? 'inbound' : 'outbound');
1253
+ };