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