@dxos/app-graph 0.8.4-main.72ec0f3 → 0.8.4-main.765dc60934
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/LICENSE +102 -5
- package/README.md +1 -1
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/neutral/chunk-WJJ5KEOH.mjs +1477 -0
- package/dist/lib/neutral/chunk-WJJ5KEOH.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +40 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/neutral/scheduler.mjs +15 -0
- package/dist/lib/neutral/scheduler.mjs.map +7 -0
- package/dist/lib/neutral/testing/index.mjs +40 -0
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/atoms.d.ts +8 -0
- package/dist/types/src/atoms.d.ts.map +1 -0
- package/dist/types/src/graph-builder.d.ts +111 -65
- package/dist/types/src/graph-builder.d.ts.map +1 -1
- package/dist/types/src/graph.d.ts +179 -213
- package/dist/types/src/graph.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +7 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/node-matcher.d.ts +244 -0
- package/dist/types/src/node-matcher.d.ts.map +1 -0
- package/dist/types/src/node-matcher.test.d.ts +2 -0
- package/dist/types/src/node-matcher.test.d.ts.map +1 -0
- package/dist/types/src/node.d.ts +50 -5
- package/dist/types/src/node.d.ts.map +1 -1
- package/dist/types/src/scheduler.browser.d.ts +2 -0
- package/dist/types/src/scheduler.browser.d.ts.map +1 -0
- package/dist/types/src/scheduler.d.ts +8 -0
- package/dist/types/src/scheduler.d.ts.map +1 -0
- 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 +40 -0
- package/dist/types/src/util.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +53 -40
- package/src/atoms.ts +25 -0
- package/src/graph-builder.test.ts +1121 -112
- package/src/graph-builder.ts +724 -284
- package/src/graph.test.ts +448 -120
- package/src/graph.ts +1022 -413
- package/src/index.ts +10 -3
- package/src/node-matcher.test.ts +301 -0
- package/src/node-matcher.ts +314 -0
- package/src/node.ts +82 -8
- package/src/scheduler.browser.ts +5 -0
- package/src/scheduler.ts +17 -0
- package/src/stories/EchoGraph.stories.tsx +151 -121
- package/src/stories/Tree.tsx +2 -2
- package/src/testing/index.ts +5 -0
- package/src/testing/setup-graph-builder.ts +41 -0
- package/src/util.ts +101 -0
- package/dist/lib/browser/index.mjs +0 -836
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/node-esm/index.mjs +0 -838
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/types/src/experimental/graph-projections.test.d.ts +0 -25
- package/dist/types/src/experimental/graph-projections.test.d.ts.map +0 -1
- package/dist/types/src/signals-integration.test.d.ts +0 -2
- package/dist/types/src/signals-integration.test.d.ts.map +0 -1
- package/dist/types/src/testing.d.ts +0 -5
- package/dist/types/src/testing.d.ts.map +0 -1
- package/src/experimental/graph-projections.test.ts +0 -56
- package/src/signals-integration.test.ts +0 -219
- package/src/testing.ts +0 -20
package/src/graph-builder.ts
CHANGED
|
@@ -3,44 +3,547 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { Atom, Registry } from '@effect-atom/atom-react';
|
|
6
|
-
import { effect } from '@preact/signals-core';
|
|
7
6
|
import * as Array from 'effect/Array';
|
|
7
|
+
import type * as Context from 'effect/Context';
|
|
8
|
+
import * as Effect from 'effect/Effect';
|
|
8
9
|
import * as Function from 'effect/Function';
|
|
9
10
|
import * as Option from 'effect/Option';
|
|
11
|
+
import * as Pipeable from 'effect/Pipeable';
|
|
10
12
|
import * as Record from 'effect/Record';
|
|
13
|
+
import type * as Schema from 'effect/Schema';
|
|
11
14
|
|
|
12
|
-
import { type CleanupFn, type
|
|
15
|
+
import { type CleanupFn, type Trigger } from '@dxos/async';
|
|
16
|
+
import { type Entity, type Type } from '@dxos/echo';
|
|
13
17
|
import { log } from '@dxos/log';
|
|
14
|
-
import { type MaybePromise, type Position, byPosition, getDebugName,
|
|
18
|
+
import { type MaybePromise, type Position, byPosition, getDebugName, isNonNullable } from '@dxos/util';
|
|
19
|
+
|
|
20
|
+
import { scheduleTask, yieldOrContinue } from '#scheduler';
|
|
21
|
+
|
|
22
|
+
import * as Graph from './graph';
|
|
23
|
+
import * as Node from './node';
|
|
24
|
+
import * as NodeMatcher from './node-matcher';
|
|
25
|
+
import {
|
|
26
|
+
getParentId,
|
|
27
|
+
nodeArgsUnchanged,
|
|
28
|
+
normalizeRelation,
|
|
29
|
+
primaryKey,
|
|
30
|
+
primaryParts,
|
|
31
|
+
qualifyId,
|
|
32
|
+
validateSegmentId,
|
|
33
|
+
} from './util';
|
|
15
34
|
|
|
16
|
-
|
|
17
|
-
|
|
35
|
+
//
|
|
36
|
+
// Extension Types
|
|
37
|
+
//
|
|
18
38
|
|
|
19
39
|
/**
|
|
20
40
|
* Graph builder extension for adding nodes to the graph based on a node id.
|
|
21
41
|
*/
|
|
22
|
-
export type ResolverExtension = (id: string) => Atom.Atom<NodeArg<any> | null>;
|
|
42
|
+
export type ResolverExtension = (id: string) => Atom.Atom<Node.NodeArg<any> | null>;
|
|
23
43
|
|
|
24
44
|
/**
|
|
25
45
|
* Graph builder extension for adding nodes to the graph based on a connection to an existing node.
|
|
26
46
|
*
|
|
27
47
|
* @param params.node The existing node the returned nodes will be connected to.
|
|
28
48
|
*/
|
|
29
|
-
export type ConnectorExtension = (node: Atom.Atom<Option.Option<Node>>) => Atom.Atom<NodeArg<any>[]>;
|
|
49
|
+
export type ConnectorExtension = (node: Atom.Atom<Option.Option<Node.Node>>) => Atom.Atom<Node.NodeArg<any>[]>;
|
|
30
50
|
|
|
31
51
|
/**
|
|
32
52
|
* Constrained case of the connector extension for more easily adding actions to the graph.
|
|
33
53
|
*/
|
|
34
54
|
export type ActionsExtension = (
|
|
35
|
-
node: Atom.Atom<Option.Option<Node>>,
|
|
36
|
-
) => Atom.Atom<Omit<NodeArg<ActionData
|
|
55
|
+
node: Atom.Atom<Option.Option<Node.Node>>,
|
|
56
|
+
) => Atom.Atom<Omit<Node.NodeArg<Node.ActionData<any>>, 'type' | 'nodes' | 'edges'>[]>;
|
|
37
57
|
|
|
38
58
|
/**
|
|
39
59
|
* Constrained case of the connector extension for more easily adding action groups to the graph.
|
|
40
60
|
*/
|
|
41
61
|
export type ActionGroupsExtension = (
|
|
42
|
-
node: Atom.Atom<Option.Option<Node>>,
|
|
43
|
-
) => Atom.Atom<Omit<NodeArg<typeof actionGroupSymbol>, 'type' | 'data' | 'nodes' | 'edges'>[]>;
|
|
62
|
+
node: Atom.Atom<Option.Option<Node.Node>>,
|
|
63
|
+
) => Atom.Atom<Omit<Node.NodeArg<typeof Node.actionGroupSymbol>, 'type' | 'data' | 'nodes' | 'edges'>[]>;
|
|
64
|
+
|
|
65
|
+
export type BuilderExtension = Readonly<{
|
|
66
|
+
id: string;
|
|
67
|
+
position: Position;
|
|
68
|
+
relation?: Node.RelationInput;
|
|
69
|
+
resolver?: ResolverExtension;
|
|
70
|
+
connector?: (node: Atom.Atom<Option.Option<Node.Node>>) => Atom.Atom<Node.NodeArg<any>[]>;
|
|
71
|
+
}>;
|
|
72
|
+
|
|
73
|
+
export type BuilderExtensions = BuilderExtension | BuilderExtension[] | BuilderExtensions[];
|
|
74
|
+
|
|
75
|
+
//
|
|
76
|
+
// GraphBuilder Core
|
|
77
|
+
//
|
|
78
|
+
|
|
79
|
+
export type GraphBuilderTraverseOptions = {
|
|
80
|
+
visitor: (node: Node.Node, path: string[]) => MaybePromise<boolean | void>;
|
|
81
|
+
registry?: Registry.Registry;
|
|
82
|
+
source?: string;
|
|
83
|
+
relation: Node.RelationInput | Node.RelationInput[];
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Identifier denoting a GraphBuilder.
|
|
88
|
+
*/
|
|
89
|
+
export const GraphBuilderTypeId: unique symbol = Symbol.for('@dxos/app-graph/GraphBuilder');
|
|
90
|
+
export type GraphBuilderTypeId = typeof GraphBuilderTypeId;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* GraphBuilder interface.
|
|
94
|
+
*/
|
|
95
|
+
export interface GraphBuilder extends Pipeable.Pipeable {
|
|
96
|
+
readonly [GraphBuilderTypeId]: GraphBuilderTypeId;
|
|
97
|
+
readonly graph: Graph.ExpandableGraph;
|
|
98
|
+
readonly extensions: Atom.Atom<Record<string, BuilderExtension>>;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The builder provides an extensible way to compose the construction of the graph.
|
|
103
|
+
* @internal
|
|
104
|
+
*/
|
|
105
|
+
// TODO(wittjosiah): Add api for setting subscription set and/or radius.
|
|
106
|
+
// Should unsubscribe from nodes that are not in the set/radius.
|
|
107
|
+
// Should track LRU nodes that are not in the set/radius and remove them beyond a certain threshold.
|
|
108
|
+
class GraphBuilderImpl implements GraphBuilder {
|
|
109
|
+
readonly [GraphBuilderTypeId]: GraphBuilderTypeId = GraphBuilderTypeId;
|
|
110
|
+
|
|
111
|
+
pipe() {
|
|
112
|
+
// eslint-disable-next-line prefer-rest-params
|
|
113
|
+
return Pipeable.pipeArguments(this, arguments);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// TODO(wittjosiah): Use Context.
|
|
117
|
+
/** Active subscriptions keyed by composite ID, cleaned up on node removal. */
|
|
118
|
+
readonly _subscriptions = new Map<string, CleanupFn>();
|
|
119
|
+
/** Connector updates pending flush, keyed by connector key. */
|
|
120
|
+
readonly _dirtyConnectors = new Map<
|
|
121
|
+
string,
|
|
122
|
+
{
|
|
123
|
+
nodes: Node.NodeArg<any>[];
|
|
124
|
+
previous: string[];
|
|
125
|
+
}
|
|
126
|
+
>();
|
|
127
|
+
/** Last-flushed node IDs per connector key, used for edge removal on update. */
|
|
128
|
+
readonly _connectorPrevious = new Map<string, string[]>();
|
|
129
|
+
/** All inline-descendant IDs per connector key, used to remove stale inline nodes on update. */
|
|
130
|
+
readonly _connectorPreviousInlineIds = new Map<string, string[]>();
|
|
131
|
+
/** Last-flushed node args per connector key, used for change detection. */
|
|
132
|
+
readonly _connectorPreviousArgs = new Map<string, Node.NodeArg<any>[]>();
|
|
133
|
+
/** Whether a dirty-flush task is already scheduled. */
|
|
134
|
+
_flushScheduled = false;
|
|
135
|
+
/** Resolves when the current flush completes. */
|
|
136
|
+
_flushPromise: Promise<void> = Promise.resolve();
|
|
137
|
+
/** Registered builder extensions keyed by extension ID. */
|
|
138
|
+
readonly _extensions = Atom.make(Record.empty<string, BuilderExtension>()).pipe(
|
|
139
|
+
Atom.keepAlive,
|
|
140
|
+
Atom.withLabel('graph-builder:extensions'),
|
|
141
|
+
);
|
|
142
|
+
/** Triggers signalling that a node's resolver has fired at least once. */
|
|
143
|
+
readonly _initialized: Record<string, Trigger> = {};
|
|
144
|
+
/** Shared atom registry for reactive subscriptions. */
|
|
145
|
+
readonly _registry: Registry.Registry;
|
|
146
|
+
/** Backing graph with internal accessors for node atoms and construction. */
|
|
147
|
+
readonly _graph: Graph.Graph & {
|
|
148
|
+
_node: (id: string) => Atom.Writable<Option.Option<Node.Node>>;
|
|
149
|
+
_constructNode: (node: Node.NodeArg<any>) => Option.Option<Node.Node>;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
constructor({ registry, ...params }: Pick<Graph.GraphProps, 'registry' | 'nodes' | 'edges'> = {}) {
|
|
153
|
+
this._registry = registry ?? Registry.make();
|
|
154
|
+
const graph = Graph.make({
|
|
155
|
+
...params,
|
|
156
|
+
registry: this._registry,
|
|
157
|
+
onExpand: (id, relation) => this._onExpand(id, relation),
|
|
158
|
+
onInitialize: (id) => this._onInitialize(id),
|
|
159
|
+
onRemoveNode: (id) => this._onRemoveNode(id),
|
|
160
|
+
});
|
|
161
|
+
// Access internal methods via type assertion since GraphBuilder needs them
|
|
162
|
+
this._graph = graph as Graph.Graph & {
|
|
163
|
+
_node: (id: string) => Atom.Writable<Option.Option<Node.Node>>;
|
|
164
|
+
_constructNode: (node: Node.NodeArg<any>) => Option.Option<Node.Node>;
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
get graph(): Graph.ExpandableGraph {
|
|
169
|
+
return this._graph;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
get extensions() {
|
|
173
|
+
return this._extensions;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Apply a set of node changes for a single connector key. */
|
|
177
|
+
private _applyConnectorUpdate(key: string, nodes: Node.NodeArg<any>[], previous: string[]): void {
|
|
178
|
+
const { id, relation } = relationFromConnectorKey(key);
|
|
179
|
+
const ids = nodes.map((node) => node.id);
|
|
180
|
+
const removed = previous.filter((pid) => !ids.includes(pid));
|
|
181
|
+
this._connectorPrevious.set(key, ids);
|
|
182
|
+
this._connectorPreviousArgs.set(key, nodes);
|
|
183
|
+
|
|
184
|
+
const currentInlineIds = collectAllInlineIds(nodes);
|
|
185
|
+
const previousInlineIds = this._connectorPreviousInlineIds.get(key) ?? [];
|
|
186
|
+
const staleInlineIds = previousInlineIds.filter((pid) => !currentInlineIds.includes(pid));
|
|
187
|
+
this._connectorPreviousInlineIds.set(key, currentInlineIds);
|
|
188
|
+
|
|
189
|
+
Graph.removeNodes(this._graph, staleInlineIds, true);
|
|
190
|
+
Graph.removeEdges(
|
|
191
|
+
this._graph,
|
|
192
|
+
removed.map((target) => ({ source: id, target, relation })),
|
|
193
|
+
true,
|
|
194
|
+
);
|
|
195
|
+
Graph.addNodes(this._graph, nodes);
|
|
196
|
+
Graph.addEdges(
|
|
197
|
+
this._graph,
|
|
198
|
+
nodes.map((node) => ({ source: id, target: node.id, relation })),
|
|
199
|
+
);
|
|
200
|
+
if (ids.length > 0) {
|
|
201
|
+
const sortedIds = [...nodes]
|
|
202
|
+
.sort((a, b) =>
|
|
203
|
+
byPosition(a.properties ?? ({} as { position?: Position }), b.properties ?? ({} as { position?: Position })),
|
|
204
|
+
)
|
|
205
|
+
.map((n) => n.id);
|
|
206
|
+
Graph.sortEdges(this._graph, id, relation, sortedIds);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private _scheduleDirtyFlush(): void {
|
|
211
|
+
if (!this._flushScheduled) {
|
|
212
|
+
this._flushScheduled = true;
|
|
213
|
+
this._flushPromise = scheduleTask(
|
|
214
|
+
() => {
|
|
215
|
+
this._flushScheduled = false;
|
|
216
|
+
while (this._dirtyConnectors.size > 0) {
|
|
217
|
+
const entries = [...this._dirtyConnectors.entries()];
|
|
218
|
+
this._dirtyConnectors.clear();
|
|
219
|
+
|
|
220
|
+
Atom.batch(() => {
|
|
221
|
+
for (const [key, { nodes, previous }] of entries) {
|
|
222
|
+
this._applyConnectorUpdate(key, nodes, previous);
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
{ strategy: 'smooth' },
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private readonly _resolvers = Atom.family<string, Atom.Atom<Option.Option<Node.NodeArg<any>>>>((id) => {
|
|
233
|
+
return Atom.make((get) => {
|
|
234
|
+
return Function.pipe(
|
|
235
|
+
get(this._extensions),
|
|
236
|
+
Record.values,
|
|
237
|
+
Array.sortBy(byPosition),
|
|
238
|
+
Array.map(({ resolver }) => resolver),
|
|
239
|
+
Array.filter(isNonNullable),
|
|
240
|
+
Array.map((resolver) => get(resolver(id))),
|
|
241
|
+
Array.filter(isNonNullable),
|
|
242
|
+
Array.head,
|
|
243
|
+
);
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
private readonly _connectors = Atom.family<string, Atom.Atom<Node.NodeArg<any>[]>>((key) => {
|
|
248
|
+
return Atom.make((get) => {
|
|
249
|
+
const { id, relation } = relationFromConnectorKey(key);
|
|
250
|
+
const node = this._graph.node(id);
|
|
251
|
+
|
|
252
|
+
const sourceNode = Option.getOrElse(get(node), () => undefined);
|
|
253
|
+
if (!sourceNode) {
|
|
254
|
+
return [];
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const extensions = Function.pipe(
|
|
258
|
+
get(this._extensions),
|
|
259
|
+
Record.values,
|
|
260
|
+
Array.sortBy(byPosition),
|
|
261
|
+
Array.filter(
|
|
262
|
+
(ext): ext is BuilderExtension & { connector: NonNullable<BuilderExtension['connector']> } =>
|
|
263
|
+
Graph.relationKey(ext.relation ?? 'child') === Graph.relationKey(relation) && ext.connector != null,
|
|
264
|
+
),
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
const nodes: Node.NodeArg<any>[] = [];
|
|
268
|
+
for (const ext of extensions) {
|
|
269
|
+
const result = get(ext.connector(node));
|
|
270
|
+
nodes.push(...result);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return nodes;
|
|
274
|
+
}).pipe(Atom.withLabel(`graph-builder:connectors:${key}`));
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
private _onExpand(id: string, relation: Node.Relation): void {
|
|
278
|
+
log('onExpand', { id, relation, registry: getDebugName(this._registry) });
|
|
279
|
+
this._expandRelation(id, relation);
|
|
280
|
+
|
|
281
|
+
// TODO(wittjosiah): Remove. This is for backwards compatibility.
|
|
282
|
+
if (relation.kind === 'child' && relation.direction === 'outbound') {
|
|
283
|
+
Graph.expand(this._graph, id, 'action');
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
private _expandRelation(id: string, relation: Node.RelationInput): void {
|
|
288
|
+
const key = connectorKey(id, relation);
|
|
289
|
+
const connectors = this._connectors(key);
|
|
290
|
+
|
|
291
|
+
const cancel = this._registry.subscribe(
|
|
292
|
+
connectors,
|
|
293
|
+
(rawNodes) => {
|
|
294
|
+
const nodes = qualifyNodeArgs(id)(rawNodes);
|
|
295
|
+
const previous = this._connectorPrevious.get(key) ?? [];
|
|
296
|
+
const ids = nodes.map((n) => n.id);
|
|
297
|
+
|
|
298
|
+
if (ids.length === previous.length && ids.every((nodeId, idx) => nodeId === previous[idx])) {
|
|
299
|
+
const prevArgs = this._connectorPreviousArgs.get(key);
|
|
300
|
+
if (prevArgs && nodeArgsUnchanged(prevArgs, nodes)) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
log('update', { id, relation, ids });
|
|
306
|
+
this._dirtyConnectors.set(key, { nodes, previous });
|
|
307
|
+
this._scheduleDirtyFlush();
|
|
308
|
+
},
|
|
309
|
+
{ immediate: true },
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
this._subscriptions.set(subscriptionKey(id, 'expand', key), cancel);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
private async _onInitialize(id: string) {
|
|
316
|
+
log('onInitialize', { id });
|
|
317
|
+
const resolver = this._resolvers(id);
|
|
318
|
+
|
|
319
|
+
const cancel = this._registry.subscribe(
|
|
320
|
+
resolver,
|
|
321
|
+
(node) => {
|
|
322
|
+
const trigger = this._initialized[id];
|
|
323
|
+
const connectorOwned = [...this._connectorPrevious.values()].some((ids) => ids.includes(id));
|
|
324
|
+
Option.match(node, {
|
|
325
|
+
onSome: (node) => {
|
|
326
|
+
if (!connectorOwned) {
|
|
327
|
+
Graph.addNodes(this._graph, [node]);
|
|
328
|
+
// Connect resolved node to its parent via a child edge.
|
|
329
|
+
const parentId = getParentId(id);
|
|
330
|
+
if (parentId) {
|
|
331
|
+
Graph.addEdges(this._graph, [{ source: parentId, target: id, relation: 'child' }]);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
trigger?.wake();
|
|
335
|
+
},
|
|
336
|
+
onNone: () => {
|
|
337
|
+
trigger?.wake();
|
|
338
|
+
if (!connectorOwned) {
|
|
339
|
+
Graph.removeNodes(this._graph, [id]);
|
|
340
|
+
}
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
},
|
|
344
|
+
{ immediate: true },
|
|
345
|
+
);
|
|
346
|
+
|
|
347
|
+
this._subscriptions.set(subscriptionKey(id, 'init'), cancel);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private _onRemoveNode(id: string): void {
|
|
351
|
+
for (const [key, cleanup] of this._subscriptions) {
|
|
352
|
+
if (primaryParts(key)[0] === id) {
|
|
353
|
+
cleanup();
|
|
354
|
+
this._subscriptions.delete(key);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Creates a new GraphBuilder instance.
|
|
362
|
+
*/
|
|
363
|
+
export const make = (params?: Pick<Graph.GraphProps, 'registry' | 'nodes' | 'edges'>): GraphBuilder => {
|
|
364
|
+
return new GraphBuilderImpl(params);
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Creates a GraphBuilder from a serialized pickle string.
|
|
369
|
+
*/
|
|
370
|
+
export const from = (pickle?: string, registry?: Registry.Registry): GraphBuilder => {
|
|
371
|
+
if (!pickle) {
|
|
372
|
+
return make({ registry });
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const { nodes, edges } = JSON.parse(pickle);
|
|
376
|
+
return make({ nodes, edges, registry });
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Implementation helper for addExtension.
|
|
381
|
+
*/
|
|
382
|
+
const addExtensionImpl = (builder: GraphBuilder, extensions: BuilderExtensions): GraphBuilder => {
|
|
383
|
+
const internal = builder as GraphBuilderImpl;
|
|
384
|
+
flattenExtensions(extensions).forEach((extension) => {
|
|
385
|
+
const extensions = internal._registry.get(internal._extensions);
|
|
386
|
+
internal._registry.set(internal._extensions, Record.set(extensions, extension.id, extension));
|
|
387
|
+
});
|
|
388
|
+
return builder;
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Add extensions to the graph builder.
|
|
393
|
+
*/
|
|
394
|
+
export function addExtension(builder: GraphBuilder, extensions: BuilderExtensions): GraphBuilder;
|
|
395
|
+
export function addExtension(extensions: BuilderExtensions): (builder: GraphBuilder) => GraphBuilder;
|
|
396
|
+
export function addExtension(
|
|
397
|
+
builderOrExtensions: GraphBuilder | BuilderExtensions,
|
|
398
|
+
extensions?: BuilderExtensions,
|
|
399
|
+
): GraphBuilder | ((builder: GraphBuilder) => GraphBuilder) {
|
|
400
|
+
if (extensions === undefined) {
|
|
401
|
+
// Curried: addExtension(extensions)
|
|
402
|
+
const extensions = builderOrExtensions as BuilderExtensions;
|
|
403
|
+
return (builder: GraphBuilder) => addExtensionImpl(builder, extensions);
|
|
404
|
+
} else {
|
|
405
|
+
// Direct: addExtension(builder, extensions)
|
|
406
|
+
const builder = builderOrExtensions as GraphBuilder;
|
|
407
|
+
return addExtensionImpl(builder, extensions);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Implementation helper for removeExtension.
|
|
413
|
+
*/
|
|
414
|
+
const removeExtensionImpl = (builder: GraphBuilder, id: string): GraphBuilder => {
|
|
415
|
+
const internal = builder as GraphBuilderImpl;
|
|
416
|
+
const extensions = internal._registry.get(internal._extensions);
|
|
417
|
+
internal._registry.set(internal._extensions, Record.remove(extensions, id));
|
|
418
|
+
return builder;
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Remove an extension from the graph builder.
|
|
423
|
+
*/
|
|
424
|
+
export function removeExtension(builder: GraphBuilder, id: string): GraphBuilder;
|
|
425
|
+
export function removeExtension(id: string): (builder: GraphBuilder) => GraphBuilder;
|
|
426
|
+
export function removeExtension(
|
|
427
|
+
builderOrId: GraphBuilder | string,
|
|
428
|
+
id?: string,
|
|
429
|
+
): GraphBuilder | ((builder: GraphBuilder) => GraphBuilder) {
|
|
430
|
+
if (typeof builderOrId === 'string') {
|
|
431
|
+
// Curried: removeExtension(id)
|
|
432
|
+
const id = builderOrId;
|
|
433
|
+
return (builder: GraphBuilder) => removeExtensionImpl(builder, id);
|
|
434
|
+
} else {
|
|
435
|
+
// Direct: removeExtension(builder, id)
|
|
436
|
+
const builder = builderOrId;
|
|
437
|
+
return removeExtensionImpl(builder, id!);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Implementation helper for explore.
|
|
443
|
+
*/
|
|
444
|
+
const exploreImpl = async (
|
|
445
|
+
builder: GraphBuilder,
|
|
446
|
+
options: GraphBuilderTraverseOptions,
|
|
447
|
+
path: string[] = [],
|
|
448
|
+
): Promise<void> => {
|
|
449
|
+
const internal = builder as GraphBuilderImpl;
|
|
450
|
+
const { registry = Registry.make(), source = Node.RootId, relation, visitor } = options;
|
|
451
|
+
// Break cycles.
|
|
452
|
+
if (path.includes(source)) {
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
await yieldOrContinue('idle');
|
|
457
|
+
|
|
458
|
+
const node = registry.get(internal._graph.nodeOrThrow(source));
|
|
459
|
+
const shouldContinue = await visitor(node, [...path, node.id]);
|
|
460
|
+
if (shouldContinue === false) {
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const nodes = Function.pipe(
|
|
465
|
+
internal._registry.get(internal._extensions),
|
|
466
|
+
Record.values,
|
|
467
|
+
Array.map((extension) => extension.connector),
|
|
468
|
+
Array.filter(isNonNullable),
|
|
469
|
+
Array.flatMap((connector) => registry.get(connector(internal._graph.node(source)))),
|
|
470
|
+
qualifyNodeArgs(source),
|
|
471
|
+
);
|
|
472
|
+
|
|
473
|
+
await Promise.all(
|
|
474
|
+
nodes.map((nodeArg) => {
|
|
475
|
+
registry.set(internal._graph._node(nodeArg.id), internal._graph._constructNode(nodeArg));
|
|
476
|
+
return exploreImpl(builder, { registry, source: nodeArg.id, relation, visitor }, [...path, node.id]);
|
|
477
|
+
}),
|
|
478
|
+
);
|
|
479
|
+
|
|
480
|
+
if (registry !== internal._registry) {
|
|
481
|
+
registry.reset();
|
|
482
|
+
registry.dispose();
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* Explore the graph by traversing it with the given options.
|
|
488
|
+
*/
|
|
489
|
+
export function explore(builder: GraphBuilder, options: GraphBuilderTraverseOptions, path?: string[]): Promise<void>;
|
|
490
|
+
export function explore(
|
|
491
|
+
options: GraphBuilderTraverseOptions,
|
|
492
|
+
path?: string[],
|
|
493
|
+
): (builder: GraphBuilder) => Promise<void>;
|
|
494
|
+
export function explore(
|
|
495
|
+
builderOrOptions: GraphBuilder | GraphBuilderTraverseOptions,
|
|
496
|
+
optionsOrPath?: GraphBuilderTraverseOptions | string[],
|
|
497
|
+
path?: string[],
|
|
498
|
+
): Promise<void> | ((builder: GraphBuilder) => Promise<void>) {
|
|
499
|
+
if (typeof builderOrOptions === 'object' && 'visitor' in builderOrOptions) {
|
|
500
|
+
// Curried: explore(options, path?)
|
|
501
|
+
const options = builderOrOptions as GraphBuilderTraverseOptions;
|
|
502
|
+
const path = Array.isArray(optionsOrPath) ? optionsOrPath : undefined;
|
|
503
|
+
return (builder: GraphBuilder) => exploreImpl(builder, options, path);
|
|
504
|
+
} else {
|
|
505
|
+
// Direct: explore(builder, options, path?)
|
|
506
|
+
const builder = builderOrOptions as GraphBuilder;
|
|
507
|
+
const options = optionsOrPath as GraphBuilderTraverseOptions;
|
|
508
|
+
const pathArg = path ?? (Array.isArray(optionsOrPath) ? optionsOrPath : undefined);
|
|
509
|
+
return exploreImpl(builder, options, pathArg);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Implementation helper for destroy.
|
|
515
|
+
*/
|
|
516
|
+
const destroyImpl = (builder: GraphBuilder): void => {
|
|
517
|
+
const internal = builder as GraphBuilderImpl;
|
|
518
|
+
internal._subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
519
|
+
internal._subscriptions.clear();
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* Destroy the graph builder and clean up resources.
|
|
524
|
+
*/
|
|
525
|
+
export function destroy(builder: GraphBuilder): void;
|
|
526
|
+
export function destroy(): (builder: GraphBuilder) => void;
|
|
527
|
+
export function destroy(builder?: GraphBuilder): void | ((builder: GraphBuilder) => void) {
|
|
528
|
+
if (builder === undefined) {
|
|
529
|
+
// Curried: destroy()
|
|
530
|
+
return (builder: GraphBuilder) => destroyImpl(builder);
|
|
531
|
+
} else {
|
|
532
|
+
// Direct: destroy(builder)
|
|
533
|
+
return destroyImpl(builder);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Wait for all pending connector updates to be flushed.
|
|
539
|
+
*/
|
|
540
|
+
export const flush = (builder: GraphBuilder): Promise<void> => {
|
|
541
|
+
return (builder as GraphBuilderImpl)._flushPromise;
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
//
|
|
545
|
+
// Extension Creation
|
|
546
|
+
//
|
|
44
547
|
|
|
45
548
|
/**
|
|
46
549
|
* A graph builder extension is used to add nodes to the graph.
|
|
@@ -53,9 +556,9 @@ export type ActionGroupsExtension = (
|
|
|
53
556
|
* @param params.actions A function to add actions to the graph based on a connection to an existing node.
|
|
54
557
|
* @param params.actionGroups A function to add action groups to the graph based on a connection to an existing node.
|
|
55
558
|
*/
|
|
56
|
-
export type
|
|
559
|
+
export type CreateExtensionRawOptions = {
|
|
57
560
|
id: string;
|
|
58
|
-
relation?:
|
|
561
|
+
relation?: Node.RelationInput;
|
|
59
562
|
position?: Position;
|
|
60
563
|
resolver?: ResolverExtension;
|
|
61
564
|
connector?: ConnectorExtension;
|
|
@@ -64,18 +567,19 @@ export type CreateExtensionOptions = {
|
|
|
64
567
|
};
|
|
65
568
|
|
|
66
569
|
/**
|
|
67
|
-
* Create a graph builder extension.
|
|
570
|
+
* Create a graph builder extension (low-level API that works directly with Atoms).
|
|
68
571
|
*/
|
|
69
|
-
export const
|
|
572
|
+
export const createExtensionRaw = (extension: CreateExtensionRawOptions): BuilderExtension[] => {
|
|
70
573
|
const {
|
|
71
574
|
id,
|
|
72
575
|
position = 'static',
|
|
73
|
-
relation = '
|
|
576
|
+
relation = 'child',
|
|
74
577
|
resolver: _resolver,
|
|
75
578
|
connector: _connector,
|
|
76
579
|
actions: _actions,
|
|
77
580
|
actionGroups: _actionGroups,
|
|
78
581
|
} = extension;
|
|
582
|
+
const normalizedRelation = normalizeRelation(relation);
|
|
79
583
|
const getId = (key: string) => `${id}/${key}`;
|
|
80
584
|
|
|
81
585
|
const resolver =
|
|
@@ -83,19 +587,19 @@ export const createExtension = (extension: CreateExtensionOptions): BuilderExten
|
|
|
83
587
|
|
|
84
588
|
const connector =
|
|
85
589
|
_connector &&
|
|
86
|
-
Atom.family((node: Atom.Atom<Option.Option<Node>>) =>
|
|
590
|
+
Atom.family((node: Atom.Atom<Option.Option<Node.Node>>) =>
|
|
87
591
|
_connector(node).pipe(Atom.withLabel(`graph-builder:_connector:${id}`)),
|
|
88
592
|
);
|
|
89
593
|
|
|
90
594
|
const actionGroups =
|
|
91
595
|
_actionGroups &&
|
|
92
|
-
Atom.family((node: Atom.Atom<Option.Option<Node>>) =>
|
|
596
|
+
Atom.family((node: Atom.Atom<Option.Option<Node.Node>>) =>
|
|
93
597
|
_actionGroups(node).pipe(Atom.withLabel(`graph-builder:_actionGroups:${id}`)),
|
|
94
598
|
);
|
|
95
599
|
|
|
96
600
|
const actions =
|
|
97
601
|
_actions &&
|
|
98
|
-
Atom.family((node: Atom.Atom<Option.Option<Node>>) =>
|
|
602
|
+
Atom.family((node: Atom.Atom<Option.Option<Node.Node>>) =>
|
|
99
603
|
_actions(node).pipe(Atom.withLabel(`graph-builder:_actions:${id}`)),
|
|
100
604
|
);
|
|
101
605
|
|
|
@@ -105,13 +609,13 @@ export const createExtension = (extension: CreateExtensionOptions): BuilderExten
|
|
|
105
609
|
? ({
|
|
106
610
|
id: getId('connector'),
|
|
107
611
|
position,
|
|
108
|
-
relation,
|
|
612
|
+
relation: normalizedRelation,
|
|
109
613
|
connector: Atom.family((node) =>
|
|
110
614
|
Atom.make((get) => {
|
|
111
615
|
try {
|
|
112
616
|
return get(connector(node));
|
|
113
|
-
} catch {
|
|
114
|
-
log.warn('Error in connector', { id: getId('connector'), node });
|
|
617
|
+
} catch (error) {
|
|
618
|
+
log.warn('Error in connector', { id: getId('connector'), node, error });
|
|
115
619
|
return [];
|
|
116
620
|
}
|
|
117
621
|
}).pipe(Atom.withLabel(`graph-builder:connector:${id}`)),
|
|
@@ -122,17 +626,17 @@ export const createExtension = (extension: CreateExtensionOptions): BuilderExten
|
|
|
122
626
|
? ({
|
|
123
627
|
id: getId('actionGroups'),
|
|
124
628
|
position,
|
|
125
|
-
relation:
|
|
629
|
+
relation: Node.actionRelation(),
|
|
126
630
|
connector: Atom.family((node) =>
|
|
127
631
|
Atom.make((get) => {
|
|
128
632
|
try {
|
|
129
633
|
return get(actionGroups(node)).map((arg) => ({
|
|
130
634
|
...arg,
|
|
131
|
-
data: actionGroupSymbol,
|
|
132
|
-
type:
|
|
635
|
+
data: Node.actionGroupSymbol,
|
|
636
|
+
type: Node.ActionGroupType,
|
|
133
637
|
}));
|
|
134
|
-
} catch {
|
|
135
|
-
log.warn('Error in actionGroups', { id: getId('actionGroups'), node });
|
|
638
|
+
} catch (error) {
|
|
639
|
+
log.warn('Error in actionGroups', { id: getId('actionGroups'), node, error });
|
|
136
640
|
return [];
|
|
137
641
|
}
|
|
138
642
|
}).pipe(Atom.withLabel(`graph-builder:connector:actionGroups:${id}`)),
|
|
@@ -143,13 +647,13 @@ export const createExtension = (extension: CreateExtensionOptions): BuilderExten
|
|
|
143
647
|
? ({
|
|
144
648
|
id: getId('actions'),
|
|
145
649
|
position,
|
|
146
|
-
relation:
|
|
650
|
+
relation: Node.actionRelation(),
|
|
147
651
|
connector: Atom.family((node) =>
|
|
148
652
|
Atom.make((get) => {
|
|
149
653
|
try {
|
|
150
|
-
return get(actions(node)).map((arg) => ({ ...arg, type:
|
|
151
|
-
} catch {
|
|
152
|
-
log.warn('Error in actions', { id: getId('actions'), node });
|
|
654
|
+
return get(actions(node)).map((arg) => ({ ...arg, type: Node.ActionType }));
|
|
655
|
+
} catch (error) {
|
|
656
|
+
log.warn('Error in actions', { id: getId('actions'), node, error });
|
|
153
657
|
return [];
|
|
154
658
|
}
|
|
155
659
|
}).pipe(Atom.withLabel(`graph-builder:connector:actions:${id}`)),
|
|
@@ -159,281 +663,217 @@ export const createExtension = (extension: CreateExtensionOptions): BuilderExten
|
|
|
159
663
|
].filter(isNonNullable);
|
|
160
664
|
};
|
|
161
665
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
export type BuilderExtension = Readonly<{
|
|
666
|
+
/**
|
|
667
|
+
* Options for creating a graph builder extension with simplified API.
|
|
668
|
+
* All callbacks must return Effects for dependency injection.
|
|
669
|
+
* Effects may fail - errors are caught, logged, and the extension returns empty results.
|
|
670
|
+
*/
|
|
671
|
+
export type CreateExtensionOptions<TMatched = Node.Node, R = never> = {
|
|
170
672
|
id: string;
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
if (Array.isArray(extension)) {
|
|
181
|
-
return [...acc, ...extension.flatMap((ext) => flattenExtensions(ext, acc))];
|
|
182
|
-
} else {
|
|
183
|
-
return [...acc, extension];
|
|
184
|
-
}
|
|
673
|
+
match: (node: Node.Node) => Option.Option<TMatched>;
|
|
674
|
+
actions?: (
|
|
675
|
+
matched: TMatched,
|
|
676
|
+
get: Atom.Context,
|
|
677
|
+
) => Effect.Effect<Omit<Node.NodeArg<Node.ActionData<any>, any>, 'type'>[], Error, R>;
|
|
678
|
+
connector?: (matched: TMatched, get: Atom.Context) => Effect.Effect<Node.NodeArg<any, any>[], Error, R>;
|
|
679
|
+
resolver?: (id: string, get: Atom.Context) => Effect.Effect<Node.NodeArg<any, any> | null, Error, R>;
|
|
680
|
+
relation?: Node.RelationInput;
|
|
681
|
+
position?: Position;
|
|
185
682
|
};
|
|
186
683
|
|
|
187
684
|
/**
|
|
188
|
-
*
|
|
685
|
+
* Run an Effect synchronously with the provided context.
|
|
686
|
+
* If the effect fails, logs the error and returns the fallback value.
|
|
687
|
+
* @internal
|
|
189
688
|
*/
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
private readonly _graph: Graph;
|
|
203
|
-
|
|
204
|
-
constructor({ registry, ...params }: Pick<GraphParams, 'registry' | 'nodes' | 'edges'> = {}) {
|
|
205
|
-
this._registry = registry ?? Registry.make();
|
|
206
|
-
this._graph = new Graph({
|
|
207
|
-
...params,
|
|
208
|
-
registry: this._registry,
|
|
209
|
-
onExpand: (id, relation) => this._onExpand(id, relation),
|
|
210
|
-
onInitialize: (id) => this._onInitialize(id),
|
|
211
|
-
onRemoveNode: (id) => this._onRemoveNode(id),
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
static from(pickle?: string, registry?: Registry.Registry): GraphBuilder {
|
|
216
|
-
if (!pickle) {
|
|
217
|
-
return new GraphBuilder({ registry });
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
const { nodes, edges } = JSON.parse(pickle);
|
|
221
|
-
return new GraphBuilder({ nodes, edges, registry });
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
get graph(): ExpandableGraph {
|
|
225
|
-
return this._graph;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
get extensions() {
|
|
229
|
-
return this._extensions;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
addExtension(extensions: BuilderExtensions): GraphBuilder {
|
|
233
|
-
flattenExtensions(extensions).forEach((extension) => {
|
|
234
|
-
const extensions = this._registry.get(this._extensions);
|
|
235
|
-
this._registry.set(this._extensions, Record.set(extensions, extension.id, extension));
|
|
236
|
-
});
|
|
237
|
-
return this;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
removeExtension(id: string): GraphBuilder {
|
|
241
|
-
const extensions = this._registry.get(this._extensions);
|
|
242
|
-
this._registry.set(this._extensions, Record.remove(extensions, id));
|
|
243
|
-
return this;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
async explore(
|
|
247
|
-
// TODO(wittjosiah): Currently defaulting to new registry.
|
|
248
|
-
// Currently unsure about how to handle nodes which are expanded in the background.
|
|
249
|
-
// This seems like a good place to start.
|
|
250
|
-
{ registry = Registry.make(), source = ROOT_ID, relation = 'outbound', visitor }: GraphBuilderTraverseOptions,
|
|
251
|
-
path: string[] = [],
|
|
252
|
-
): Promise<void> {
|
|
253
|
-
// Break cycles.
|
|
254
|
-
if (path.includes(source)) {
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
// TODO(wittjosiah): This is a workaround for esm not working in the test runner.
|
|
259
|
-
// Switching to vitest is blocked by having node esm versions of echo-schema & echo-signals.
|
|
260
|
-
if (!isNode()) {
|
|
261
|
-
const { yieldOrContinue } = await import('main-thread-scheduling');
|
|
262
|
-
await yieldOrContinue('idle');
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
const node = registry.get(this._graph.nodeOrThrow(source));
|
|
266
|
-
const shouldContinue = await visitor(node, [...path, node.id]);
|
|
267
|
-
if (shouldContinue === false) {
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
const nodes = Object.values(this._registry.get(this._extensions))
|
|
272
|
-
.filter((extension) => relation === (extension.relation ?? 'outbound'))
|
|
273
|
-
.map((extension) => extension.connector)
|
|
274
|
-
.filter(isNonNullable)
|
|
275
|
-
.flatMap((connector) => registry.get(connector(this._graph.node(source))));
|
|
276
|
-
|
|
277
|
-
await Promise.all(
|
|
278
|
-
nodes.map((nodeArg) => {
|
|
279
|
-
registry.set(this._graph._node(nodeArg.id), this._graph._constructNode(nodeArg));
|
|
280
|
-
return this.explore({ registry, source: nodeArg.id, relation, visitor }, [...path, node.id]);
|
|
689
|
+
const runEffectSyncWithFallback = <T, R>(
|
|
690
|
+
effect: Effect.Effect<T, Error, R>,
|
|
691
|
+
context: Context.Context<R>,
|
|
692
|
+
extensionId: string,
|
|
693
|
+
fallback: T,
|
|
694
|
+
): T => {
|
|
695
|
+
return Effect.runSync(
|
|
696
|
+
effect.pipe(
|
|
697
|
+
Effect.provide(context),
|
|
698
|
+
Effect.catchAll((error) => {
|
|
699
|
+
log.warn('Extension failed', { extension: extensionId, error });
|
|
700
|
+
return Effect.succeed(fallback);
|
|
281
701
|
}),
|
|
282
|
-
)
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
registry.reset();
|
|
286
|
-
registry.dispose();
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
destroy(): void {
|
|
291
|
-
this._subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
292
|
-
this._subscriptions.clear();
|
|
293
|
-
}
|
|
702
|
+
),
|
|
703
|
+
);
|
|
704
|
+
};
|
|
294
705
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
706
|
+
/**
|
|
707
|
+
* Create a graph builder extension with simplified API.
|
|
708
|
+
* Returns an Effect to allow callbacks to access services via dependency injection.
|
|
709
|
+
*/
|
|
710
|
+
export const createExtension = <TMatched = Node.Node, R = never>(
|
|
711
|
+
options: CreateExtensionOptions<TMatched, R>,
|
|
712
|
+
): Effect.Effect<BuilderExtension[], never, R> =>
|
|
713
|
+
Effect.map(Effect.context<R>(), (context) => {
|
|
714
|
+
const { id, match, actions, connector, resolver, relation, position } = options;
|
|
715
|
+
|
|
716
|
+
const connectorExtension = connector ? createConnectorWithRuntime(id, match, connector, context) : undefined;
|
|
717
|
+
|
|
718
|
+
const actionsExtension = actions
|
|
719
|
+
? (node: Atom.Atom<Option.Option<Node.Node>>) =>
|
|
720
|
+
Atom.make((get) =>
|
|
721
|
+
Function.pipe(
|
|
722
|
+
get(node),
|
|
723
|
+
Option.flatMap(match),
|
|
724
|
+
Option.map((matched) =>
|
|
725
|
+
runEffectSyncWithFallback(actions(matched, get), context, id, []).map((action) => ({
|
|
726
|
+
...action,
|
|
727
|
+
// Attach captured context for action execution.
|
|
728
|
+
_actionContext: context,
|
|
729
|
+
})),
|
|
730
|
+
),
|
|
731
|
+
Option.getOrElse(() => []),
|
|
732
|
+
),
|
|
733
|
+
)
|
|
734
|
+
: undefined;
|
|
735
|
+
|
|
736
|
+
const resolverExtension = resolver
|
|
737
|
+
? (nodeId: string) =>
|
|
738
|
+
Atom.make((get) => runEffectSyncWithFallback(resolver(nodeId, get), context, id, null) ?? null)
|
|
739
|
+
: undefined;
|
|
740
|
+
|
|
741
|
+
return createExtensionRaw({
|
|
742
|
+
id,
|
|
743
|
+
relation,
|
|
744
|
+
position,
|
|
745
|
+
connector: connectorExtension,
|
|
746
|
+
actions: actionsExtension,
|
|
747
|
+
resolver: resolverExtension,
|
|
307
748
|
});
|
|
308
749
|
});
|
|
309
750
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
});
|
|
327
|
-
|
|
328
|
-
private _onExpand(id: string, relation: Relation): void {
|
|
329
|
-
log('onExpand', { id, relation, registry: getDebugName(this._registry) });
|
|
330
|
-
const connectors = this._connectors(`${id}+${relation}`);
|
|
331
|
-
|
|
332
|
-
let previous: string[] = [];
|
|
333
|
-
const cancel = this._registry.subscribe(
|
|
334
|
-
connectors,
|
|
335
|
-
(nodes) => {
|
|
336
|
-
const ids = nodes.map((n) => n.id);
|
|
337
|
-
const removed = previous.filter((id) => !ids.includes(id));
|
|
338
|
-
previous = ids;
|
|
339
|
-
|
|
340
|
-
log('update', { id, relation, ids, removed });
|
|
341
|
-
const update = () => {
|
|
342
|
-
Atom.batch(() => {
|
|
343
|
-
this._graph.removeEdges(
|
|
344
|
-
removed.map((target) => ({ source: id, target })),
|
|
345
|
-
true,
|
|
346
|
-
);
|
|
347
|
-
this._graph.addNodes(nodes);
|
|
348
|
-
this._graph.addEdges(
|
|
349
|
-
nodes.map((node) =>
|
|
350
|
-
relation === 'outbound' ? { source: id, target: node.id } : { source: node.id, target: id },
|
|
351
|
-
),
|
|
352
|
-
);
|
|
353
|
-
this._graph.sortEdges(
|
|
354
|
-
id,
|
|
355
|
-
relation,
|
|
356
|
-
nodes.map(({ id }) => id),
|
|
357
|
-
);
|
|
358
|
-
});
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
// TODO(wittjosiah): Remove `requestAnimationFrame` once we have a better solution.
|
|
362
|
-
// This is a workaround to avoid a race condition where the graph is updated during React render.
|
|
363
|
-
if (typeof requestAnimationFrame === 'function') {
|
|
364
|
-
requestAnimationFrame(update);
|
|
365
|
-
} else {
|
|
366
|
-
update();
|
|
367
|
-
}
|
|
368
|
-
},
|
|
369
|
-
{ immediate: true },
|
|
751
|
+
/**
|
|
752
|
+
* Create a connector extension from a matcher and factory function.
|
|
753
|
+
* The factory's data type is inferred from the matcher's return type.
|
|
754
|
+
*/
|
|
755
|
+
export const createConnector = <TData>(
|
|
756
|
+
matcher: (node: Node.Node) => Option.Option<TData>,
|
|
757
|
+
factory: (data: TData, get: Atom.Context) => Node.NodeArg<any>[],
|
|
758
|
+
): ConnectorExtension => {
|
|
759
|
+
return (node: Atom.Atom<Option.Option<Node.Node>>) =>
|
|
760
|
+
Atom.make((get) =>
|
|
761
|
+
Function.pipe(
|
|
762
|
+
get(node),
|
|
763
|
+
Option.flatMap(matcher),
|
|
764
|
+
Option.map((data) => factory(data, get)),
|
|
765
|
+
Option.getOrElse(() => []),
|
|
766
|
+
),
|
|
370
767
|
);
|
|
768
|
+
};
|
|
371
769
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
this._graph.removeNodes([id]);
|
|
392
|
-
},
|
|
393
|
-
});
|
|
394
|
-
},
|
|
395
|
-
{ immediate: true },
|
|
770
|
+
/**
|
|
771
|
+
* Create a connector extension from a matcher and factory function with Effect support.
|
|
772
|
+
* The factory must return an Effect. Errors are caught and logged.
|
|
773
|
+
* @internal
|
|
774
|
+
*/
|
|
775
|
+
const createConnectorWithRuntime = <TData, R>(
|
|
776
|
+
extensionId: string,
|
|
777
|
+
matcher: (node: Node.Node) => Option.Option<TData>,
|
|
778
|
+
factory: (data: TData, get: Atom.Context) => Effect.Effect<Node.NodeArg<any>[], Error, R>,
|
|
779
|
+
context: Context.Context<R>,
|
|
780
|
+
): ConnectorExtension => {
|
|
781
|
+
return (node: Atom.Atom<Option.Option<Node.Node>>) =>
|
|
782
|
+
Atom.make((get) =>
|
|
783
|
+
Function.pipe(
|
|
784
|
+
get(node),
|
|
785
|
+
Option.flatMap(matcher),
|
|
786
|
+
Option.map((data) => runEffectSyncWithFallback(factory(data, get), context, extensionId, [])),
|
|
787
|
+
Option.getOrElse(() => []),
|
|
788
|
+
),
|
|
396
789
|
);
|
|
397
|
-
|
|
398
|
-
this._subscriptions.set(id, cancel);
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
private _onRemoveNode(id: string): void {
|
|
402
|
-
this._subscriptions.get(id)?.();
|
|
403
|
-
this._subscriptions.delete(id);
|
|
404
|
-
}
|
|
405
|
-
}
|
|
790
|
+
};
|
|
406
791
|
|
|
407
792
|
/**
|
|
408
|
-
*
|
|
409
|
-
*
|
|
793
|
+
* Options for creating a type-based extension.
|
|
794
|
+
* All callbacks must return Effects for dependency injection.
|
|
795
|
+
* Effects may fail - errors are caught, logged, and the extension returns empty results.
|
|
410
796
|
*/
|
|
411
|
-
export
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
797
|
+
export type CreateTypeExtensionOptions<T extends Type.AnyEntity = Type.AnyEntity, R = never> = {
|
|
798
|
+
id: string;
|
|
799
|
+
type: T;
|
|
800
|
+
actions?: (
|
|
801
|
+
object: Entity.Entity<Schema.Schema.Type<T>>,
|
|
802
|
+
get: Atom.Context,
|
|
803
|
+
) => Effect.Effect<Omit<Node.NodeArg<Node.ActionData<any>>, 'type'>[], Error, R>;
|
|
804
|
+
connector?: (
|
|
805
|
+
object: Entity.Entity<Schema.Schema.Type<T>>,
|
|
806
|
+
get: Atom.Context,
|
|
807
|
+
) => Effect.Effect<Node.NodeArg<any>[], Error, R>;
|
|
808
|
+
relation?: Node.RelationInput;
|
|
809
|
+
position?: Position;
|
|
810
|
+
};
|
|
418
811
|
|
|
419
|
-
|
|
812
|
+
/**
|
|
813
|
+
* Create an extension that matches nodes by schema type.
|
|
814
|
+
* The entity type is inferred from the schema type and works for both object and relation schemas.
|
|
815
|
+
* Returns an Effect to allow callbacks to access services via dependency injection.
|
|
816
|
+
*/
|
|
817
|
+
export const createTypeExtension = <T extends Type.AnyEntity, R = never>(
|
|
818
|
+
options: CreateTypeExtensionOptions<T, R>,
|
|
819
|
+
): Effect.Effect<BuilderExtension[], never, R> => {
|
|
820
|
+
const { id, type, actions, connector, relation, position } = options;
|
|
821
|
+
return createExtension<Entity.Entity<Schema.Schema.Type<T>>, R>({
|
|
822
|
+
id,
|
|
823
|
+
match: NodeMatcher.whenEchoType(type),
|
|
824
|
+
actions,
|
|
825
|
+
connector,
|
|
826
|
+
relation,
|
|
827
|
+
position,
|
|
420
828
|
});
|
|
421
829
|
};
|
|
422
830
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
get.addFinalizer(() => subscription.unsubscribe());
|
|
831
|
+
//
|
|
832
|
+
// Extension Utilities
|
|
833
|
+
//
|
|
428
834
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
835
|
+
/**
|
|
836
|
+
* Qualify node IDs by prefixing with the parent path.
|
|
837
|
+
* Validates that segment IDs do not contain the path separator.
|
|
838
|
+
* Recursively qualifies inline child nodes.
|
|
839
|
+
*/
|
|
840
|
+
const qualifyNodeArgs =
|
|
841
|
+
(parentId: string) =>
|
|
842
|
+
(nodes: Node.NodeArg<any>[]): Node.NodeArg<any>[] =>
|
|
843
|
+
nodes.map((node) => {
|
|
844
|
+
validateSegmentId(node.id);
|
|
845
|
+
const qualified = qualifyId(parentId, node.id);
|
|
846
|
+
return {
|
|
847
|
+
...node,
|
|
848
|
+
id: qualified,
|
|
849
|
+
nodes: node.nodes ? qualifyNodeArgs(qualified)(node.nodes) : undefined,
|
|
850
|
+
};
|
|
851
|
+
});
|
|
432
852
|
|
|
433
853
|
/**
|
|
434
|
-
*
|
|
435
|
-
*
|
|
854
|
+
* Recursively collect all inline-descendant IDs (the `nodes` arrays at every level)
|
|
855
|
+
* from a list of top-level NodeArgs. Top-level IDs are excluded because they are
|
|
856
|
+
* already tracked via `_connectorPrevious`.
|
|
436
857
|
*/
|
|
437
|
-
|
|
438
|
-
|
|
858
|
+
const collectAllInlineIds = (nodes: Node.NodeArg<any>[]): string[] =>
|
|
859
|
+
nodes.flatMap((node) =>
|
|
860
|
+
node.nodes ? [...node.nodes.map((child) => child.id), ...collectAllInlineIds(node.nodes)] : [],
|
|
861
|
+
);
|
|
862
|
+
|
|
863
|
+
const connectorKey = (id: string, relation: Node.RelationInput): string => primaryKey(id, Graph.relationKey(relation));
|
|
864
|
+
|
|
865
|
+
const relationFromConnectorKey = (key: string): { id: string; relation: Node.Relation } => {
|
|
866
|
+
const [id, encodedRelation] = primaryParts(key);
|
|
867
|
+
return { id, relation: Graph.relationFromKey(encodedRelation) };
|
|
868
|
+
};
|
|
869
|
+
|
|
870
|
+
const subscriptionKey = (id: string, kind: string, detail?: string): string =>
|
|
871
|
+
detail != null ? primaryKey(id, kind, detail) : primaryKey(id, kind);
|
|
872
|
+
|
|
873
|
+
export const flattenExtensions = (extension: BuilderExtensions, acc: BuilderExtension[] = []): BuilderExtension[] => {
|
|
874
|
+
if (Array.isArray(extension)) {
|
|
875
|
+
return [...acc, ...extension.flatMap((ext) => flattenExtensions(ext, acc))];
|
|
876
|
+
} else {
|
|
877
|
+
return [...acc, extension];
|
|
878
|
+
}
|
|
439
879
|
};
|