@dxos/app-graph 0.8.4-main.b97322e → 0.8.4-main.bcb3aa67d6
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 +27 -767
- 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 +27 -768
- 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 +117 -60
- package/dist/types/src/graph-builder.d.ts.map +1 -1
- package/dist/types/src/graph.d.ts +188 -218
- 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 +8 -10
- 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 +48 -36
- package/src/atoms.ts +25 -0
- package/src/graph-builder.test.ts +1067 -126
- package/src/graph-builder.ts +734 -264
- package/src/graph.test.ts +451 -123
- package/src/graph.ts +1054 -407
- 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 +173 -133
- package/src/stories/Tree.tsx +1 -1
- 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 -218
- package/src/testing.ts +0 -20
package/src/graph.ts
CHANGED
|
@@ -2,42 +2,45 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
5
|
+
import { Atom, Registry } from '@effect-atom/atom-react';
|
|
6
|
+
import * as Function from 'effect/Function';
|
|
7
|
+
import * as Option from 'effect/Option';
|
|
8
|
+
import * as Pipeable from 'effect/Pipeable';
|
|
9
|
+
import * as Record from 'effect/Record';
|
|
7
10
|
|
|
8
11
|
import { Event, Trigger } from '@dxos/async';
|
|
9
12
|
import { todo } from '@dxos/debug';
|
|
10
13
|
import { invariant } from '@dxos/invariant';
|
|
11
14
|
import { log } from '@dxos/log';
|
|
12
|
-
import {
|
|
15
|
+
import { type MakeOptional, isNonNullable } from '@dxos/util';
|
|
13
16
|
|
|
14
|
-
import
|
|
17
|
+
import * as Node from './node';
|
|
18
|
+
import { normalizeRelation, primaryKey, primaryParts, secondaryKey, secondaryParts, shallowEqual } from './util';
|
|
15
19
|
|
|
16
20
|
const graphSymbol = Symbol('graph');
|
|
17
|
-
|
|
18
|
-
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 };
|
|
19
27
|
|
|
20
28
|
/**
|
|
21
29
|
* Get the Graph a Node is currently associated with.
|
|
22
30
|
*/
|
|
23
|
-
export const getGraph = (node: Node): Graph => {
|
|
31
|
+
export const getGraph = (node: Node.Node): Graph => {
|
|
24
32
|
const graph = (node as NodeInternal)[graphSymbol];
|
|
25
33
|
invariant(graph, 'Node is not associated with a graph.');
|
|
26
|
-
return graph;
|
|
34
|
+
return graph as Graph;
|
|
27
35
|
};
|
|
28
36
|
|
|
29
|
-
export const ROOT_ID = 'root';
|
|
30
|
-
export const ROOT_TYPE = 'dxos.org/type/GraphRoot';
|
|
31
|
-
export const ACTION_TYPE = 'dxos.org/type/GraphAction';
|
|
32
|
-
export const ACTION_GROUP_TYPE = 'dxos.org/type/GraphActionGroup';
|
|
33
|
-
|
|
34
37
|
export type GraphTraversalOptions = {
|
|
35
38
|
/**
|
|
36
39
|
* A callback which is called for each node visited during traversal.
|
|
37
40
|
*
|
|
38
41
|
* If the callback returns `false`, traversal is stops recursing.
|
|
39
42
|
*/
|
|
40
|
-
visitor: (node: Node, path: string[]) => boolean | void;
|
|
43
|
+
visitor: (node: Node.Node, path: string[]) => boolean | void;
|
|
41
44
|
|
|
42
45
|
/**
|
|
43
46
|
* The node to start traversing from.
|
|
@@ -46,247 +49,167 @@ export type GraphTraversalOptions = {
|
|
|
46
49
|
*/
|
|
47
50
|
source?: string;
|
|
48
51
|
|
|
49
|
-
/**
|
|
50
|
-
|
|
51
|
-
*
|
|
52
|
-
* @default 'outbound'
|
|
53
|
-
*/
|
|
54
|
-
relation?: Relation;
|
|
52
|
+
/** The relation(s) to traverse graph edges. */
|
|
53
|
+
relation: Node.RelationInput | Node.RelationInput[];
|
|
55
54
|
};
|
|
56
55
|
|
|
57
|
-
export type
|
|
56
|
+
export type GraphProps = {
|
|
58
57
|
registry?: Registry.Registry;
|
|
59
|
-
nodes?: MakeOptional<Node, 'data' | 'cacheable'>[];
|
|
58
|
+
nodes?: MakeOptional<Node.Node, 'data' | 'cacheable'>[];
|
|
60
59
|
edges?: Record<string, Edges>;
|
|
61
|
-
onExpand?:
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
onRemoveNode?: Graph['_onRemoveNode'];
|
|
60
|
+
onExpand?: (id: string, relation: Node.Relation) => void;
|
|
61
|
+
onInitialize?: (id: string) => Promise<void>;
|
|
62
|
+
onRemoveNode?: (id: string) => void;
|
|
65
63
|
};
|
|
66
64
|
|
|
67
|
-
export type Edge = { source: string; target: string };
|
|
68
|
-
export type Edges =
|
|
69
|
-
|
|
70
|
-
export interface ReadableGraph {
|
|
71
|
-
/**
|
|
72
|
-
* Event emitted when a node is changed.
|
|
73
|
-
*/
|
|
74
|
-
onNodeChanged: Event<{ id: string; node: Option.Option<Node> }>;
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Convert the graph to a JSON object.
|
|
78
|
-
*/
|
|
79
|
-
toJSON(id?: string): object;
|
|
80
|
-
|
|
81
|
-
json(id?: string): Rx.Rx<any>;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Get the rx key for the node with the given id.
|
|
85
|
-
*/
|
|
86
|
-
node(id: string): Rx.Rx<Option.Option<Node>>;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Get the rx key for the node with the given id.
|
|
90
|
-
*/
|
|
91
|
-
nodeOrThrow(id: string): Rx.Rx<Node>;
|
|
65
|
+
export type Edge = { source: string; target: string; relation: Node.RelationInput };
|
|
66
|
+
export type Edges = Record<string, string[]>;
|
|
92
67
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Get the rx key for the actions of the node with the given id.
|
|
100
|
-
*/
|
|
101
|
-
actions(id: string): Rx.Rx<(Action | ActionGroup)[]>;
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Get the rx key for the edges of the node with the given id.
|
|
105
|
-
*/
|
|
106
|
-
edges(id: string): Rx.Rx<Edges>;
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Alias for `getNodeOrThrow(ROOT_ID)`.
|
|
110
|
-
*/
|
|
111
|
-
get root(): Node;
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Get the node with the given id from the graph's registry.
|
|
115
|
-
*/
|
|
116
|
-
getNode(id: string): Option.Option<Node>;
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Get the node with the given id from the graph's registry.
|
|
120
|
-
*
|
|
121
|
-
* @throws If the node is Option.none().
|
|
122
|
-
*/
|
|
123
|
-
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;
|
|
124
73
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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;
|
|
129
79
|
|
|
130
|
-
|
|
131
|
-
* Get all actions connected to the node with the given id from the graph's registry.
|
|
132
|
-
*/
|
|
133
|
-
getActions(id: string): Node[];
|
|
80
|
+
export type GraphKindType = 'readable' | 'expandable' | 'writable';
|
|
134
81
|
|
|
82
|
+
export interface BaseGraph extends Pipeable.Pipeable {
|
|
83
|
+
readonly [GraphTypeId]: GraphTypeId;
|
|
84
|
+
readonly [GraphKind]: GraphKindType;
|
|
135
85
|
/**
|
|
136
|
-
*
|
|
86
|
+
* Event emitted when a node is changed.
|
|
137
87
|
*/
|
|
138
|
-
|
|
139
|
-
|
|
88
|
+
readonly onNodeChanged: Event<{ id: string; node: Option.Option<Node.Node> }>;
|
|
140
89
|
/**
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
* @param options.node The node to start traversing from.
|
|
144
|
-
* @param options.relation The relation to traverse graph edges.
|
|
145
|
-
* @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.
|
|
146
91
|
*/
|
|
147
|
-
|
|
148
|
-
|
|
92
|
+
json(id?: string): Atom.Atom<any>;
|
|
149
93
|
/**
|
|
150
|
-
* Get the
|
|
94
|
+
* Get the atom key for the node with the given id.
|
|
151
95
|
*/
|
|
152
|
-
|
|
153
|
-
|
|
96
|
+
node(id: string): Atom.Atom<Option.Option<Node.Node>>;
|
|
154
97
|
/**
|
|
155
|
-
*
|
|
98
|
+
* Get the atom key for the node with the given id.
|
|
156
99
|
*/
|
|
157
|
-
|
|
158
|
-
params: { source?: string; target: string },
|
|
159
|
-
options?: { timeout?: number; interval?: number },
|
|
160
|
-
): Promise<string[]>;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export interface ExpandableGraph extends ReadableGraph {
|
|
100
|
+
nodeOrThrow(id: string): Atom.Atom<Node.Node>;
|
|
164
101
|
/**
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
* 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.
|
|
168
103
|
*/
|
|
169
|
-
|
|
170
|
-
|
|
104
|
+
connections(id: string, relation: Node.RelationInput): Atom.Atom<Node.Node[]>;
|
|
171
105
|
/**
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
* 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.
|
|
175
107
|
*/
|
|
176
|
-
|
|
177
|
-
|
|
108
|
+
actions(id: string): Atom.Atom<(Node.Action | Node.ActionGroup)[]>;
|
|
178
109
|
/**
|
|
179
|
-
*
|
|
110
|
+
* Get the atom key for the edges of the node with the given id.
|
|
180
111
|
*/
|
|
181
|
-
|
|
112
|
+
edges(id: string): Atom.Atom<Edges>;
|
|
182
113
|
}
|
|
183
114
|
|
|
184
|
-
export
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
*/
|
|
188
|
-
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' };
|
|
189
118
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Remove nodes from the graph.
|
|
197
|
-
*/
|
|
198
|
-
removeNodes(ids: string[], edges?: boolean): void;
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Remove a node from the graph.
|
|
202
|
-
*/
|
|
203
|
-
removeNode(id: string, edges?: boolean): void;
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Add edges to the graph.
|
|
207
|
-
*/
|
|
208
|
-
addEdges(edges: Edge[]): void;
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Add an edge to the graph.
|
|
212
|
-
*/
|
|
213
|
-
addEdge(edge: Edge): void;
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Remove edges from the graph.
|
|
217
|
-
*/
|
|
218
|
-
removeEdges(edges: Edge[], removeOrphans?: boolean): void;
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Remove an edge from the graph.
|
|
222
|
-
*/
|
|
223
|
-
removeEdge(edge: Edge, removeOrphans?: boolean): void;
|
|
224
|
-
}
|
|
119
|
+
/**
|
|
120
|
+
* Graph interface.
|
|
121
|
+
*/
|
|
122
|
+
export type Graph = WritableGraph;
|
|
225
123
|
|
|
226
124
|
/**
|
|
227
125
|
* The Graph represents the user interface information architecture of the application constructed via plugins.
|
|
126
|
+
* @internal
|
|
228
127
|
*/
|
|
229
|
-
|
|
230
|
-
readonly
|
|
128
|
+
class GraphImpl implements WritableGraph {
|
|
129
|
+
readonly [GraphTypeId]: GraphTypeId = GraphTypeId;
|
|
130
|
+
readonly [GraphKind] = 'writable' as const;
|
|
231
131
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
132
|
+
pipe() {
|
|
133
|
+
// eslint-disable-next-line prefer-rest-params
|
|
134
|
+
return Pipeable.pipeArguments(this, arguments);
|
|
135
|
+
}
|
|
235
136
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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
|
+
],
|
|
242
161
|
]);
|
|
243
162
|
|
|
244
163
|
/** @internal */
|
|
245
|
-
readonly _node =
|
|
164
|
+
readonly _node = Atom.family<string, Atom.Writable<Option.Option<Node.Node>>>((id) => {
|
|
246
165
|
const initial = Option.flatten(Record.get(this._initialNodes, id));
|
|
247
|
-
return
|
|
166
|
+
return Atom.make<Option.Option<Node.Node>>(initial).pipe(Atom.keepAlive, Atom.withLabel(`graph:node:${id}`));
|
|
248
167
|
});
|
|
249
168
|
|
|
250
|
-
|
|
251
|
-
return
|
|
169
|
+
readonly _nodeOrThrow = Atom.family<string, Atom.Atom<Node.Node>>((id) => {
|
|
170
|
+
return Atom.make((get) => {
|
|
252
171
|
const node = get(this._node(id));
|
|
253
172
|
invariant(Option.isSome(node), `Node not available: ${id}`);
|
|
254
173
|
return node.value;
|
|
255
174
|
});
|
|
256
175
|
});
|
|
257
176
|
|
|
258
|
-
|
|
259
|
-
const initial = Record.get(this._initialEdges, id).pipe(Option.getOrElse(() => ({
|
|
260
|
-
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}`));
|
|
261
180
|
});
|
|
262
181
|
|
|
263
|
-
// NOTE: Currently the argument to the family needs to be referentially stable for the
|
|
264
|
-
// TODO(wittjosiah):
|
|
265
|
-
|
|
266
|
-
return
|
|
267
|
-
|
|
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 || primaryParts(key).length < 2) {
|
|
187
|
+
return [];
|
|
188
|
+
}
|
|
189
|
+
const { id, relation } = relationFromConnectionKey(key);
|
|
268
190
|
const edges = get(this._edges(id));
|
|
269
|
-
return edges[relation
|
|
191
|
+
return (edges[relationKey(relation)] ?? [])
|
|
270
192
|
.map((id) => get(this._node(id)))
|
|
271
193
|
.filter(Option.isSome)
|
|
272
194
|
.map((o) => o.value);
|
|
273
|
-
}).pipe(
|
|
195
|
+
}).pipe(Atom.withLabel(`graph:connections:${key}`));
|
|
274
196
|
});
|
|
275
197
|
|
|
276
|
-
|
|
277
|
-
return
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
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}`));
|
|
282
205
|
});
|
|
283
206
|
|
|
284
|
-
|
|
285
|
-
return
|
|
286
|
-
const toJSON = (node: Node, seen: string[] = []): any => {
|
|
287
|
-
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')));
|
|
288
211
|
const obj: Record<string, any> = {
|
|
289
|
-
id: node.id
|
|
212
|
+
id: node.id,
|
|
290
213
|
type: node.type,
|
|
291
214
|
};
|
|
292
215
|
if (node.properties.label) {
|
|
@@ -294,7 +217,7 @@ export class Graph implements WritableGraph {
|
|
|
294
217
|
}
|
|
295
218
|
if (nodes.length) {
|
|
296
219
|
obj.nodes = nodes
|
|
297
|
-
.map((n) => {
|
|
220
|
+
.map((n: Node.Node) => {
|
|
298
221
|
// Break cycles.
|
|
299
222
|
const nextSeen = [...seen, node.id];
|
|
300
223
|
return nextSeen.includes(n.id) ? undefined : toJSON(n, nextSeen);
|
|
@@ -304,13 +227,14 @@ export class Graph implements WritableGraph {
|
|
|
304
227
|
return obj;
|
|
305
228
|
};
|
|
306
229
|
|
|
307
|
-
const root = get(this.
|
|
230
|
+
const root = get(this._nodeOrThrow(id));
|
|
308
231
|
return toJSON(root);
|
|
309
|
-
}).pipe(
|
|
232
|
+
}).pipe(Atom.withLabel(`graph:json:${id}`));
|
|
310
233
|
});
|
|
311
234
|
|
|
312
|
-
constructor({ registry, nodes, edges, onExpand, onRemoveNode }:
|
|
235
|
+
constructor({ registry, nodes, edges, onInitialize, onExpand, onRemoveNode }: GraphProps = {}) {
|
|
313
236
|
this._registry = registry ?? Registry.make();
|
|
237
|
+
this._onInitialize = onInitialize;
|
|
314
238
|
this._onExpand = onExpand;
|
|
315
239
|
this._onRemoveNode = onRemoveNode;
|
|
316
240
|
|
|
@@ -327,277 +251,1000 @@ export class Graph implements WritableGraph {
|
|
|
327
251
|
}
|
|
328
252
|
}
|
|
329
253
|
|
|
330
|
-
|
|
331
|
-
return
|
|
254
|
+
json(id = Node.RootId): Atom.Atom<any> {
|
|
255
|
+
return jsonImpl(this, id);
|
|
332
256
|
}
|
|
333
257
|
|
|
334
|
-
|
|
335
|
-
return this
|
|
258
|
+
node(id: string): Atom.Atom<Option.Option<Node.Node>> {
|
|
259
|
+
return nodeImpl(this, id);
|
|
336
260
|
}
|
|
337
261
|
|
|
338
|
-
|
|
339
|
-
return this
|
|
262
|
+
nodeOrThrow(id: string): Atom.Atom<Node.Node> {
|
|
263
|
+
return nodeOrThrowImpl(this, id);
|
|
340
264
|
}
|
|
341
265
|
|
|
342
|
-
|
|
343
|
-
return this
|
|
266
|
+
connections(id: string, relation: Node.RelationInput): Atom.Atom<Node.Node[]> {
|
|
267
|
+
return connectionsImpl(this, id, relation);
|
|
344
268
|
}
|
|
345
269
|
|
|
346
|
-
|
|
347
|
-
return this
|
|
270
|
+
actions(id: string): Atom.Atom<(Node.Action | Node.ActionGroup)[]> {
|
|
271
|
+
return actionsImpl(this, id);
|
|
348
272
|
}
|
|
349
273
|
|
|
350
|
-
|
|
351
|
-
return this
|
|
274
|
+
edges(id: string): Atom.Atom<Edges> {
|
|
275
|
+
return edgesImpl(this, id);
|
|
352
276
|
}
|
|
353
277
|
|
|
354
|
-
|
|
355
|
-
|
|
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
|
+
});
|
|
356
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
|
+
};
|
|
357
312
|
|
|
358
|
-
|
|
359
|
-
|
|
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!);
|
|
360
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
|
+
};
|
|
361
388
|
|
|
362
|
-
|
|
363
|
-
|
|
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!);
|
|
364
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
|
+
};
|
|
365
426
|
|
|
366
|
-
|
|
367
|
-
|
|
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);
|
|
368
449
|
}
|
|
450
|
+
}
|
|
369
451
|
|
|
370
|
-
|
|
371
|
-
|
|
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!);
|
|
372
477
|
}
|
|
478
|
+
}
|
|
373
479
|
|
|
374
|
-
|
|
375
|
-
|
|
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!);
|
|
376
502
|
}
|
|
503
|
+
}
|
|
377
504
|
|
|
378
|
-
|
|
379
|
-
|
|
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;
|
|
380
516
|
}
|
|
381
517
|
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
// await this._onInitialize?.(id);
|
|
388
|
-
// Record.set(this._initialized, id, true);
|
|
389
|
-
// }
|
|
390
|
-
// }
|
|
518
|
+
const node = getNodeOrThrow(graph, source);
|
|
519
|
+
const shouldContinue = visitor(node, [...path, source]);
|
|
520
|
+
if (shouldContinue === false) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
391
523
|
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
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
|
+
}
|
|
399
532
|
}
|
|
400
533
|
}
|
|
534
|
+
};
|
|
401
535
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
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);
|
|
406
557
|
}
|
|
558
|
+
}
|
|
407
559
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
this.onNodeChanged.emit({ id, node: newNode });
|
|
430
|
-
},
|
|
431
|
-
});
|
|
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
|
+
});
|
|
432
581
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
this.addEdges(_edges);
|
|
438
|
-
// });
|
|
439
|
-
}
|
|
582
|
+
return found;
|
|
583
|
+
}),
|
|
584
|
+
);
|
|
585
|
+
};
|
|
440
586
|
|
|
441
|
-
|
|
442
|
-
|
|
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);
|
|
443
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);
|
|
444
659
|
}
|
|
660
|
+
}
|
|
445
661
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
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);
|
|
450
672
|
}
|
|
673
|
+
return graph;
|
|
674
|
+
};
|
|
451
675
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
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
|
+
}
|
|
458
697
|
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
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
|
+
}
|
|
467
717
|
|
|
468
|
-
|
|
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);
|
|
469
723
|
}
|
|
724
|
+
return graph;
|
|
725
|
+
};
|
|
470
726
|
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
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);
|
|
475
758
|
}
|
|
759
|
+
}
|
|
476
760
|
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
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
|
+
};
|
|
484
787
|
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
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!);
|
|
491
820
|
}
|
|
821
|
+
}
|
|
492
822
|
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
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);
|
|
497
850
|
}
|
|
851
|
+
}
|
|
498
852
|
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
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,
|
|
506
882
|
});
|
|
507
|
-
|
|
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
|
+
});
|
|
508
912
|
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
outbound: target.outbound,
|
|
515
|
-
});
|
|
516
|
-
}
|
|
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
|
+
}
|
|
517
918
|
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
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
|
+
}
|
|
526
1003
|
}
|
|
527
1004
|
}
|
|
1005
|
+
removeEdgesImpl(graph, edgesToRemove);
|
|
528
1006
|
}
|
|
529
1007
|
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
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);
|
|
537
1033
|
}
|
|
1034
|
+
}
|
|
538
1035
|
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
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
|
+
};
|
|
544
1045
|
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
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
|
+
}
|
|
550
1065
|
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
if (node.id === target) {
|
|
569
|
-
found = Option.some(path);
|
|
570
|
-
}
|
|
571
|
-
},
|
|
572
|
-
});
|
|
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
|
+
}
|
|
573
1083
|
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
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] });
|
|
577
1090
|
}
|
|
578
1091
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
{ timeout = 5_000, interval = 500 }: { timeout?: number; interval?: number } = {},
|
|
582
|
-
): Promise<string[]> {
|
|
583
|
-
const path = this.getPath(params);
|
|
584
|
-
if (Option.isSome(path)) {
|
|
585
|
-
return path.value;
|
|
586
|
-
}
|
|
1092
|
+
return graph;
|
|
1093
|
+
};
|
|
587
1094
|
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
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
|
+
}
|
|
1114
|
+
|
|
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);
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
595
1148
|
|
|
596
|
-
|
|
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) });
|
|
597
1164
|
}
|
|
598
1165
|
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
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);
|
|
602
1212
|
}
|
|
603
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
|
+
};
|