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