@dxos/app-graph 0.8.4-main.406dc2a → 0.8.4-main.40e3dcdf1b
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/index.mjs +1416 -695
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +1415 -695
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- 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 +112 -66
- package/dist/types/src/graph-builder.d.ts.map +1 -1
- package/dist/types/src/graph.d.ts +187 -221
- package/dist/types/src/graph.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +6 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/node-matcher.d.ts +218 -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 +44 -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/util.d.ts +34 -0
- package/dist/types/src/util.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +36 -34
- package/src/atoms.ts +25 -0
- package/src/graph-builder.test.ts +895 -137
- package/src/graph-builder.ts +703 -289
- package/src/graph.test.ts +450 -122
- package/src/graph.ts +1052 -401
- package/src/index.ts +9 -3
- package/src/node-matcher.test.ts +301 -0
- package/src/node-matcher.ts +282 -0
- package/src/node.ts +56 -8
- package/src/stories/EchoGraph.stories.tsx +159 -120
- package/src/stories/Tree.tsx +1 -1
- package/src/util.ts +71 -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 -218
- package/src/testing.ts +0 -20
package/src/graph.ts
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
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,33 +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 { Separators, normalizeRelation, shallowEqual } from './util';
|
|
17
19
|
|
|
18
20
|
const graphSymbol = Symbol('graph');
|
|
19
|
-
|
|
20
|
-
type
|
|
21
|
+
|
|
22
|
+
type DeepWriteable<T> = {
|
|
23
|
+
-readonly [K in keyof T]: T[K] extends object ? DeepWriteable<T[K]> : T[K];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type NodeInternal = DeepWriteable<Node.Node> & { [graphSymbol]: GraphImpl };
|
|
21
27
|
|
|
22
28
|
/**
|
|
23
29
|
* Get the Graph a Node is currently associated with.
|
|
24
30
|
*/
|
|
25
|
-
export const getGraph = (node: Node): Graph => {
|
|
31
|
+
export const getGraph = (node: Node.Node): Graph => {
|
|
26
32
|
const graph = (node as NodeInternal)[graphSymbol];
|
|
27
33
|
invariant(graph, 'Node is not associated with a graph.');
|
|
28
|
-
return graph;
|
|
34
|
+
return graph as Graph;
|
|
29
35
|
};
|
|
30
36
|
|
|
31
|
-
export const ROOT_ID = 'root';
|
|
32
|
-
export const ROOT_TYPE = 'dxos.org/type/GraphRoot';
|
|
33
|
-
export const ACTION_TYPE = 'dxos.org/type/GraphAction';
|
|
34
|
-
export const ACTION_GROUP_TYPE = 'dxos.org/type/GraphActionGroup';
|
|
35
|
-
|
|
36
37
|
export type GraphTraversalOptions = {
|
|
37
38
|
/**
|
|
38
39
|
* A callback which is called for each node visited during traversal.
|
|
39
40
|
*
|
|
40
41
|
* If the callback returns `false`, traversal is stops recursing.
|
|
41
42
|
*/
|
|
42
|
-
visitor: (node: Node, path: string[]) => boolean | void;
|
|
43
|
+
visitor: (node: Node.Node, path: string[]) => boolean | void;
|
|
43
44
|
|
|
44
45
|
/**
|
|
45
46
|
* The node to start traversing from.
|
|
@@ -48,244 +49,165 @@ export type GraphTraversalOptions = {
|
|
|
48
49
|
*/
|
|
49
50
|
source?: string;
|
|
50
51
|
|
|
51
|
-
/**
|
|
52
|
-
|
|
53
|
-
*
|
|
54
|
-
* @default 'outbound'
|
|
55
|
-
*/
|
|
56
|
-
relation?: Relation;
|
|
52
|
+
/** The relation(s) to traverse graph edges. */
|
|
53
|
+
relation: Node.RelationInput | Node.RelationInput[];
|
|
57
54
|
};
|
|
58
55
|
|
|
59
|
-
export type
|
|
56
|
+
export type GraphProps = {
|
|
60
57
|
registry?: Registry.Registry;
|
|
61
|
-
nodes?: MakeOptional<Node, 'data' | 'cacheable'>[];
|
|
58
|
+
nodes?: MakeOptional<Node.Node, 'data' | 'cacheable'>[];
|
|
62
59
|
edges?: Record<string, Edges>;
|
|
63
|
-
onExpand?:
|
|
64
|
-
onInitialize?:
|
|
65
|
-
onRemoveNode?:
|
|
60
|
+
onExpand?: (id: string, relation: Node.Relation) => void;
|
|
61
|
+
onInitialize?: (id: string) => Promise<void>;
|
|
62
|
+
onRemoveNode?: (id: string) => void;
|
|
66
63
|
};
|
|
67
64
|
|
|
68
|
-
export type Edge = { source: string; target: string };
|
|
69
|
-
export type Edges =
|
|
70
|
-
|
|
71
|
-
export interface ReadableGraph {
|
|
72
|
-
/**
|
|
73
|
-
* Event emitted when a node is changed.
|
|
74
|
-
*/
|
|
75
|
-
onNodeChanged: Event<{ id: string; node: Option.Option<Node> }>;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Convert the graph to a JSON object.
|
|
79
|
-
*/
|
|
80
|
-
toJSON(id?: string): object;
|
|
81
|
-
|
|
82
|
-
json(id?: string): Rx.Rx<any>;
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Get the rx key for the node with the given id.
|
|
86
|
-
*/
|
|
87
|
-
node(id: string): Rx.Rx<Option.Option<Node>>;
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Get the rx key for the node with the given id.
|
|
91
|
-
*/
|
|
92
|
-
nodeOrThrow(id: string): Rx.Rx<Node>;
|
|
65
|
+
export type Edge = { source: string; target: string; relation: Node.RelationInput };
|
|
66
|
+
export type Edges = Record<string, string[]>;
|
|
93
67
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Get the rx key for the actions of the node with the given id.
|
|
101
|
-
*/
|
|
102
|
-
actions(id: string): Rx.Rx<(Action | ActionGroup)[]>;
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Get the rx key for the edges of the node with the given id.
|
|
106
|
-
*/
|
|
107
|
-
edges(id: string): Rx.Rx<Edges>;
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Alias for `getNodeOrThrow(ROOT_ID)`.
|
|
111
|
-
*/
|
|
112
|
-
get root(): Node;
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Get the node with the given id from the graph's registry.
|
|
116
|
-
*/
|
|
117
|
-
getNode(id: string): Option.Option<Node>;
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Get the node with the given id from the graph's registry.
|
|
121
|
-
*
|
|
122
|
-
* @throws If the node is Option.none().
|
|
123
|
-
*/
|
|
124
|
-
getNodeOrThrow(id: string): Node;
|
|
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;
|
|
125
73
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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;
|
|
130
79
|
|
|
131
|
-
|
|
132
|
-
* Get all actions connected to the node with the given id from the graph's registry.
|
|
133
|
-
*/
|
|
134
|
-
getActions(id: string): Node[];
|
|
80
|
+
export type GraphKindType = 'readable' | 'expandable' | 'writable';
|
|
135
81
|
|
|
82
|
+
export interface BaseGraph extends Pipeable.Pipeable {
|
|
83
|
+
readonly [GraphTypeId]: GraphTypeId;
|
|
84
|
+
readonly [GraphKind]: GraphKindType;
|
|
136
85
|
/**
|
|
137
|
-
*
|
|
86
|
+
* Event emitted when a node is changed.
|
|
138
87
|
*/
|
|
139
|
-
|
|
140
|
-
|
|
88
|
+
readonly onNodeChanged: Event<{ id: string; node: Option.Option<Node.Node> }>;
|
|
141
89
|
/**
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
* @param options.node The node to start traversing from.
|
|
145
|
-
* @param options.relation The relation to traverse graph edges.
|
|
146
|
-
* @param options.visitor A callback which is called for each node visited during traversal.
|
|
90
|
+
* Get the atom key for the JSON representation of the graph.
|
|
147
91
|
*/
|
|
148
|
-
|
|
149
|
-
|
|
92
|
+
json(id?: string): Atom.Atom<any>;
|
|
150
93
|
/**
|
|
151
|
-
* Get the
|
|
94
|
+
* Get the atom key for the node with the given id.
|
|
152
95
|
*/
|
|
153
|
-
|
|
154
|
-
|
|
96
|
+
node(id: string): Atom.Atom<Option.Option<Node.Node>>;
|
|
155
97
|
/**
|
|
156
|
-
*
|
|
98
|
+
* Get the atom key for the node with the given id.
|
|
157
99
|
*/
|
|
158
|
-
|
|
159
|
-
params: { source?: string; target: string },
|
|
160
|
-
options?: { timeout?: number; interval?: number },
|
|
161
|
-
): Promise<string[]>;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export interface ExpandableGraph extends ReadableGraph {
|
|
100
|
+
nodeOrThrow(id: string): Atom.Atom<Node.Node>;
|
|
165
101
|
/**
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
* Fires the `onInitialize` callback to provide initial data for a node.
|
|
102
|
+
* Get the atom key for the connections of the node with the given id.
|
|
169
103
|
*/
|
|
170
|
-
|
|
171
|
-
|
|
104
|
+
connections(id: string, relation: Node.RelationInput): Atom.Atom<Node.Node[]>;
|
|
172
105
|
/**
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
* Fires the `onExpand` callback to add connections to the node.
|
|
106
|
+
* Get the atom key for the actions of the node with the given id.
|
|
176
107
|
*/
|
|
177
|
-
|
|
178
|
-
|
|
108
|
+
actions(id: string): Atom.Atom<(Node.Action | Node.ActionGroup)[]>;
|
|
179
109
|
/**
|
|
180
|
-
*
|
|
110
|
+
* Get the atom key for the edges of the node with the given id.
|
|
181
111
|
*/
|
|
182
|
-
|
|
112
|
+
edges(id: string): Atom.Atom<Edges>;
|
|
183
113
|
}
|
|
184
114
|
|
|
185
|
-
export
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
*/
|
|
189
|
-
addNodes(nodes: NodeArg<any, Record<string, any>>[]): 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' };
|
|
190
118
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Remove nodes from the graph.
|
|
198
|
-
*/
|
|
199
|
-
removeNodes(ids: string[], edges?: boolean): void;
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Remove a node from the graph.
|
|
203
|
-
*/
|
|
204
|
-
removeNode(id: string, edges?: boolean): void;
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Add edges to the graph.
|
|
208
|
-
*/
|
|
209
|
-
addEdges(edges: Edge[]): void;
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Add an edge to the graph.
|
|
213
|
-
*/
|
|
214
|
-
addEdge(edge: Edge): void;
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Remove edges from the graph.
|
|
218
|
-
*/
|
|
219
|
-
removeEdges(edges: Edge[], removeOrphans?: boolean): void;
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Remove an edge from the graph.
|
|
223
|
-
*/
|
|
224
|
-
removeEdge(edge: Edge, removeOrphans?: boolean): void;
|
|
225
|
-
}
|
|
119
|
+
/**
|
|
120
|
+
* Graph interface.
|
|
121
|
+
*/
|
|
122
|
+
export type Graph = WritableGraph;
|
|
226
123
|
|
|
227
124
|
/**
|
|
228
125
|
* The Graph represents the user interface information architecture of the application constructed via plugins.
|
|
126
|
+
* @internal
|
|
229
127
|
*/
|
|
230
|
-
|
|
231
|
-
readonly
|
|
128
|
+
class GraphImpl implements WritableGraph {
|
|
129
|
+
readonly [GraphTypeId]: GraphTypeId = GraphTypeId;
|
|
130
|
+
readonly [GraphKind] = 'writable' as const;
|
|
232
131
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
132
|
+
pipe() {
|
|
133
|
+
// eslint-disable-next-line prefer-rest-params
|
|
134
|
+
return Pipeable.pipeArguments(this, arguments);
|
|
135
|
+
}
|
|
236
136
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
137
|
+
readonly onNodeChanged = new Event<{
|
|
138
|
+
id: string;
|
|
139
|
+
node: Option.Option<Node.Node>;
|
|
140
|
+
}>();
|
|
141
|
+
|
|
142
|
+
readonly _onExpand?: GraphProps['onExpand'];
|
|
143
|
+
readonly _onInitialize?: GraphProps['onInitialize'];
|
|
144
|
+
readonly _onRemoveNode?: GraphProps['onRemoveNode'];
|
|
145
|
+
|
|
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([
|
|
152
|
+
[
|
|
153
|
+
Node.RootId,
|
|
154
|
+
this._constructNode({
|
|
155
|
+
id: Node.RootId,
|
|
156
|
+
type: Node.RootType,
|
|
157
|
+
data: null,
|
|
158
|
+
properties: {},
|
|
159
|
+
}),
|
|
160
|
+
],
|
|
243
161
|
]);
|
|
244
162
|
|
|
245
163
|
/** @internal */
|
|
246
|
-
readonly _node =
|
|
164
|
+
readonly _node = Atom.family<string, Atom.Writable<Option.Option<Node.Node>>>((id) => {
|
|
247
165
|
const initial = Option.flatten(Record.get(this._initialNodes, id));
|
|
248
|
-
return
|
|
166
|
+
return Atom.make<Option.Option<Node.Node>>(initial).pipe(Atom.keepAlive, Atom.withLabel(`graph:node:${id}`));
|
|
249
167
|
});
|
|
250
168
|
|
|
251
|
-
|
|
252
|
-
return
|
|
169
|
+
readonly _nodeOrThrow = Atom.family<string, Atom.Atom<Node.Node>>((id) => {
|
|
170
|
+
return Atom.make((get) => {
|
|
253
171
|
const node = get(this._node(id));
|
|
254
172
|
invariant(Option.isSome(node), `Node not available: ${id}`);
|
|
255
173
|
return node.value;
|
|
256
174
|
});
|
|
257
175
|
});
|
|
258
176
|
|
|
259
|
-
|
|
260
|
-
const initial = Record.get(this._initialEdges, id).pipe(Option.getOrElse(() => ({
|
|
261
|
-
return
|
|
177
|
+
readonly _edges = Atom.family<string, Atom.Writable<Edges>>((id) => {
|
|
178
|
+
const initial = Record.get(this._initialEdges, id).pipe(Option.getOrElse(() => ({}) as Edges));
|
|
179
|
+
return Atom.make<Edges>(initial).pipe(Atom.keepAlive, Atom.withLabel(`graph:edges:${id}`));
|
|
262
180
|
});
|
|
263
181
|
|
|
264
|
-
// NOTE: Currently the argument to the family needs to be referentially stable for the
|
|
265
|
-
// TODO(wittjosiah):
|
|
266
|
-
|
|
267
|
-
return
|
|
268
|
-
|
|
182
|
+
// NOTE: Currently the argument to the family needs to be referentially stable for the atom to be referentially stable.
|
|
183
|
+
// TODO(wittjosiah): Atom feature request, support for something akin to `ComplexMap` to allow for complex arguments.
|
|
184
|
+
readonly _connections = Atom.family<string, Atom.Atom<Node.Node[]>>((key) => {
|
|
185
|
+
return Atom.make((get) => {
|
|
186
|
+
if (!key || key.indexOf(Separators.primary) <= 0) {
|
|
187
|
+
return [];
|
|
188
|
+
}
|
|
189
|
+
const { id, relation } = relationFromConnectionKey(key);
|
|
269
190
|
const edges = get(this._edges(id));
|
|
270
|
-
return edges[relation
|
|
191
|
+
return (edges[relationKey(relation)] ?? [])
|
|
271
192
|
.map((id) => get(this._node(id)))
|
|
272
193
|
.filter(Option.isSome)
|
|
273
194
|
.map((o) => o.value);
|
|
274
|
-
}).pipe(
|
|
195
|
+
}).pipe(Atom.withLabel(`graph:connections:${key}`));
|
|
275
196
|
});
|
|
276
197
|
|
|
277
|
-
|
|
278
|
-
return
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
198
|
+
readonly _actions = Atom.family<string, Atom.Atom<(Node.Action | Node.ActionGroup)[]>>((id) => {
|
|
199
|
+
return Atom.make((get) => {
|
|
200
|
+
if (!id) {
|
|
201
|
+
return [];
|
|
202
|
+
}
|
|
203
|
+
return get(this._connections(connectionKey(id, Node.actionRelation()))) as (Node.Action | Node.ActionGroup)[];
|
|
204
|
+
}).pipe(Atom.withLabel(`graph:actions:${id}`));
|
|
283
205
|
});
|
|
284
206
|
|
|
285
|
-
|
|
286
|
-
return
|
|
287
|
-
const toJSON = (node: Node, seen: string[] = []): any => {
|
|
288
|
-
const nodes = get(this.
|
|
207
|
+
readonly _json = Atom.family<string, Atom.Atom<any>>((id) => {
|
|
208
|
+
return Atom.make((get) => {
|
|
209
|
+
const toJSON = (node: Node.Node, seen: string[] = []): any => {
|
|
210
|
+
const nodes = get(this._connections(connectionKey(node.id, 'child')));
|
|
289
211
|
const obj: Record<string, any> = {
|
|
290
212
|
id: node.id,
|
|
291
213
|
type: node.type,
|
|
@@ -295,7 +217,7 @@ export class Graph implements WritableGraph {
|
|
|
295
217
|
}
|
|
296
218
|
if (nodes.length) {
|
|
297
219
|
obj.nodes = nodes
|
|
298
|
-
.map((n) => {
|
|
220
|
+
.map((n: Node.Node) => {
|
|
299
221
|
// Break cycles.
|
|
300
222
|
const nextSeen = [...seen, node.id];
|
|
301
223
|
return nextSeen.includes(n.id) ? undefined : toJSON(n, nextSeen);
|
|
@@ -305,12 +227,12 @@ export class Graph implements WritableGraph {
|
|
|
305
227
|
return obj;
|
|
306
228
|
};
|
|
307
229
|
|
|
308
|
-
const root = get(this.
|
|
230
|
+
const root = get(this._nodeOrThrow(id));
|
|
309
231
|
return toJSON(root);
|
|
310
|
-
}).pipe(
|
|
232
|
+
}).pipe(Atom.withLabel(`graph:json:${id}`));
|
|
311
233
|
});
|
|
312
234
|
|
|
313
|
-
constructor({ registry, nodes, edges, onInitialize, onExpand, onRemoveNode }:
|
|
235
|
+
constructor({ registry, nodes, edges, onInitialize, onExpand, onRemoveNode }: GraphProps = {}) {
|
|
314
236
|
this._registry = registry ?? Registry.make();
|
|
315
237
|
this._onInitialize = onInitialize;
|
|
316
238
|
this._onExpand = onExpand;
|
|
@@ -329,276 +251,1005 @@ export class Graph implements WritableGraph {
|
|
|
329
251
|
}
|
|
330
252
|
}
|
|
331
253
|
|
|
332
|
-
|
|
333
|
-
return
|
|
254
|
+
json(id = Node.RootId): Atom.Atom<any> {
|
|
255
|
+
return jsonImpl(this, id);
|
|
334
256
|
}
|
|
335
257
|
|
|
336
|
-
|
|
337
|
-
return this
|
|
258
|
+
node(id: string): Atom.Atom<Option.Option<Node.Node>> {
|
|
259
|
+
return nodeImpl(this, id);
|
|
338
260
|
}
|
|
339
261
|
|
|
340
|
-
|
|
341
|
-
return this
|
|
262
|
+
nodeOrThrow(id: string): Atom.Atom<Node.Node> {
|
|
263
|
+
return nodeOrThrowImpl(this, id);
|
|
342
264
|
}
|
|
343
265
|
|
|
344
|
-
|
|
345
|
-
return this
|
|
266
|
+
connections(id: string, relation: Node.RelationInput): Atom.Atom<Node.Node[]> {
|
|
267
|
+
return connectionsImpl(this, id, relation);
|
|
346
268
|
}
|
|
347
269
|
|
|
348
|
-
|
|
349
|
-
return this
|
|
270
|
+
actions(id: string): Atom.Atom<(Node.Action | Node.ActionGroup)[]> {
|
|
271
|
+
return actionsImpl(this, id);
|
|
350
272
|
}
|
|
351
273
|
|
|
352
|
-
|
|
353
|
-
return this
|
|
274
|
+
edges(id: string): Atom.Atom<Edges> {
|
|
275
|
+
return edgesImpl(this, id);
|
|
354
276
|
}
|
|
355
277
|
|
|
356
|
-
|
|
357
|
-
|
|
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
|
+
});
|
|
358
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
|
+
};
|
|
359
312
|
|
|
360
|
-
|
|
361
|
-
|
|
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
|
+
};
|
|
360
|
+
|
|
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!);
|
|
362
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
|
+
};
|
|
363
388
|
|
|
364
|
-
|
|
365
|
-
|
|
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!);
|
|
366
408
|
}
|
|
409
|
+
}
|
|
410
|
+
|
|
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
|
+
};
|
|
367
426
|
|
|
368
|
-
|
|
369
|
-
|
|
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);
|
|
370
449
|
}
|
|
450
|
+
}
|
|
371
451
|
|
|
372
|
-
|
|
373
|
-
|
|
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!);
|
|
374
477
|
}
|
|
478
|
+
}
|
|
375
479
|
|
|
376
|
-
|
|
377
|
-
|
|
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!);
|
|
378
502
|
}
|
|
503
|
+
}
|
|
379
504
|
|
|
380
|
-
|
|
381
|
-
|
|
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;
|
|
382
516
|
}
|
|
383
517
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
await this._onInitialize?.(id);
|
|
389
|
-
Record.set(this._initialized, id, true);
|
|
390
|
-
}
|
|
518
|
+
const node = getNodeOrThrow(graph, source);
|
|
519
|
+
const shouldContinue = visitor(node, [...path, source]);
|
|
520
|
+
if (shouldContinue === false) {
|
|
521
|
+
return;
|
|
391
522
|
}
|
|
392
523
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
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
|
+
}
|
|
400
532
|
}
|
|
401
533
|
}
|
|
534
|
+
};
|
|
402
535
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
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);
|
|
407
557
|
}
|
|
558
|
+
}
|
|
408
559
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
this.onNodeChanged.emit({ id, node: newNode });
|
|
431
|
-
},
|
|
432
|
-
});
|
|
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
|
+
});
|
|
433
581
|
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
this.addEdges(_edges);
|
|
439
|
-
// });
|
|
440
|
-
}
|
|
582
|
+
return found;
|
|
583
|
+
}),
|
|
584
|
+
);
|
|
585
|
+
};
|
|
441
586
|
|
|
442
|
-
|
|
443
|
-
|
|
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);
|
|
444
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);
|
|
445
659
|
}
|
|
660
|
+
}
|
|
446
661
|
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
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);
|
|
451
672
|
}
|
|
673
|
+
return graph;
|
|
674
|
+
};
|
|
452
675
|
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
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
|
+
}
|
|
459
697
|
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
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 = `${id}${Separators.primary}${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
|
+
}
|
|
468
717
|
|
|
469
|
-
|
|
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);
|
|
470
723
|
}
|
|
724
|
+
return graph;
|
|
725
|
+
};
|
|
471
726
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
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);
|
|
476
758
|
}
|
|
759
|
+
}
|
|
477
760
|
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
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
|
+
};
|
|
485
787
|
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
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!);
|
|
492
820
|
}
|
|
821
|
+
}
|
|
493
822
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
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);
|
|
498
850
|
}
|
|
851
|
+
}
|
|
499
852
|
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
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,
|
|
507
882
|
});
|
|
508
|
-
|
|
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 prefix = `${id}${Separators.primary}`;
|
|
904
|
+
const toApply = [...internal._pendingExpands].filter((k) => k.startsWith(prefix));
|
|
905
|
+
for (const pendingKey of toApply) {
|
|
906
|
+
internal._pendingExpands.delete(pendingKey);
|
|
907
|
+
const relation = relationFromKey(pendingKey.slice(prefix.length));
|
|
908
|
+
Record.set(internal._expanded, pendingKey, true);
|
|
909
|
+
internal._onExpand?.(id, relation);
|
|
910
|
+
}
|
|
911
|
+
},
|
|
912
|
+
});
|
|
509
913
|
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
outbound: target.outbound,
|
|
516
|
-
});
|
|
517
|
-
}
|
|
914
|
+
if (nodes) {
|
|
915
|
+
addNodesImpl(graph, nodes);
|
|
916
|
+
const _edges = nodes.map((node) => ({ source: id, target: node.id, relation: 'child' as const }));
|
|
917
|
+
addEdgesImpl(graph, _edges);
|
|
918
|
+
}
|
|
518
919
|
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
920
|
+
if (edges) {
|
|
921
|
+
todo();
|
|
922
|
+
}
|
|
923
|
+
return graph;
|
|
924
|
+
};
|
|
925
|
+
|
|
926
|
+
/**
|
|
927
|
+
* Add a node to the graph.
|
|
928
|
+
*/
|
|
929
|
+
export function addNode<T extends WritableGraph>(graph: T, nodeArg: Node.NodeArg<any, Record<string, any>>): T;
|
|
930
|
+
export function addNode(nodeArg: Node.NodeArg<any, Record<string, any>>): <T extends WritableGraph>(graph: T) => T;
|
|
931
|
+
export function addNode<T extends WritableGraph>(
|
|
932
|
+
graphOrNodeArg: T | Node.NodeArg<any, Record<string, any>>,
|
|
933
|
+
nodeArg?: Node.NodeArg<any, Record<string, any>>,
|
|
934
|
+
): T | (<T extends WritableGraph>(graph: T) => T) {
|
|
935
|
+
if (nodeArg === undefined) {
|
|
936
|
+
// Curried: addNode(nodeArg)
|
|
937
|
+
const nodeArg = graphOrNodeArg as Node.NodeArg<any, Record<string, any>>;
|
|
938
|
+
return <T extends WritableGraph>(graph: T) => addNodeImpl(graph, nodeArg);
|
|
939
|
+
} else {
|
|
940
|
+
// Direct: addNode(graph, nodeArg)
|
|
941
|
+
const graph = graphOrNodeArg as T;
|
|
942
|
+
return addNodeImpl(graph, nodeArg);
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
/**
|
|
947
|
+
* Implementation helper for removeNodes.
|
|
948
|
+
*/
|
|
949
|
+
const removeNodesImpl = <T extends WritableGraph>(graph: T, ids: string[], edges = false): T => {
|
|
950
|
+
Atom.batch(() => {
|
|
951
|
+
ids.map((id) => removeNodeImpl(graph, id, edges));
|
|
952
|
+
});
|
|
953
|
+
return graph;
|
|
954
|
+
};
|
|
955
|
+
|
|
956
|
+
/**
|
|
957
|
+
* Remove nodes from the graph.
|
|
958
|
+
*/
|
|
959
|
+
export function removeNodes<T extends WritableGraph>(graph: T, ids: string[], edges?: boolean): T;
|
|
960
|
+
export function removeNodes(ids: string[], edges?: boolean): <T extends WritableGraph>(graph: T) => T;
|
|
961
|
+
export function removeNodes<T extends WritableGraph>(
|
|
962
|
+
graphOrIds: T | string[],
|
|
963
|
+
idsOrEdges?: string[] | boolean,
|
|
964
|
+
edges?: boolean,
|
|
965
|
+
): T | (<T extends WritableGraph>(graph: T) => T) {
|
|
966
|
+
if (Array.isArray(graphOrIds)) {
|
|
967
|
+
// Curried: removeNodes(ids, edges?)
|
|
968
|
+
const ids = graphOrIds;
|
|
969
|
+
const edgesArg = typeof idsOrEdges === 'boolean' ? idsOrEdges : false;
|
|
970
|
+
return <T extends WritableGraph>(graph: T) => removeNodesImpl(graph, ids, edgesArg);
|
|
971
|
+
} else {
|
|
972
|
+
// Direct: removeNodes(graph, ids, edges?)
|
|
973
|
+
const graph = graphOrIds;
|
|
974
|
+
const ids = idsOrEdges as string[];
|
|
975
|
+
const edgesArg = edges ?? false;
|
|
976
|
+
return removeNodesImpl(graph, ids, edgesArg);
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
/**
|
|
981
|
+
* Implementation helper for removeNode.
|
|
982
|
+
*/
|
|
983
|
+
const removeNodeImpl = <T extends WritableGraph>(graph: T, id: string, edges = false): T => {
|
|
984
|
+
const internal = getInternal(graph);
|
|
985
|
+
const nodeAtom = internal._node(id);
|
|
986
|
+
// TODO(wittjosiah): Is there a way to mark these atom values for garbage collection?
|
|
987
|
+
internal._registry.set(nodeAtom, Option.none());
|
|
988
|
+
graph.onNodeChanged.emit({ id, node: Option.none() });
|
|
989
|
+
// TODO(wittjosiah): Reset expanded and initialized flags?
|
|
990
|
+
|
|
991
|
+
if (edges) {
|
|
992
|
+
const nodeEdges = internal._registry.get(internal._edges(id));
|
|
993
|
+
const edgesToRemove: Edge[] = [];
|
|
994
|
+
for (const [relationKeyValue, relatedIds] of Object.entries(nodeEdges)) {
|
|
995
|
+
const relation = relationFromKey(relationKeyValue);
|
|
996
|
+
const isInboundRelation = relation.direction === 'inbound';
|
|
997
|
+
for (const relatedId of relatedIds) {
|
|
998
|
+
if (isInboundRelation) {
|
|
999
|
+
// Inbound edge lists store source node IDs; reconstruct the canonical outbound edge.
|
|
1000
|
+
edgesToRemove.push({ source: relatedId, target: id, relation: inverseRelation(relation) });
|
|
1001
|
+
} else {
|
|
1002
|
+
edgesToRemove.push({ source: id, target: relatedId, relation });
|
|
1003
|
+
}
|
|
527
1004
|
}
|
|
528
1005
|
}
|
|
1006
|
+
removeEdgesImpl(graph, edgesToRemove);
|
|
529
1007
|
}
|
|
530
1008
|
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
1009
|
+
internal._onRemoveNode?.(id);
|
|
1010
|
+
return graph;
|
|
1011
|
+
};
|
|
1012
|
+
|
|
1013
|
+
/**
|
|
1014
|
+
* Remove a node from the graph.
|
|
1015
|
+
*/
|
|
1016
|
+
export function removeNode<T extends WritableGraph>(graph: T, id: string, edges?: boolean): T;
|
|
1017
|
+
export function removeNode(id: string, edges?: boolean): <T extends WritableGraph>(graph: T) => T;
|
|
1018
|
+
export function removeNode<T extends WritableGraph>(
|
|
1019
|
+
graphOrId: T | string,
|
|
1020
|
+
idOrEdges?: string | boolean,
|
|
1021
|
+
edges?: boolean,
|
|
1022
|
+
): T | (<T extends WritableGraph>(graph: T) => T) {
|
|
1023
|
+
if (typeof graphOrId === 'string') {
|
|
1024
|
+
// Curried: removeNode(id, edges?)
|
|
1025
|
+
const id = graphOrId;
|
|
1026
|
+
const edgesArg = typeof idOrEdges === 'boolean' ? idOrEdges : false;
|
|
1027
|
+
return <T extends WritableGraph>(graph: T) => removeNodeImpl(graph, id, edgesArg);
|
|
1028
|
+
} else {
|
|
1029
|
+
// Direct: removeNode(graph, id, edges?)
|
|
1030
|
+
const graph = graphOrId;
|
|
1031
|
+
const id = idOrEdges as string;
|
|
1032
|
+
const edgesArg = edges ?? false;
|
|
1033
|
+
return removeNodeImpl(graph, id, edgesArg);
|
|
538
1034
|
}
|
|
1035
|
+
}
|
|
539
1036
|
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
1037
|
+
/**
|
|
1038
|
+
* Implementation helper for addEdges.
|
|
1039
|
+
*/
|
|
1040
|
+
const addEdgesImpl = <T extends WritableGraph>(graph: T, edges: Edge[]): T => {
|
|
1041
|
+
Atom.batch(() => {
|
|
1042
|
+
edges.map((edge) => addEdgeImpl(graph, edge));
|
|
1043
|
+
});
|
|
1044
|
+
return graph;
|
|
1045
|
+
};
|
|
545
1046
|
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
1047
|
+
/**
|
|
1048
|
+
* Add edges to the graph.
|
|
1049
|
+
*/
|
|
1050
|
+
export function addEdges<T extends WritableGraph>(graph: T, edges: Edge[]): T;
|
|
1051
|
+
export function addEdges(edges: Edge[]): <T extends WritableGraph>(graph: T) => T;
|
|
1052
|
+
export function addEdges<T extends WritableGraph>(
|
|
1053
|
+
graphOrEdges: T | Edge[],
|
|
1054
|
+
edges?: Edge[],
|
|
1055
|
+
): T | (<T extends WritableGraph>(graph: T) => T) {
|
|
1056
|
+
if (edges === undefined) {
|
|
1057
|
+
// Curried: addEdges(edges)
|
|
1058
|
+
const edges = graphOrEdges as Edge[];
|
|
1059
|
+
return <T extends WritableGraph>(graph: T) => addEdgesImpl(graph, edges);
|
|
1060
|
+
} else {
|
|
1061
|
+
// Direct: addEdges(graph, edges)
|
|
1062
|
+
const graph = graphOrEdges as T;
|
|
1063
|
+
return addEdgesImpl(graph, edges);
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
551
1066
|
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
if (node.id === target) {
|
|
570
|
-
found = Option.some(path);
|
|
571
|
-
}
|
|
572
|
-
},
|
|
573
|
-
});
|
|
1067
|
+
/**
|
|
1068
|
+
* Implementation helper for addEdge.
|
|
1069
|
+
*/
|
|
1070
|
+
const addEdgeImpl = <T extends WritableGraph>(graph: T, edgeArg: Edge): T => {
|
|
1071
|
+
const relation = normalizeRelation(edgeArg.relation);
|
|
1072
|
+
const relationId = relationKey(relation);
|
|
1073
|
+
const inverse = inverseRelation(relation);
|
|
1074
|
+
const inverseId = relationKey(inverse);
|
|
1075
|
+
const internal = getInternal(graph);
|
|
1076
|
+
|
|
1077
|
+
const sourceAtom = internal._edges(edgeArg.source);
|
|
1078
|
+
const source = internal._registry.get(sourceAtom);
|
|
1079
|
+
const sourceList = source[relationId] ?? [];
|
|
1080
|
+
if (!sourceList.includes(edgeArg.target)) {
|
|
1081
|
+
log('add edge', { source: edgeArg.source, target: edgeArg.target, relation: relationId });
|
|
1082
|
+
internal._registry.set(sourceAtom, { ...source, [relationId]: [...sourceList, edgeArg.target] });
|
|
1083
|
+
}
|
|
574
1084
|
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
1085
|
+
const targetAtom = internal._edges(edgeArg.target);
|
|
1086
|
+
const target = internal._registry.get(targetAtom);
|
|
1087
|
+
const targetList = target[inverseId] ?? [];
|
|
1088
|
+
if (!targetList.includes(edgeArg.source)) {
|
|
1089
|
+
log('add inverse edge', { source: edgeArg.source, target: edgeArg.target, relation: inverseId });
|
|
1090
|
+
internal._registry.set(targetAtom, { ...target, [inverseId]: [...targetList, edgeArg.source] });
|
|
578
1091
|
}
|
|
579
1092
|
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
{ timeout = 5_000, interval = 500 }: { timeout?: number; interval?: number } = {},
|
|
583
|
-
): Promise<string[]> {
|
|
584
|
-
const path = this.getPath(params);
|
|
585
|
-
if (Option.isSome(path)) {
|
|
586
|
-
return path.value;
|
|
587
|
-
}
|
|
1093
|
+
return graph;
|
|
1094
|
+
};
|
|
588
1095
|
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
1096
|
+
/**
|
|
1097
|
+
* Add an edge to the graph.
|
|
1098
|
+
*/
|
|
1099
|
+
export function addEdge<T extends WritableGraph>(graph: T, edgeArg: Edge): T;
|
|
1100
|
+
export function addEdge(edgeArg: Edge): <T extends WritableGraph>(graph: T) => T;
|
|
1101
|
+
export function addEdge<T extends WritableGraph>(
|
|
1102
|
+
graphOrEdgeArg: T | Edge,
|
|
1103
|
+
edgeArg?: Edge,
|
|
1104
|
+
): T | (<T extends WritableGraph>(graph: T) => T) {
|
|
1105
|
+
if (edgeArg === undefined) {
|
|
1106
|
+
// Curried: addEdge(edgeArg)
|
|
1107
|
+
const edgeArg = graphOrEdgeArg as Edge;
|
|
1108
|
+
return <T extends WritableGraph>(graph: T) => addEdgeImpl(graph, edgeArg);
|
|
1109
|
+
} else {
|
|
1110
|
+
// Direct: addEdge(graph, edgeArg)
|
|
1111
|
+
const graph = graphOrEdgeArg as T;
|
|
1112
|
+
return addEdgeImpl(graph, edgeArg);
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
/**
|
|
1117
|
+
* Implementation helper for removeEdges.
|
|
1118
|
+
*/
|
|
1119
|
+
const removeEdgesImpl = <T extends WritableGraph>(graph: T, edges: Edge[], removeOrphans = false): T => {
|
|
1120
|
+
Atom.batch(() => {
|
|
1121
|
+
edges.map((edge) => removeEdgeImpl(graph, edge, removeOrphans));
|
|
1122
|
+
});
|
|
1123
|
+
return graph;
|
|
1124
|
+
};
|
|
1125
|
+
|
|
1126
|
+
/**
|
|
1127
|
+
* Remove edges from the graph.
|
|
1128
|
+
*/
|
|
1129
|
+
export function removeEdges<T extends WritableGraph>(graph: T, edges: Edge[], removeOrphans?: boolean): T;
|
|
1130
|
+
export function removeEdges(edges: Edge[], removeOrphans?: boolean): <T extends WritableGraph>(graph: T) => T;
|
|
1131
|
+
export function removeEdges<T extends WritableGraph>(
|
|
1132
|
+
graphOrEdges: T | Edge[],
|
|
1133
|
+
edgesOrRemoveOrphans?: Edge[] | boolean,
|
|
1134
|
+
removeOrphans?: boolean,
|
|
1135
|
+
): T | (<T extends WritableGraph>(graph: T) => T) {
|
|
1136
|
+
if (Array.isArray(graphOrEdges)) {
|
|
1137
|
+
// Curried: removeEdges(edges, removeOrphans?)
|
|
1138
|
+
const edges = graphOrEdges;
|
|
1139
|
+
const removeOrphansArg = typeof edgesOrRemoveOrphans === 'boolean' ? edgesOrRemoveOrphans : false;
|
|
1140
|
+
return <T extends WritableGraph>(graph: T) => removeEdgesImpl(graph, edges, removeOrphansArg);
|
|
1141
|
+
} else {
|
|
1142
|
+
// Direct: removeEdges(graph, edges, removeOrphans?)
|
|
1143
|
+
const graph = graphOrEdges;
|
|
1144
|
+
const edges = edgesOrRemoveOrphans as Edge[];
|
|
1145
|
+
const removeOrphansArg = removeOrphans ?? false;
|
|
1146
|
+
return removeEdgesImpl(graph, edges, removeOrphansArg);
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
596
1149
|
|
|
597
|
-
|
|
1150
|
+
/**
|
|
1151
|
+
* Implementation helper for removeEdge.
|
|
1152
|
+
*/
|
|
1153
|
+
const removeEdgeImpl = <T extends WritableGraph>(graph: T, edgeArg: Edge, removeOrphans = false): T => {
|
|
1154
|
+
const relation = normalizeRelation(edgeArg.relation);
|
|
1155
|
+
const relationId = relationKey(relation);
|
|
1156
|
+
const inverse = inverseRelation(relation);
|
|
1157
|
+
const inverseId = relationKey(inverse);
|
|
1158
|
+
const internal = getInternal(graph);
|
|
1159
|
+
|
|
1160
|
+
const sourceAtom = internal._edges(edgeArg.source);
|
|
1161
|
+
const source = internal._registry.get(sourceAtom);
|
|
1162
|
+
const sourceList = source[relationId] ?? [];
|
|
1163
|
+
if (sourceList.includes(edgeArg.target)) {
|
|
1164
|
+
internal._registry.set(sourceAtom, { ...source, [relationId]: sourceList.filter((id) => id !== edgeArg.target) });
|
|
598
1165
|
}
|
|
599
1166
|
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
1167
|
+
const targetAtom = internal._edges(edgeArg.target);
|
|
1168
|
+
const target = internal._registry.get(targetAtom);
|
|
1169
|
+
const targetList = target[inverseId] ?? [];
|
|
1170
|
+
if (targetList.includes(edgeArg.source)) {
|
|
1171
|
+
internal._registry.set(targetAtom, { ...target, [inverseId]: targetList.filter((id) => id !== edgeArg.source) });
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
if (removeOrphans) {
|
|
1175
|
+
const sourceAfter = internal._registry.get(sourceAtom);
|
|
1176
|
+
const targetAfter = internal._registry.get(targetAtom);
|
|
1177
|
+
const isEmpty = (edges: Edges) => Object.values(edges).every((ids) => ids.length === 0);
|
|
1178
|
+
if (isEmpty(sourceAfter) && edgeArg.source !== Node.RootId) {
|
|
1179
|
+
removeNodesImpl(graph, [edgeArg.source]);
|
|
1180
|
+
}
|
|
1181
|
+
if (isEmpty(targetAfter) && edgeArg.target !== Node.RootId) {
|
|
1182
|
+
removeNodesImpl(graph, [edgeArg.target]);
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
return graph;
|
|
1186
|
+
};
|
|
1187
|
+
|
|
1188
|
+
/**
|
|
1189
|
+
* Remove an edge from the graph.
|
|
1190
|
+
*/
|
|
1191
|
+
export function removeEdge<T extends WritableGraph>(graph: T, edgeArg: Edge, removeOrphans?: boolean): T;
|
|
1192
|
+
export function removeEdge(edgeArg: Edge, removeOrphans?: boolean): <T extends WritableGraph>(graph: T) => T;
|
|
1193
|
+
export function removeEdge<T extends WritableGraph>(
|
|
1194
|
+
graphOrEdgeArg: T | Edge,
|
|
1195
|
+
edgeArgOrRemoveOrphans?: Edge | boolean,
|
|
1196
|
+
removeOrphans?: boolean,
|
|
1197
|
+
): T | (<T extends WritableGraph>(graph: T) => T) {
|
|
1198
|
+
if (
|
|
1199
|
+
edgeArgOrRemoveOrphans === undefined ||
|
|
1200
|
+
typeof edgeArgOrRemoveOrphans === 'boolean' ||
|
|
1201
|
+
'source' in graphOrEdgeArg
|
|
1202
|
+
) {
|
|
1203
|
+
// Curried: removeEdge(edgeArg, removeOrphans?)
|
|
1204
|
+
const edgeArg = graphOrEdgeArg as Edge;
|
|
1205
|
+
const removeOrphansArg = typeof edgeArgOrRemoveOrphans === 'boolean' ? edgeArgOrRemoveOrphans : false;
|
|
1206
|
+
return <T extends WritableGraph>(graph: T) => removeEdgeImpl(graph, edgeArg, removeOrphansArg);
|
|
1207
|
+
} else {
|
|
1208
|
+
// Direct: removeEdge(graph, edgeArg, removeOrphans?)
|
|
1209
|
+
const graph = graphOrEdgeArg as T;
|
|
1210
|
+
const edgeArg = edgeArgOrRemoveOrphans as Edge;
|
|
1211
|
+
const removeOrphansArg = removeOrphans ?? false;
|
|
1212
|
+
return removeEdgeImpl(graph, edgeArg, removeOrphansArg);
|
|
603
1213
|
}
|
|
604
1214
|
}
|
|
1215
|
+
|
|
1216
|
+
/**
|
|
1217
|
+
* Creates a new Graph instance.
|
|
1218
|
+
*/
|
|
1219
|
+
export const make = (params?: GraphProps): Graph => {
|
|
1220
|
+
return new GraphImpl(params);
|
|
1221
|
+
};
|
|
1222
|
+
|
|
1223
|
+
//
|
|
1224
|
+
// Utilities
|
|
1225
|
+
//
|
|
1226
|
+
|
|
1227
|
+
export const relationKey = (relation: Node.RelationInput): string => {
|
|
1228
|
+
const normalized = normalizeRelation(relation);
|
|
1229
|
+
return `${normalized.kind}${Separators.secondary}${normalized.direction}`;
|
|
1230
|
+
};
|
|
1231
|
+
|
|
1232
|
+
export const relationFromKey = (encoded: string): Node.Relation => {
|
|
1233
|
+
const separatorIndex = encoded.lastIndexOf(Separators.secondary);
|
|
1234
|
+
invariant(separatorIndex > 0 && separatorIndex < encoded.length - 1, `Invalid relation key: ${encoded}`);
|
|
1235
|
+
const kind = encoded.slice(0, separatorIndex);
|
|
1236
|
+
const directionRaw = encoded.slice(separatorIndex + 1);
|
|
1237
|
+
invariant(directionRaw === 'outbound' || directionRaw === 'inbound', `Invalid relation direction: ${directionRaw}`);
|
|
1238
|
+
return Node.relation(kind, directionRaw);
|
|
1239
|
+
};
|
|
1240
|
+
|
|
1241
|
+
const connectionKey = (id: string, relation: Node.RelationInput): string =>
|
|
1242
|
+
`${id}${Separators.primary}${relationKey(relation)}`;
|
|
1243
|
+
|
|
1244
|
+
const relationFromConnectionKey = (key: string): { id: string; relation: Node.Relation } => {
|
|
1245
|
+
const separatorIndex = key.indexOf(Separators.primary);
|
|
1246
|
+
invariant(separatorIndex > 0 && separatorIndex < key.length - 1, `Invalid connection key: ${key}`);
|
|
1247
|
+
const id = key.slice(0, separatorIndex);
|
|
1248
|
+
const encodedRelation = key.slice(separatorIndex + 1);
|
|
1249
|
+
return { id, relation: relationFromKey(encodedRelation) };
|
|
1250
|
+
};
|
|
1251
|
+
|
|
1252
|
+
const inverseRelation = (relation: Node.RelationInput): Node.Relation => {
|
|
1253
|
+
const normalized = normalizeRelation(relation);
|
|
1254
|
+
return Node.relation(normalized.kind, normalized.direction === 'outbound' ? 'inbound' : 'outbound');
|
|
1255
|
+
};
|