@dxos/app-graph 0.6.4 → 0.6.5-staging.435ed25

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.
@@ -177,13 +177,16 @@ var Graph = class {
177
177
  ];
178
178
  }
179
179
  async expand(node, relation = "outbound", type) {
180
- const key = `${node.id}-${relation}-${type}`;
180
+ const key = this._key(node, relation, type);
181
181
  const initialized = this._initialized[key];
182
182
  if (!initialized && this._onInitialNodes) {
183
183
  await this._onInitialNodes(node, relation, type);
184
184
  this._initialized[key] = true;
185
185
  }
186
186
  }
187
+ _key(node, relation, type) {
188
+ return `${node.id}-${relation}-${type}`;
189
+ }
187
190
  /**
188
191
  * Recursive depth-first traversal of the graph.
189
192
  *
@@ -369,6 +372,9 @@ var Graph = class {
369
372
  delete this._edges[id];
370
373
  }
371
374
  delete this._nodes[id];
375
+ Object.keys(this._initialized).filter((key) => key.startsWith(id)).forEach((key) => {
376
+ delete this._initialized[key];
377
+ });
372
378
  void this._onRemoveNode?.(id);
373
379
  });
374
380
  }
@@ -641,12 +647,11 @@ var GraphBuilder = class {
641
647
  }
642
648
  async _onInitialNode(nodeId) {
643
649
  this._nodeChanged[nodeId] = this._nodeChanged[nodeId] ?? signal({});
644
- let resolved = false;
645
- for (const { id, resolver } of Object.values(this._extensions)) {
646
- if (resolved || !resolver) {
647
- continue;
648
- }
649
- const unsubscribe = effect2(() => {
650
+ this._resolverSubscriptions.set(nodeId, effect2(() => {
651
+ for (const { id, resolver } of Object.values(this._extensions)) {
652
+ if (!resolver) {
653
+ continue;
654
+ }
650
655
  this._dispatcher.currentExtension = id;
651
656
  this._dispatcher.stateIndex = 0;
652
657
  BuilderInternal.currentDispatcher = this._dispatcher;
@@ -655,28 +660,26 @@ var GraphBuilder = class {
655
660
  });
656
661
  BuilderInternal.currentDispatcher = void 0;
657
662
  if (node) {
658
- resolved = true;
659
663
  this.graph._addNodes([
660
664
  node
661
665
  ]);
662
666
  if (this._nodeChanged[node.id]) {
663
667
  this._nodeChanged[node.id].value = {};
664
668
  }
669
+ break;
665
670
  }
666
- });
667
- if (resolved) {
668
- this._resolverSubscriptions.get(nodeId)?.();
669
- this._resolverSubscriptions.set(nodeId, unsubscribe);
670
- break;
671
- } else {
672
- unsubscribe();
673
671
  }
674
- }
672
+ }));
675
673
  }
676
674
  async _onInitialNodes(node, nodesRelation, nodesType) {
677
675
  this._nodeChanged[node.id] = this._nodeChanged[node.id] ?? signal({});
676
+ let first = true;
678
677
  let previous = [];
679
678
  this._connectorSubscriptions.set(node.id, effect2(() => {
679
+ if (!first && !this._connectorSubscriptions.has(node.id)) {
680
+ return;
681
+ }
682
+ first = false;
680
683
  Object.keys(this._extensions);
681
684
  this._nodeChanged[node.id].value;
682
685
  const nodes = [];
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/graph.ts", "../../../src/node.ts", "../../../src/graph-builder.ts"],
4
- "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { batch, effect, untracked } from '@preact/signals-core';\n\nimport { asyncTimeout, Trigger } from '@dxos/async';\nimport { type ReactiveObject, create } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { nonNullable } from '@dxos/util';\n\nimport { type Relation, type Node, type NodeArg, type NodeFilter, isActionLike } from './node';\n\nconst graphSymbol = Symbol('graph');\ntype DeepWriteable<T> = { -readonly [K in keyof T]: DeepWriteable<T[K]> };\ntype NodeInternal = DeepWriteable<Node> & { [graphSymbol]: Graph };\n\nexport const getGraph = (node: Node): Graph => {\n const graph = (node as NodeInternal)[graphSymbol];\n invariant(graph, 'Node is not associated with a graph.');\n return graph;\n};\n\nexport const ROOT_ID = 'root';\nexport const ROOT_TYPE = 'dxos.org/type/GraphRoot';\nexport const ACTION_TYPE = 'dxos.org/type/GraphAction';\nexport const ACTION_GROUP_TYPE = 'dxos.org/type/GraphActionGroup';\n\nexport type NodesOptions<T = any, U extends Record<string, any> = Record<string, any>> = {\n relation?: Relation;\n filter?: NodeFilter<T, U>;\n expansion?: boolean;\n type?: string;\n};\n\nexport type GraphTraversalOptions = {\n /**\n * A callback which is called for each node visited during traversal.\n *\n * If the callback returns `false`, traversal is stops recursing.\n */\n visitor: (node: Node, path: string[]) => boolean | void;\n\n /**\n * The node to start traversing from.\n *\n * @default root\n */\n node?: Node;\n\n /**\n * The relation to traverse graph edges.\n *\n * @default 'outbound'\n */\n relation?: Relation;\n\n /**\n * Allow traversal to trigger expansion of the graph via `onInitialNodes`.\n */\n expansion?: boolean;\n};\n\n/**\n * The Graph represents the structure of the application constructed via plugins.\n */\nexport class Graph {\n private readonly _onInitialNode?: (id: string) => Promise<void>;\n private readonly _onInitialNodes?: (node: Node, relation: Relation, type?: string) => Promise<void>;\n private readonly _onRemoveNode?: (id: string) => Promise<void>;\n\n private readonly _waitingForNodes: Record<string, Trigger<Node>> = {};\n private readonly _initialized: Record<string, boolean> = {};\n\n /**\n * @internal\n */\n readonly _nodes: Record<string, ReactiveObject<NodeInternal>> = {};\n\n /**\n * @internal\n */\n readonly _edges: Record<string, ReactiveObject<{ inbound: string[]; outbound: string[] }>> = {};\n\n constructor({\n onInitialNode,\n onInitialNodes,\n onRemoveNode,\n }: {\n onInitialNode?: Graph['_onInitialNode'];\n onInitialNodes?: Graph['_onInitialNodes'];\n onRemoveNode?: Graph['_onRemoveNode'];\n } = {}) {\n this._onInitialNode = onInitialNode;\n this._onInitialNodes = onInitialNodes;\n this._onRemoveNode = onRemoveNode;\n this._nodes[ROOT_ID] = this._constructNode({ id: ROOT_ID, type: ROOT_TYPE, properties: {}, data: null });\n this._edges[ROOT_ID] = create({ inbound: [], outbound: [] });\n }\n\n /**\n * Alias for `findNode('root')`.\n */\n get root() {\n return this.findNode(ROOT_ID)!;\n }\n\n /**\n * Convert the graph to a JSON object.\n */\n toJSON({ id = ROOT_ID, maxLength = 32 }: { id?: string; maxLength?: number } = {}) {\n const toJSON = (node: Node, seen: string[] = []): any => {\n const nodes = this.nodes(node);\n const obj: Record<string, any> = {\n id: node.id.length > maxLength ? `${node.id.slice(0, maxLength - 3)}...` : node.id,\n type: node.type,\n };\n if (node.properties.label) {\n obj.label = node.properties.label;\n }\n if (nodes.length) {\n obj.nodes = nodes\n .map((n) => {\n // Break cycles.\n const nextSeen = [...seen, node.id];\n return nextSeen.includes(n.id) ? undefined : toJSON(n, nextSeen);\n })\n .filter(nonNullable);\n }\n return obj;\n };\n\n const root = this.findNode(id);\n invariant(root, `Node not found: ${id}`);\n return toJSON(root);\n }\n\n /**\n * Find the node with the given id in the graph.\n *\n * If a node is not found within the graph and an `onInitialNode` callback is provided,\n * it is called with the id and type of the node, potentially initializing the node.\n */\n findNode(id: string): Node | undefined {\n const existingNode = this._nodes[id];\n if (!existingNode) {\n void this._onInitialNode?.(id);\n }\n\n return existingNode;\n }\n\n /**\n * Wait for a node to be added to the graph.\n *\n * If the node is already present in the graph, the promise resolves immediately.\n *\n * @param id The id of the node to wait for.\n * @param timeout The time in milliseconds to wait for the node to be added.\n */\n async waitForNode(id: string, timeout?: number): Promise<Node> {\n const trigger = this._waitingForNodes[id] ?? (this._waitingForNodes[id] = new Trigger<Node>());\n const node = this.findNode(id);\n if (node) {\n delete this._waitingForNodes[id];\n return node;\n }\n\n if (timeout === undefined) {\n return trigger.wait();\n } else {\n return asyncTimeout(trigger.wait(), timeout, `Node not found: ${id}`);\n }\n }\n\n /**\n * Nodes that this node is connected to in default order.\n */\n nodes<T = any, U extends Record<string, any> = Record<string, any>>(node: Node, options: NodesOptions<T, U> = {}) {\n const { relation, expansion, filter, type } = options;\n const nodes = this._getNodes({ node, relation, expansion, type });\n return nodes.filter((n) => untracked(() => !isActionLike(n))).filter((n) => filter?.(n, node) ?? true);\n }\n\n /**\n * Edges that this node is connected to in default order.\n */\n edges(node: Node, { relation = 'outbound' }: { relation?: Relation } = {}) {\n return this._edges[node.id]?.[relation] ?? [];\n }\n\n /**\n * Actions or action groups that this node is connected to in default order.\n */\n actions(node: Node, { expansion }: { expansion?: boolean } = {}) {\n return [\n ...this._getNodes({ node, expansion, type: ACTION_GROUP_TYPE }),\n ...this._getNodes({ node, expansion, type: ACTION_TYPE }),\n ];\n }\n\n async expand(node: Node, relation: Relation = 'outbound', type?: string) {\n // TODO(wittjosiah): Factor out helper.\n const key = `${node.id}-${relation}-${type}`;\n const initialized = this._initialized[key];\n if (!initialized && this._onInitialNodes) {\n await this._onInitialNodes(node, relation, type);\n this._initialized[key] = true;\n }\n }\n\n /**\n * Recursive depth-first traversal of the graph.\n *\n * @param options.node The node to start traversing from.\n * @param options.relation The relation to traverse graph edges.\n * @param options.visitor A callback which is called for each node visited during traversal.\n */\n traverse(\n { visitor, node = this.root, relation = 'outbound', expansion }: GraphTraversalOptions,\n path: string[] = [],\n ): void {\n // Break cycles.\n if (path.includes(node.id)) {\n return;\n }\n\n const shouldContinue = visitor(node, [...path, node.id]);\n if (shouldContinue === false) {\n return;\n }\n\n Object.values(this._getNodes({ node, relation, expansion })).forEach((child) =>\n this.traverse({ node: child, relation, visitor, expansion }, [...path, node.id]),\n );\n }\n\n /**\n * Recursive depth-first traversal of the graph wrapping each visitor call in an effect.\n *\n * @param options.node The node to start traversing from.\n * @param options.relation The relation to traverse graph edges.\n * @param options.visitor A callback which is called for each node visited during traversal.\n */\n subscribeTraverse(\n { visitor, node = this.root, relation = 'outbound', expansion }: GraphTraversalOptions,\n currentPath: string[] = [],\n ) {\n return effect(() => {\n const path = [...currentPath, node.id];\n const result = visitor(node, path);\n if (result === false) {\n return;\n }\n\n const nodes = this._getNodes({ node, relation, expansion });\n const nodeSubscriptions = nodes.map((n) => this.subscribeTraverse({ node: n, visitor, expansion }, path));\n\n return () => {\n nodeSubscriptions.forEach((unsubscribe) => unsubscribe());\n };\n });\n }\n\n /**\n * Get the path between two nodes in the graph.\n */\n getPath({ source = 'root', target }: { source?: string; target: string }): string[] | undefined {\n const start = this.findNode(source);\n if (!start) {\n return undefined;\n }\n\n let found: string[] | undefined;\n this.traverse({\n node: start,\n visitor: (node, path) => {\n if (found) {\n return false;\n }\n\n if (node.id === target) {\n found = path;\n }\n },\n });\n\n return found;\n }\n\n /**\n * Add nodes to the graph.\n *\n * @internal\n */\n _addNodes<TData = null, TProperties extends Record<string, any> = Record<string, any>>(\n nodes: NodeArg<TData, TProperties>[],\n ): Node<TData, TProperties>[] {\n return batch(() => nodes.map((node) => this._addNode(node)));\n }\n\n private _addNode<TData, TProperties extends Record<string, any> = Record<string, any>>({\n nodes,\n edges,\n ..._node\n }: NodeArg<TData, TProperties>): Node<TData, TProperties> {\n return untracked(() => {\n const existingNode = this._nodes[_node.id];\n const node = existingNode ?? this._constructNode({ data: null, properties: {}, ..._node });\n if (existingNode) {\n const { data, properties, type } = _node;\n if (data && data !== node.data) {\n node.data = data;\n }\n\n if (type !== node.type) {\n node.type = type;\n }\n\n for (const key in properties) {\n if (properties[key] !== node.properties[key]) {\n node.properties[key] = properties[key];\n }\n }\n } else {\n this._nodes[node.id] = node;\n this._edges[node.id] = create({ inbound: [], outbound: [] });\n }\n\n const trigger = this._waitingForNodes[node.id];\n if (trigger) {\n trigger.wake(node);\n delete this._waitingForNodes[node.id];\n }\n\n if (nodes) {\n nodes.forEach((subNode) => {\n this._addNode(subNode);\n this._addEdge({ source: node.id, target: subNode.id });\n });\n }\n\n if (edges) {\n edges.forEach(([id, relation]) =>\n relation === 'outbound'\n ? this._addEdge({ source: node.id, target: id })\n : this._addEdge({ source: id, target: node.id }),\n );\n }\n\n return node as unknown as Node<TData, TProperties>;\n });\n }\n\n /**\n * Remove nodes from the graph.\n *\n * @param ids The id of the node to remove.\n * @param edges Whether to remove edges connected to the node from the graph as well.\n * @internal\n */\n _removeNodes(ids: string[], edges = false) {\n batch(() => ids.forEach((id) => this._removeNode(id, edges)));\n }\n\n private _removeNode(id: string, edges = false) {\n untracked(() => {\n const node = this.findNode(id);\n if (!node) {\n return;\n }\n\n if (edges) {\n // Remove edges from connected nodes.\n this._getNodes({ node }).forEach((node) => {\n this._removeEdge({ source: id, target: node.id });\n });\n this._getNodes({ node, relation: 'inbound' }).forEach((node) => {\n this._removeEdge({ source: node.id, target: id });\n });\n\n // Remove edges from node.\n delete this._edges[id];\n }\n\n // Remove node.\n delete this._nodes[id];\n void this._onRemoveNode?.(id);\n });\n }\n\n /**\n * Add edges to the graph.\n *\n * @internal\n */\n _addEdges(edges: { source: string; target: string }[]) {\n batch(() => edges.forEach((edge) => this._addEdge(edge)));\n }\n\n private _addEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n if (!this._edges[source]) {\n this._edges[source] = create({ inbound: [], outbound: [] });\n }\n if (!this._edges[target]) {\n this._edges[target] = create({ inbound: [], outbound: [] });\n }\n\n const sourceEdges = this._edges[source];\n if (!sourceEdges.outbound.includes(target)) {\n sourceEdges.outbound.push(target);\n }\n\n const targetEdges = this._edges[target];\n if (!targetEdges.inbound.includes(source)) {\n targetEdges.inbound.push(source);\n }\n });\n }\n\n /**\n * Remove edges from the graph.\n * @internal\n */\n _removeEdges(edges: { source: string; target: string }[]) {\n batch(() => edges.forEach((edge) => this._removeEdge(edge)));\n }\n\n private _removeEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n batch(() => {\n const outboundIndex = this._edges[source]?.outbound.findIndex((id) => id === target);\n if (outboundIndex !== undefined && outboundIndex !== -1) {\n this._edges[source].outbound.splice(outboundIndex, 1);\n }\n\n const inboundIndex = this._edges[target]?.inbound.findIndex((id) => id === source);\n if (inboundIndex !== undefined && inboundIndex !== -1) {\n this._edges[target].inbound.splice(inboundIndex, 1);\n }\n });\n });\n }\n\n /**\n * Sort edges for a node.\n *\n * Edges not included in the sorted list are appended to the end of the list.\n *\n * @param nodeId The id of the node to sort edges for.\n * @param relation The relation of the edges from the node to sort.\n * @param edges The ordered list of edges.\n * @ignore\n */\n _sortEdges(nodeId: string, relation: Relation, edges: string[]) {\n untracked(() => {\n batch(() => {\n const current = this._edges[nodeId];\n if (current) {\n const unsorted = current[relation].filter((id) => !edges.includes(id)) ?? [];\n const sorted = edges.filter((id) => current[relation].includes(id)) ?? [];\n current[relation].splice(0, current[relation].length, ...[...sorted, ...unsorted]);\n }\n });\n });\n }\n\n private _constructNode = (node: Omit<Node, typeof graphSymbol>) => {\n return create<NodeInternal>({ ...node, [graphSymbol]: this });\n };\n\n private _getNodes({\n node,\n relation = 'outbound',\n type,\n expansion,\n }: {\n node: Node;\n relation?: Relation;\n type?: string;\n expansion?: boolean;\n }): Node[] {\n if (expansion) {\n void this.expand(node, relation, type);\n }\n\n const edges = this._edges[node.id];\n if (!edges) {\n return [];\n } else {\n return edges[relation]\n .map((id) => this._nodes[id])\n .filter(nonNullable)\n .filter((n) => !type || n.type === type);\n }\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { type MaybePromise, type MakeOptional } from '@dxos/util';\n\n/**\n * Represents a node in the graph.\n */\n// TODO(wittjosiah): Use Effect Schema.\nexport type Node<TData = any, TProperties extends Record<string, any> = Record<string, any>> = Readonly<{\n /**\n * Globally unique ID.\n */\n id: string;\n\n /**\n * Typename of the data the node represents.\n */\n type: string;\n\n /**\n * Properties of the node relevant to displaying the node.\n */\n properties: Readonly<TProperties>;\n\n /**\n * Data the node represents.\n */\n // TODO(burdon): Type system (e.g., minimally provide identifier string vs. TypedObject vs. Graph mixin type system)?\n // type field would prevent convoluted sniffing of object properties. And allow direct pass-through for ECHO TypedObjects.\n data: TData;\n}>;\n\nexport type NodeFilter<T = any, U extends Record<string, any> = Record<string, any>> = (\n node: Node<unknown, Record<string, any>>,\n connectedNode: Node,\n) => node is Node<T, U>;\n\nexport type Relation = 'outbound' | 'inbound';\n\nexport const isGraphNode = (data: unknown): data is Node =>\n data && typeof data === 'object' && 'id' in data && 'properties' in data && data.properties\n ? typeof data.properties === 'object' && 'data' in data\n : false;\n\nexport type NodeArg<TData, TProperties extends Record<string, any> = Record<string, any>> = MakeOptional<\n Node<TData, TProperties>,\n 'data' | 'properties'\n> & {\n /** Will automatically add nodes with an edge from this node to each. */\n nodes?: NodeArg<unknown>[];\n\n /** Will automatically add specified edges. */\n edges?: [string, Relation][];\n};\n\n//\n// Actions\n//\n\nexport type InvokeParams = {\n /** Node the invoked action is connected to. */\n node: Node;\n\n caller?: string;\n};\n\nexport type ActionData = (params: InvokeParams) => MaybePromise<void>;\n\nexport type Action<TProperties extends Record<string, any> = Record<string, any>> = Readonly<\n Omit<Node<ActionData, TProperties>, 'properties'> & {\n properties: Readonly<TProperties>;\n }\n>;\n\nexport const isAction = (data: unknown): data is Action =>\n isGraphNode(data) ? typeof data.data === 'function' : false;\n\nexport const actionGroupSymbol = Symbol('ActionGroup');\n\nexport type ActionGroup = Readonly<\n Omit<Node<typeof actionGroupSymbol, Record<string, any>>, 'properties'> & {\n properties: Readonly<Record<string, any>>;\n }\n>;\n\nexport const isActionGroup = (data: unknown): data is ActionGroup =>\n isGraphNode(data) ? data.data === actionGroupSymbol : false;\n\nexport type ActionLike = Action | ActionGroup;\n\nexport const isActionLike = (data: unknown): data is Action | ActionGroup => isAction(data) || isActionGroup(data);\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { type Signal, effect, signal } from '@preact/signals-core';\n// import { yieldOrContinue } from 'main-thread-scheduling';\n\nimport { type UnsubscribeCallback } from '@dxos/async';\nimport { create } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { nonNullable } from '@dxos/util';\n\nimport { ACTION_GROUP_TYPE, ACTION_TYPE, Graph } from './graph';\nimport { type Relation, type NodeArg, type Node, type ActionData, actionGroupSymbol } from './node';\n\n/**\n * Graph builder extension for adding nodes to the graph based on just the node id.\n * This is useful for creating the first node in a graph or for hydrating cached nodes with data.\n *\n * @param params.id The id of the node to resolve.\n */\nexport type ResolverExtension = (params: { id: string }) => NodeArg<any> | undefined;\n\n/**\n * Graph builder extension for adding nodes to the graph based on a connection to an existing node.\n *\n * @param params.node The existing node the returned nodes will be connected to.\n */\nexport type ConnectorExtension<T = any> = (params: { node: Node<T> }) => NodeArg<any>[] | undefined;\n\n/**\n * Constrained case of the connector extension for more easily adding actions to the graph.\n */\nexport type ActionsExtension<T = any> = (params: {\n node: Node<T>;\n}) => Omit<NodeArg<ActionData>, 'type' | 'nodes' | 'edges'>[] | undefined;\n\n/**\n * Constrained case of the connector extension for more easily adding action groups to the graph.\n */\nexport type ActionGroupsExtension<T = any> = (params: {\n node: Node<T>;\n}) => Omit<NodeArg<typeof actionGroupSymbol>, 'type' | 'data' | 'nodes' | 'edges'>[] | undefined;\n\ntype GuardedNodeType<T> = T extends (value: any) => value is infer N ? (N extends Node<infer D> ? D : unknown) : never;\n\n/**\n * A graph builder extension is used to add nodes to the graph.\n *\n * @param params.id The unique id of the extension.\n * @param params.relation The relation the graph is being expanded from the existing node.\n * @param params.type If provided, all nodes returned are expected to have this type.\n * @param params.filter A filter function to determine if an extension should act on a node.\n * @param params.resolver A function to add nodes to the graph based on just the node id.\n * @param params.connector A function to add nodes to the graph based on a connection to an existing node.\n * @param params.actions A function to add actions to the graph based on a connection to an existing node.\n * @param params.actionGroups A function to add action groups to the graph based on a connection to an existing node.\n */\nexport type CreateExtensionOptions<T = any> = {\n id: string;\n relation?: Relation;\n type?: string;\n filter?: (node: Node) => node is Node<T>;\n resolver?: ResolverExtension;\n connector?: ConnectorExtension<GuardedNodeType<CreateExtensionOptions<T>['filter']>>;\n actions?: ActionsExtension<GuardedNodeType<CreateExtensionOptions<T>['filter']>>;\n actionGroups?: ActionGroupsExtension<GuardedNodeType<CreateExtensionOptions<T>['filter']>>;\n};\n\n/**\n * Create a graph builder extension.\n */\nexport const createExtension = <T = any>(extension: CreateExtensionOptions<T>): BuilderExtension[] => {\n const { id, resolver, connector, actions, actionGroups, ...rest } = extension;\n const getId = (key: string) => `${id}/${key}`;\n return [\n resolver ? { id: getId('resolver'), resolver } : undefined,\n connector ? { ...rest, id: getId('connector'), connector } : undefined,\n actionGroups\n ? ({\n ...rest,\n id: getId('actionGroups'),\n type: ACTION_GROUP_TYPE,\n relation: 'outbound',\n connector: ({ node }) =>\n actionGroups({ node })?.map((arg) => ({ ...arg, data: actionGroupSymbol, type: ACTION_GROUP_TYPE })),\n } satisfies BuilderExtension)\n : undefined,\n actions\n ? ({\n ...rest,\n id: getId('actions'),\n type: ACTION_TYPE,\n relation: 'outbound',\n connector: ({ node }) => actions({ node })?.map((arg) => ({ ...arg, type: ACTION_TYPE })),\n } satisfies BuilderExtension)\n : undefined,\n ].filter(nonNullable);\n};\n\nexport type GraphBuilderTraverseOptions = {\n node: Node;\n relation?: Relation;\n visitor: (node: Node, path: string[]) => void;\n};\n\n/**\n * The dispatcher is used to keep track of the current extension and state when memoizing functions.\n */\nclass Dispatcher {\n currentExtension?: string;\n stateIndex = 0;\n state: Record<string, any[]> = {};\n cleanup: (() => void)[] = [];\n}\n\nclass BuilderInternal {\n // This must be static to avoid passing the dispatcher instance to every memoized function.\n // If the dispatcher is not set that means that the memoized function is being called outside of the graph builder.\n static currentDispatcher?: Dispatcher;\n}\n\n/**\n * Allows code to be memoized within the context of a graph builder extension.\n * This is useful for creating instances which should be subscribed to rather than recreated.\n */\nexport const memoize = <T>(fn: () => T, key = 'result'): T => {\n const dispatcher = BuilderInternal.currentDispatcher;\n invariant(dispatcher?.currentExtension, 'memoize must be called within an extension');\n const all = dispatcher.state[dispatcher.currentExtension][dispatcher.stateIndex] ?? {};\n const current = all[key];\n const result = current ? current.result : fn();\n dispatcher.state[dispatcher.currentExtension][dispatcher.stateIndex] = { ...all, [key]: { result } };\n dispatcher.stateIndex++;\n return result;\n};\n\n/**\n * Register a cleanup function to be called when the graph builder is destroyed.\n */\nexport const cleanup = (fn: () => void): void => {\n memoize(() => {\n const dispatcher = BuilderInternal.currentDispatcher;\n invariant(dispatcher, 'cleanup must be called within an extension');\n dispatcher.cleanup.push(fn);\n });\n};\n\n/**\n * Convert a subscribe/get pair into a signal.\n */\nexport const toSignal = <T>(\n subscribe: (onChange: () => void) => () => void,\n get: () => T | undefined,\n key?: string,\n) => {\n const thisSignal = memoize(() => {\n return signal(get());\n }, key);\n const unsubscribe = memoize(() => {\n return subscribe(() => (thisSignal.value = get()));\n }, key);\n cleanup(() => {\n unsubscribe();\n });\n return thisSignal.value;\n};\n\nexport type BuilderExtension = {\n id: string;\n resolver?: ResolverExtension;\n connector?: ConnectorExtension;\n // Only for connector.\n relation?: Relation;\n type?: string;\n filter?: (node: Node) => boolean;\n};\n\ntype ExtensionArg = BuilderExtension | BuilderExtension[] | ExtensionArg[];\n\n/**\n * The builder provides an extensible way to compose the construction of the graph.\n */\n// TODO(wittjosiah): Add api for setting subscription set and/or radius.\n// Should unsubscribe from nodes that are not in the set/radius.\n// Should track LRU nodes that are not in the set/radius and remove them beyond a certain threshold.\nexport class GraphBuilder {\n private readonly _dispatcher = new Dispatcher();\n private readonly _extensions = create<Record<string, BuilderExtension>>({});\n private readonly _resolverSubscriptions = new Map<string, UnsubscribeCallback>();\n private readonly _connectorSubscriptions = new Map<string, UnsubscribeCallback>();\n private readonly _nodeChanged: Record<string, Signal<{}>> = {};\n private _graph: Graph;\n\n constructor() {\n this._graph = new Graph({\n onInitialNode: (id) => this._onInitialNode(id),\n onInitialNodes: (node, relation, type) => this._onInitialNodes(node, relation, type),\n onRemoveNode: (id) => this._onRemoveNode(id),\n });\n }\n\n get graph() {\n return this._graph;\n }\n\n /**\n * Register a node builder which will be called in order to construct the graph.\n */\n addExtension(extension: ExtensionArg): GraphBuilder {\n if (Array.isArray(extension)) {\n extension.forEach((ext) => this.addExtension(ext));\n return this;\n }\n\n this._dispatcher.state[extension.id] = [];\n this._extensions[extension.id] = extension;\n return this;\n }\n\n /**\n * Remove a node builder from the graph builder.\n */\n removeExtension(id: string): GraphBuilder {\n delete this._extensions[id];\n return this;\n }\n\n destroy() {\n this._dispatcher.cleanup.forEach((fn) => fn());\n this._resolverSubscriptions.forEach((unsubscribe) => unsubscribe());\n this._connectorSubscriptions.forEach((unsubscribe) => unsubscribe());\n this._resolverSubscriptions.clear();\n this._connectorSubscriptions.clear();\n }\n\n /**\n * Traverse a graph using just the connector extensions, without subscribing to any signals or persisting any nodes.\n */\n // TODO(wittjosiah): Rename? This is not traversing the graph proper.\n async traverse({ node, relation = 'outbound', visitor }: GraphBuilderTraverseOptions, path: string[] = []) {\n // Break cycles.\n if (path.includes(node.id)) {\n return;\n }\n\n // TODO(wittjosiah): Failed in test environment. ESM only?\n // await yieldOrContinue('idle');\n visitor(node, [...path, node.id]);\n\n const nodes = Object.values(this._extensions)\n .filter((extension) => relation === (extension.relation ?? 'outbound'))\n .flatMap((extension) => extension.connector?.({ node }) ?? [])\n .map(\n (arg): Node => ({\n id: arg.id,\n type: arg.type,\n data: arg.data ?? null,\n properties: arg.properties ?? {},\n }),\n );\n\n await Promise.all(nodes.map((n) => this.traverse({ node: n, relation, visitor }, [...path, node.id])));\n }\n\n private async _onInitialNode(nodeId: string) {\n this._nodeChanged[nodeId] = this._nodeChanged[nodeId] ?? signal({});\n let resolved = false;\n for (const { id, resolver } of Object.values(this._extensions)) {\n if (resolved || !resolver) {\n continue;\n }\n\n const unsubscribe = effect(() => {\n this._dispatcher.currentExtension = id;\n this._dispatcher.stateIndex = 0;\n BuilderInternal.currentDispatcher = this._dispatcher;\n const node = resolver({ id: nodeId });\n BuilderInternal.currentDispatcher = undefined;\n if (node) {\n resolved = true;\n this.graph._addNodes([node]);\n if (this._nodeChanged[node.id]) {\n this._nodeChanged[node.id].value = {};\n }\n }\n });\n\n if (resolved) {\n this._resolverSubscriptions.get(nodeId)?.();\n this._resolverSubscriptions.set(nodeId, unsubscribe);\n break;\n } else {\n unsubscribe();\n }\n }\n }\n\n private async _onInitialNodes(node: Node, nodesRelation: Relation, nodesType?: string) {\n this._nodeChanged[node.id] = this._nodeChanged[node.id] ?? signal({});\n let previous: string[] = [];\n this._connectorSubscriptions.set(\n node.id,\n effect(() => {\n // Subscribe to extensions being added.\n Object.keys(this._extensions);\n // Subscribe to connected node changes.\n this._nodeChanged[node.id].value;\n\n // TODO(wittjosiah): Consider allowing extensions to collaborate on the same node by merging their results.\n const nodes: NodeArg<any>[] = [];\n for (const { id, connector, filter, type, relation = 'outbound' } of Object.values(this._extensions)) {\n if (\n !connector ||\n relation !== nodesRelation ||\n (nodesType && type !== nodesType) ||\n (filter && !filter(node))\n ) {\n continue;\n }\n\n this._dispatcher.currentExtension = id;\n this._dispatcher.stateIndex = 0;\n BuilderInternal.currentDispatcher = this._dispatcher;\n nodes.push(...(connector({ node }) ?? []));\n BuilderInternal.currentDispatcher = undefined;\n }\n const ids = nodes.map((n) => n.id);\n const removed = previous.filter((id) => !ids.includes(id));\n previous = ids;\n\n this.graph._removeNodes(removed, true);\n this.graph._addNodes(nodes);\n this.graph._addEdges(\n nodes.map(({ id }) =>\n nodesRelation === 'outbound' ? { source: node.id, target: id } : { source: id, target: node.id },\n ),\n );\n this.graph._sortEdges(\n node.id,\n nodesRelation,\n nodes.map(({ id }) => id),\n );\n nodes.forEach((n) => {\n if (this._nodeChanged[n.id]) {\n this._nodeChanged[n.id].value = {};\n }\n });\n }),\n );\n }\n\n private async _onRemoveNode(nodeId: string) {\n this._resolverSubscriptions.get(nodeId)?.();\n this._connectorSubscriptions.get(nodeId)?.();\n this._resolverSubscriptions.delete(nodeId);\n this._connectorSubscriptions.delete(nodeId);\n }\n}\n"],
5
- "mappings": ";AAIA,SAASA,OAAOC,QAAQC,iBAAiB;AAEzC,SAASC,cAAcC,eAAe;AACtC,SAA8BC,cAAc;AAC5C,SAASC,iBAAiB;AAC1B,SAASC,mBAAmB;;;ACgCrB,IAAMC,cAAc,CAACC,SAC1BA,QAAQ,OAAOA,SAAS,YAAY,QAAQA,QAAQ,gBAAgBA,QAAQA,KAAKC,aAC7E,OAAOD,KAAKC,eAAe,YAAY,UAAUD,OACjD;AAgCC,IAAME,WAAW,CAACF,SACvBD,YAAYC,IAAAA,IAAQ,OAAOA,KAAKA,SAAS,aAAa;AAEjD,IAAMG,oBAAoBC,OAAO,aAAA;AAQjC,IAAMC,gBAAgB,CAACL,SAC5BD,YAAYC,IA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ACzC4B,cAAMtC,KAAI,GAAKhC,UAAU;UAAEU;QAAK,CAAA,KAAM,CAAA,CAAE;AACxCY,wBAAgBI,oBAAoBpB;MACtC;AACA,YAAMgF,MAAMhB,MAAM3D,IAAI,CAACiE,MAAMA,EAAE9E,EAAE;AACjC,YAAMyF,UAAUH,SAASpE,OAAO,CAAClB,OAAO,CAACwF,IAAIjB,SAASvE,EAAAA,CAAAA;AACtDsF,iBAAWE;AAEX,WAAK7B,MAAM+B,aAAaD,SAAS,IAAA;AACjC,WAAK9B,MAAMuB,UAAUV,KAAAA;AACrB,WAAKb,MAAMgC,UACTnB,MAAM3D,IAAI,CAAC,EAAEb,GAAE,MACboF,kBAAkB,aAAa;QAAEQ,QAAQhF,KAAKZ;QAAI6F,QAAQ7F;MAAG,IAAI;QAAE4F,QAAQ5F;QAAI6F,QAAQjF,KAAKZ;MAAG,CAAA,CAAA;AAGnG,WAAK2D,MAAMmC,WACTlF,KAAKZ,IACLoF,eACAZ,MAAM3D,IAAI,CAAC,EAAEb,GAAE,MAAOA,EAAAA,CAAAA;AAExBwE,YAAMT,QAAQ,CAACe,MAAAA;AACb,YAAI,KAAK5B,aAAa4B,EAAE9E,EAAE,GAAG;AAC3B,eAAKkD,aAAa4B,EAAE9E,EAAE,EAAEyC,QAAQ,CAAC;QACnC;MACF,CAAA;IACF,CAAA,CAAA;EAEJ;EAEA,MAAciB,cAAcqB,QAAgB;AAC1C,SAAKhC,uBAAuBV,IAAI0C,MAAAA,IAAAA;AAChC,SAAK9B,wBAAwBZ,IAAI0C,MAAAA,IAAAA;AACjC,SAAKhC,uBAAuBgD,OAAOhB,MAAAA;AACnC,SAAK9B,wBAAwB8C,OAAOhB,MAAAA;EACtC;AACF;",
6
- "names": ["batch", "effect", "untracked", "asyncTimeout", "Trigger", "create", "invariant", "nonNullable", "isGraphNode", "data", "properties", "isAction", "actionGroupSymbol", "Symbol", "isActionGroup", "isActionLike", "graphSymbol", "Symbol", "getGraph", "node", "graph", "invariant", "ROOT_ID", "ROOT_TYPE", "ACTION_TYPE", "ACTION_GROUP_TYPE", "Graph", "constructor", "onInitialNode", "onInitialNodes", "onRemoveNode", "_waitingForNodes", "_initialized", "_nodes", "_edges", "_constructNode", "create", "_onInitialNode", "_onInitialNodes", "_onRemoveNode", "id", "type", "properties", "data", "inbound", "outbound", "root", "findNode", "toJSON", "maxLength", "seen", "nodes", "obj", "length", "slice", "label", "map", "n", "nextSeen", "includes", "undefined", "filter", "nonNullable", "existingNode", "waitForNode", "timeout", "trigger", "Trigger", "wait", "asyncTimeout", "options", "relation", "expansion", "_getNodes", "untracked", "isActionLike", "edges", "actions", "expand", "key", "initialized", "traverse", "visitor", "path", "shouldContinue", "Object", "values", "forEach", "child", "subscribeTraverse", "currentPath", "effect", "result", "nodeSubscriptions", "unsubscribe", "getPath", "source", "target", "start", "found", "_addNodes", "batch", "_addNode", "_node", "wake", "subNode", "_addEdge", "_removeNodes", "ids", "_removeNode", "_removeEdge", "_addEdges", "edge", "sourceEdges", "push", "targetEdges", "_removeEdges", "outboundIndex", "findIndex", "splice", "inboundIndex", "_sortEdges", "nodeId", "current", "unsorted", "sorted", "effect", "signal", "create", "invariant", "nonNullable", "createExtension", "extension", "id", "resolver", "connector", "actions", "actionGroups", "rest", "getId", "key", "undefined", "type", "ACTION_GROUP_TYPE", "relation", "node", "map", "arg", "data", "actionGroupSymbol", "ACTION_TYPE", "filter", "nonNullable", "Dispatcher", "stateIndex", "state", "cleanup", "BuilderInternal", "memoize", "fn", "dispatcher", "currentDispatcher", "invariant", "currentExtension", "all", "current", "result", "push", "toSignal", "subscribe", "get", "thisSignal", "signal", "unsubscribe", "value", "GraphBuilder", "constructor", "_dispatcher", "_extensions", "create", "_resolverSubscriptions", "Map", "_connectorSubscriptions", "_nodeChanged", "_graph", "Graph", "onInitialNode", "_onInitialNode", "onInitialNodes", "_onInitialNodes", "onRemoveNode", "_onRemoveNode", "graph", "addExtension", "Array", "isArray", "forEach", "ext", "removeExtension", "destroy", "clear", "traverse", "visitor", "path", "includes", "nodes", "Object", "values", "flatMap", "properties", "Promise", "n", "nodeId", "resolved", "effect", "_addNodes", "set", "nodesRelation", "nodesType", "previous", "keys", "ids", "removed", "_removeNodes", "_addEdges", "source", "target", "_sortEdges", "delete"]
4
+ "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { batch, effect, untracked } from '@preact/signals-core';\n\nimport { asyncTimeout, Trigger } from '@dxos/async';\nimport { type ReactiveObject, create } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { nonNullable } from '@dxos/util';\n\nimport { type Relation, type Node, type NodeArg, type NodeFilter, isActionLike } from './node';\n\nconst graphSymbol = Symbol('graph');\ntype DeepWriteable<T> = { -readonly [K in keyof T]: DeepWriteable<T[K]> };\ntype NodeInternal = DeepWriteable<Node> & { [graphSymbol]: Graph };\n\nexport const getGraph = (node: Node): Graph => {\n const graph = (node as NodeInternal)[graphSymbol];\n invariant(graph, 'Node is not associated with a graph.');\n return graph;\n};\n\nexport const ROOT_ID = 'root';\nexport const ROOT_TYPE = 'dxos.org/type/GraphRoot';\nexport const ACTION_TYPE = 'dxos.org/type/GraphAction';\nexport const ACTION_GROUP_TYPE = 'dxos.org/type/GraphActionGroup';\n\nexport type NodesOptions<T = any, U extends Record<string, any> = Record<string, any>> = {\n relation?: Relation;\n filter?: NodeFilter<T, U>;\n expansion?: boolean;\n type?: string;\n};\n\nexport type GraphTraversalOptions = {\n /**\n * A callback which is called for each node visited during traversal.\n *\n * If the callback returns `false`, traversal is stops recursing.\n */\n visitor: (node: Node, path: string[]) => boolean | void;\n\n /**\n * The node to start traversing from.\n *\n * @default root\n */\n node?: Node;\n\n /**\n * The relation to traverse graph edges.\n *\n * @default 'outbound'\n */\n relation?: Relation;\n\n /**\n * Allow traversal to trigger expansion of the graph via `onInitialNodes`.\n */\n expansion?: boolean;\n};\n\n/**\n * The Graph represents the structure of the application constructed via plugins.\n */\nexport class Graph {\n private readonly _onInitialNode?: (id: string) => Promise<void>;\n private readonly _onInitialNodes?: (node: Node, relation: Relation, type?: string) => Promise<void>;\n private readonly _onRemoveNode?: (id: string) => Promise<void>;\n\n private readonly _waitingForNodes: Record<string, Trigger<Node>> = {};\n private readonly _initialized: Record<string, boolean> = {};\n\n /**\n * @internal\n */\n readonly _nodes: Record<string, ReactiveObject<NodeInternal>> = {};\n\n /**\n * @internal\n */\n readonly _edges: Record<string, ReactiveObject<{ inbound: string[]; outbound: string[] }>> = {};\n\n constructor({\n onInitialNode,\n onInitialNodes,\n onRemoveNode,\n }: {\n onInitialNode?: Graph['_onInitialNode'];\n onInitialNodes?: Graph['_onInitialNodes'];\n onRemoveNode?: Graph['_onRemoveNode'];\n } = {}) {\n this._onInitialNode = onInitialNode;\n this._onInitialNodes = onInitialNodes;\n this._onRemoveNode = onRemoveNode;\n this._nodes[ROOT_ID] = this._constructNode({ id: ROOT_ID, type: ROOT_TYPE, properties: {}, data: null });\n this._edges[ROOT_ID] = create({ inbound: [], outbound: [] });\n }\n\n /**\n * Alias for `findNode('root')`.\n */\n get root() {\n return this.findNode(ROOT_ID)!;\n }\n\n /**\n * Convert the graph to a JSON object.\n */\n toJSON({ id = ROOT_ID, maxLength = 32 }: { id?: string; maxLength?: number } = {}) {\n const toJSON = (node: Node, seen: string[] = []): any => {\n const nodes = this.nodes(node);\n const obj: Record<string, any> = {\n id: node.id.length > maxLength ? `${node.id.slice(0, maxLength - 3)}...` : node.id,\n type: node.type,\n };\n if (node.properties.label) {\n obj.label = node.properties.label;\n }\n if (nodes.length) {\n obj.nodes = nodes\n .map((n) => {\n // Break cycles.\n const nextSeen = [...seen, node.id];\n return nextSeen.includes(n.id) ? undefined : toJSON(n, nextSeen);\n })\n .filter(nonNullable);\n }\n return obj;\n };\n\n const root = this.findNode(id);\n invariant(root, `Node not found: ${id}`);\n return toJSON(root);\n }\n\n /**\n * Find the node with the given id in the graph.\n *\n * If a node is not found within the graph and an `onInitialNode` callback is provided,\n * it is called with the id and type of the node, potentially initializing the node.\n */\n findNode(id: string): Node | undefined {\n const existingNode = this._nodes[id];\n if (!existingNode) {\n void this._onInitialNode?.(id);\n }\n\n return existingNode;\n }\n\n /**\n * Wait for a node to be added to the graph.\n *\n * If the node is already present in the graph, the promise resolves immediately.\n *\n * @param id The id of the node to wait for.\n * @param timeout The time in milliseconds to wait for the node to be added.\n */\n async waitForNode(id: string, timeout?: number): Promise<Node> {\n const trigger = this._waitingForNodes[id] ?? (this._waitingForNodes[id] = new Trigger<Node>());\n const node = this.findNode(id);\n if (node) {\n delete this._waitingForNodes[id];\n return node;\n }\n\n if (timeout === undefined) {\n return trigger.wait();\n } else {\n return asyncTimeout(trigger.wait(), timeout, `Node not found: ${id}`);\n }\n }\n\n /**\n * Nodes that this node is connected to in default order.\n */\n nodes<T = any, U extends Record<string, any> = Record<string, any>>(node: Node, options: NodesOptions<T, U> = {}) {\n const { relation, expansion, filter, type } = options;\n const nodes = this._getNodes({ node, relation, expansion, type });\n return nodes.filter((n) => untracked(() => !isActionLike(n))).filter((n) => filter?.(n, node) ?? true);\n }\n\n /**\n * Edges that this node is connected to in default order.\n */\n edges(node: Node, { relation = 'outbound' }: { relation?: Relation } = {}) {\n return this._edges[node.id]?.[relation] ?? [];\n }\n\n /**\n * Actions or action groups that this node is connected to in default order.\n */\n actions(node: Node, { expansion }: { expansion?: boolean } = {}) {\n return [\n ...this._getNodes({ node, expansion, type: ACTION_GROUP_TYPE }),\n ...this._getNodes({ node, expansion, type: ACTION_TYPE }),\n ];\n }\n\n async expand(node: Node, relation: Relation = 'outbound', type?: string) {\n const key = this._key(node, relation, type);\n const initialized = this._initialized[key];\n if (!initialized && this._onInitialNodes) {\n await this._onInitialNodes(node, relation, type);\n this._initialized[key] = true;\n }\n }\n\n private _key(node: Node, relation: Relation, type?: string) {\n return `${node.id}-${relation}-${type}`;\n }\n\n /**\n * Recursive depth-first traversal of the graph.\n *\n * @param options.node The node to start traversing from.\n * @param options.relation The relation to traverse graph edges.\n * @param options.visitor A callback which is called for each node visited during traversal.\n */\n traverse(\n { visitor, node = this.root, relation = 'outbound', expansion }: GraphTraversalOptions,\n path: string[] = [],\n ): void {\n // Break cycles.\n if (path.includes(node.id)) {\n return;\n }\n\n const shouldContinue = visitor(node, [...path, node.id]);\n if (shouldContinue === false) {\n return;\n }\n\n Object.values(this._getNodes({ node, relation, expansion })).forEach((child) =>\n this.traverse({ node: child, relation, visitor, expansion }, [...path, node.id]),\n );\n }\n\n /**\n * Recursive depth-first traversal of the graph wrapping each visitor call in an effect.\n *\n * @param options.node The node to start traversing from.\n * @param options.relation The relation to traverse graph edges.\n * @param options.visitor A callback which is called for each node visited during traversal.\n */\n subscribeTraverse(\n { visitor, node = this.root, relation = 'outbound', expansion }: GraphTraversalOptions,\n currentPath: string[] = [],\n ) {\n return effect(() => {\n const path = [...currentPath, node.id];\n const result = visitor(node, path);\n if (result === false) {\n return;\n }\n\n const nodes = this._getNodes({ node, relation, expansion });\n const nodeSubscriptions = nodes.map((n) => this.subscribeTraverse({ node: n, visitor, expansion }, path));\n\n return () => {\n nodeSubscriptions.forEach((unsubscribe) => unsubscribe());\n };\n });\n }\n\n /**\n * Get the path between two nodes in the graph.\n */\n getPath({ source = 'root', target }: { source?: string; target: string }): string[] | undefined {\n const start = this.findNode(source);\n if (!start) {\n return undefined;\n }\n\n let found: string[] | undefined;\n this.traverse({\n node: start,\n visitor: (node, path) => {\n if (found) {\n return false;\n }\n\n if (node.id === target) {\n found = path;\n }\n },\n });\n\n return found;\n }\n\n /**\n * Add nodes to the graph.\n *\n * @internal\n */\n _addNodes<TData = null, TProperties extends Record<string, any> = Record<string, any>>(\n nodes: NodeArg<TData, TProperties>[],\n ): Node<TData, TProperties>[] {\n return batch(() => nodes.map((node) => this._addNode(node)));\n }\n\n private _addNode<TData, TProperties extends Record<string, any> = Record<string, any>>({\n nodes,\n edges,\n ..._node\n }: NodeArg<TData, TProperties>): Node<TData, TProperties> {\n return untracked(() => {\n const existingNode = this._nodes[_node.id];\n const node = existingNode ?? this._constructNode({ data: null, properties: {}, ..._node });\n if (existingNode) {\n const { data, properties, type } = _node;\n if (data && data !== node.data) {\n node.data = data;\n }\n\n if (type !== node.type) {\n node.type = type;\n }\n\n for (const key in properties) {\n if (properties[key] !== node.properties[key]) {\n node.properties[key] = properties[key];\n }\n }\n } else {\n this._nodes[node.id] = node;\n this._edges[node.id] = create({ inbound: [], outbound: [] });\n }\n\n const trigger = this._waitingForNodes[node.id];\n if (trigger) {\n trigger.wake(node);\n delete this._waitingForNodes[node.id];\n }\n\n if (nodes) {\n nodes.forEach((subNode) => {\n this._addNode(subNode);\n this._addEdge({ source: node.id, target: subNode.id });\n });\n }\n\n if (edges) {\n edges.forEach(([id, relation]) =>\n relation === 'outbound'\n ? this._addEdge({ source: node.id, target: id })\n : this._addEdge({ source: id, target: node.id }),\n );\n }\n\n return node as unknown as Node<TData, TProperties>;\n });\n }\n\n /**\n * Remove nodes from the graph.\n *\n * @param ids The id of the node to remove.\n * @param edges Whether to remove edges connected to the node from the graph as well.\n * @internal\n */\n _removeNodes(ids: string[], edges = false) {\n batch(() => ids.forEach((id) => this._removeNode(id, edges)));\n }\n\n private _removeNode(id: string, edges = false) {\n untracked(() => {\n const node = this.findNode(id);\n if (!node) {\n return;\n }\n\n if (edges) {\n // Remove edges from connected nodes.\n this._getNodes({ node }).forEach((node) => {\n this._removeEdge({ source: id, target: node.id });\n });\n this._getNodes({ node, relation: 'inbound' }).forEach((node) => {\n this._removeEdge({ source: node.id, target: id });\n });\n\n // Remove edges from node.\n delete this._edges[id];\n }\n\n // Remove node.\n delete this._nodes[id];\n Object.keys(this._initialized)\n .filter((key) => key.startsWith(id))\n .forEach((key) => {\n delete this._initialized[key];\n });\n void this._onRemoveNode?.(id);\n });\n }\n\n /**\n * Add edges to the graph.\n *\n * @internal\n */\n _addEdges(edges: { source: string; target: string }[]) {\n batch(() => edges.forEach((edge) => this._addEdge(edge)));\n }\n\n private _addEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n if (!this._edges[source]) {\n this._edges[source] = create({ inbound: [], outbound: [] });\n }\n if (!this._edges[target]) {\n this._edges[target] = create({ inbound: [], outbound: [] });\n }\n\n const sourceEdges = this._edges[source];\n if (!sourceEdges.outbound.includes(target)) {\n sourceEdges.outbound.push(target);\n }\n\n const targetEdges = this._edges[target];\n if (!targetEdges.inbound.includes(source)) {\n targetEdges.inbound.push(source);\n }\n });\n }\n\n /**\n * Remove edges from the graph.\n * @internal\n */\n _removeEdges(edges: { source: string; target: string }[]) {\n batch(() => edges.forEach((edge) => this._removeEdge(edge)));\n }\n\n private _removeEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n batch(() => {\n const outboundIndex = this._edges[source]?.outbound.findIndex((id) => id === target);\n if (outboundIndex !== undefined && outboundIndex !== -1) {\n this._edges[source].outbound.splice(outboundIndex, 1);\n }\n\n const inboundIndex = this._edges[target]?.inbound.findIndex((id) => id === source);\n if (inboundIndex !== undefined && inboundIndex !== -1) {\n this._edges[target].inbound.splice(inboundIndex, 1);\n }\n });\n });\n }\n\n /**\n * Sort edges for a node.\n *\n * Edges not included in the sorted list are appended to the end of the list.\n *\n * @param nodeId The id of the node to sort edges for.\n * @param relation The relation of the edges from the node to sort.\n * @param edges The ordered list of edges.\n * @ignore\n */\n _sortEdges(nodeId: string, relation: Relation, edges: string[]) {\n untracked(() => {\n batch(() => {\n const current = this._edges[nodeId];\n if (current) {\n const unsorted = current[relation].filter((id) => !edges.includes(id)) ?? [];\n const sorted = edges.filter((id) => current[relation].includes(id)) ?? [];\n current[relation].splice(0, current[relation].length, ...[...sorted, ...unsorted]);\n }\n });\n });\n }\n\n private _constructNode = (node: Omit<Node, typeof graphSymbol>) => {\n return create<NodeInternal>({ ...node, [graphSymbol]: this });\n };\n\n private _getNodes({\n node,\n relation = 'outbound',\n type,\n expansion,\n }: {\n node: Node;\n relation?: Relation;\n type?: string;\n expansion?: boolean;\n }): Node[] {\n if (expansion) {\n void this.expand(node, relation, type);\n }\n\n const edges = this._edges[node.id];\n if (!edges) {\n return [];\n } else {\n return edges[relation]\n .map((id) => this._nodes[id])\n .filter(nonNullable)\n .filter((n) => !type || n.type === type);\n }\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { type MaybePromise, type MakeOptional } from '@dxos/util';\n\n/**\n * Represents a node in the graph.\n */\n// TODO(wittjosiah): Use Effect Schema.\nexport type Node<TData = any, TProperties extends Record<string, any> = Record<string, any>> = Readonly<{\n /**\n * Globally unique ID.\n */\n id: string;\n\n /**\n * Typename of the data the node represents.\n */\n type: string;\n\n /**\n * Properties of the node relevant to displaying the node.\n */\n properties: Readonly<TProperties>;\n\n /**\n * Data the node represents.\n */\n // TODO(burdon): Type system (e.g., minimally provide identifier string vs. TypedObject vs. Graph mixin type system)?\n // type field would prevent convoluted sniffing of object properties. And allow direct pass-through for ECHO TypedObjects.\n data: TData;\n}>;\n\nexport type NodeFilter<T = any, U extends Record<string, any> = Record<string, any>> = (\n node: Node<unknown, Record<string, any>>,\n connectedNode: Node,\n) => node is Node<T, U>;\n\nexport type Relation = 'outbound' | 'inbound';\n\nexport const isGraphNode = (data: unknown): data is Node =>\n data && typeof data === 'object' && 'id' in data && 'properties' in data && data.properties\n ? typeof data.properties === 'object' && 'data' in data\n : false;\n\nexport type NodeArg<TData, TProperties extends Record<string, any> = Record<string, any>> = MakeOptional<\n Node<TData, TProperties>,\n 'data' | 'properties'\n> & {\n /** Will automatically add nodes with an edge from this node to each. */\n nodes?: NodeArg<unknown>[];\n\n /** Will automatically add specified edges. */\n edges?: [string, Relation][];\n};\n\n//\n// Actions\n//\n\nexport type InvokeParams = {\n /** Node the invoked action is connected to. */\n node: Node;\n\n caller?: string;\n};\n\nexport type ActionData = (params: InvokeParams) => MaybePromise<void>;\n\nexport type Action<TProperties extends Record<string, any> = Record<string, any>> = Readonly<\n Omit<Node<ActionData, TProperties>, 'properties'> & {\n properties: Readonly<TProperties>;\n }\n>;\n\nexport const isAction = (data: unknown): data is Action =>\n isGraphNode(data) ? typeof data.data === 'function' : false;\n\nexport const actionGroupSymbol = Symbol('ActionGroup');\n\nexport type ActionGroup = Readonly<\n Omit<Node<typeof actionGroupSymbol, Record<string, any>>, 'properties'> & {\n properties: Readonly<Record<string, any>>;\n }\n>;\n\nexport const isActionGroup = (data: unknown): data is ActionGroup =>\n isGraphNode(data) ? data.data === actionGroupSymbol : false;\n\nexport type ActionLike = Action | ActionGroup;\n\nexport const isActionLike = (data: unknown): data is Action | ActionGroup => isAction(data) || isActionGroup(data);\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { type Signal, effect, signal } from '@preact/signals-core';\n// import { yieldOrContinue } from 'main-thread-scheduling';\n\nimport { type UnsubscribeCallback } from '@dxos/async';\nimport { create } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { nonNullable } from '@dxos/util';\n\nimport { ACTION_GROUP_TYPE, ACTION_TYPE, Graph } from './graph';\nimport { type Relation, type NodeArg, type Node, type ActionData, actionGroupSymbol } from './node';\n\n/**\n * Graph builder extension for adding nodes to the graph based on just the node id.\n * This is useful for creating the first node in a graph or for hydrating cached nodes with data.\n *\n * @param params.id The id of the node to resolve.\n */\nexport type ResolverExtension = (params: { id: string }) => NodeArg<any> | undefined;\n\n/**\n * Graph builder extension for adding nodes to the graph based on a connection to an existing node.\n *\n * @param params.node The existing node the returned nodes will be connected to.\n */\nexport type ConnectorExtension<T = any> = (params: { node: Node<T> }) => NodeArg<any>[] | undefined;\n\n/**\n * Constrained case of the connector extension for more easily adding actions to the graph.\n */\nexport type ActionsExtension<T = any> = (params: {\n node: Node<T>;\n}) => Omit<NodeArg<ActionData>, 'type' | 'nodes' | 'edges'>[] | undefined;\n\n/**\n * Constrained case of the connector extension for more easily adding action groups to the graph.\n */\nexport type ActionGroupsExtension<T = any> = (params: {\n node: Node<T>;\n}) => Omit<NodeArg<typeof actionGroupSymbol>, 'type' | 'data' | 'nodes' | 'edges'>[] | undefined;\n\ntype GuardedNodeType<T> = T extends (value: any) => value is infer N ? (N extends Node<infer D> ? D : unknown) : never;\n\n/**\n * A graph builder extension is used to add nodes to the graph.\n *\n * @param params.id The unique id of the extension.\n * @param params.relation The relation the graph is being expanded from the existing node.\n * @param params.type If provided, all nodes returned are expected to have this type.\n * @param params.filter A filter function to determine if an extension should act on a node.\n * @param params.resolver A function to add nodes to the graph based on just the node id.\n * @param params.connector A function to add nodes to the graph based on a connection to an existing node.\n * @param params.actions A function to add actions to the graph based on a connection to an existing node.\n * @param params.actionGroups A function to add action groups to the graph based on a connection to an existing node.\n */\nexport type CreateExtensionOptions<T = any> = {\n id: string;\n relation?: Relation;\n type?: string;\n filter?: (node: Node) => node is Node<T>;\n resolver?: ResolverExtension;\n connector?: ConnectorExtension<GuardedNodeType<CreateExtensionOptions<T>['filter']>>;\n actions?: ActionsExtension<GuardedNodeType<CreateExtensionOptions<T>['filter']>>;\n actionGroups?: ActionGroupsExtension<GuardedNodeType<CreateExtensionOptions<T>['filter']>>;\n};\n\n/**\n * Create a graph builder extension.\n */\nexport const createExtension = <T = any>(extension: CreateExtensionOptions<T>): BuilderExtension[] => {\n const { id, resolver, connector, actions, actionGroups, ...rest } = extension;\n const getId = (key: string) => `${id}/${key}`;\n return [\n resolver ? { id: getId('resolver'), resolver } : undefined,\n connector ? { ...rest, id: getId('connector'), connector } : undefined,\n actionGroups\n ? ({\n ...rest,\n id: getId('actionGroups'),\n type: ACTION_GROUP_TYPE,\n relation: 'outbound',\n connector: ({ node }) =>\n actionGroups({ node })?.map((arg) => ({ ...arg, data: actionGroupSymbol, type: ACTION_GROUP_TYPE })),\n } satisfies BuilderExtension)\n : undefined,\n actions\n ? ({\n ...rest,\n id: getId('actions'),\n type: ACTION_TYPE,\n relation: 'outbound',\n connector: ({ node }) => actions({ node })?.map((arg) => ({ ...arg, type: ACTION_TYPE })),\n } satisfies BuilderExtension)\n : undefined,\n ].filter(nonNullable);\n};\n\nexport type GraphBuilderTraverseOptions = {\n node: Node;\n relation?: Relation;\n visitor: (node: Node, path: string[]) => void;\n};\n\n/**\n * The dispatcher is used to keep track of the current extension and state when memoizing functions.\n */\nclass Dispatcher {\n currentExtension?: string;\n stateIndex = 0;\n state: Record<string, any[]> = {};\n cleanup: (() => void)[] = [];\n}\n\nclass BuilderInternal {\n // This must be static to avoid passing the dispatcher instance to every memoized function.\n // If the dispatcher is not set that means that the memoized function is being called outside of the graph builder.\n static currentDispatcher?: Dispatcher;\n}\n\n/**\n * Allows code to be memoized within the context of a graph builder extension.\n * This is useful for creating instances which should be subscribed to rather than recreated.\n */\nexport const memoize = <T>(fn: () => T, key = 'result'): T => {\n const dispatcher = BuilderInternal.currentDispatcher;\n invariant(dispatcher?.currentExtension, 'memoize must be called within an extension');\n const all = dispatcher.state[dispatcher.currentExtension][dispatcher.stateIndex] ?? {};\n const current = all[key];\n const result = current ? current.result : fn();\n dispatcher.state[dispatcher.currentExtension][dispatcher.stateIndex] = { ...all, [key]: { result } };\n dispatcher.stateIndex++;\n return result;\n};\n\n/**\n * Register a cleanup function to be called when the graph builder is destroyed.\n */\nexport const cleanup = (fn: () => void): void => {\n memoize(() => {\n const dispatcher = BuilderInternal.currentDispatcher;\n invariant(dispatcher, 'cleanup must be called within an extension');\n dispatcher.cleanup.push(fn);\n });\n};\n\n/**\n * Convert a subscribe/get pair into a signal.\n */\nexport const toSignal = <T>(\n subscribe: (onChange: () => void) => () => void,\n get: () => T | undefined,\n key?: string,\n) => {\n const thisSignal = memoize(() => {\n return signal(get());\n }, key);\n const unsubscribe = memoize(() => {\n return subscribe(() => (thisSignal.value = get()));\n }, key);\n cleanup(() => {\n unsubscribe();\n });\n return thisSignal.value;\n};\n\nexport type BuilderExtension = {\n id: string;\n resolver?: ResolverExtension;\n connector?: ConnectorExtension;\n // Only for connector.\n relation?: Relation;\n type?: string;\n filter?: (node: Node) => boolean;\n};\n\ntype ExtensionArg = BuilderExtension | BuilderExtension[] | ExtensionArg[];\n\n/**\n * The builder provides an extensible way to compose the construction of the graph.\n */\n// TODO(wittjosiah): Add api for setting subscription set and/or radius.\n// Should unsubscribe from nodes that are not in the set/radius.\n// Should track LRU nodes that are not in the set/radius and remove them beyond a certain threshold.\nexport class GraphBuilder {\n private readonly _dispatcher = new Dispatcher();\n private readonly _extensions = create<Record<string, BuilderExtension>>({});\n private readonly _resolverSubscriptions = new Map<string, UnsubscribeCallback>();\n private readonly _connectorSubscriptions = new Map<string, UnsubscribeCallback>();\n private readonly _nodeChanged: Record<string, Signal<{}>> = {};\n private _graph: Graph;\n\n constructor() {\n this._graph = new Graph({\n onInitialNode: (id) => this._onInitialNode(id),\n onInitialNodes: (node, relation, type) => this._onInitialNodes(node, relation, type),\n onRemoveNode: (id) => this._onRemoveNode(id),\n });\n }\n\n get graph() {\n return this._graph;\n }\n\n /**\n * Register a node builder which will be called in order to construct the graph.\n */\n addExtension(extension: ExtensionArg): GraphBuilder {\n if (Array.isArray(extension)) {\n extension.forEach((ext) => this.addExtension(ext));\n return this;\n }\n\n this._dispatcher.state[extension.id] = [];\n this._extensions[extension.id] = extension;\n return this;\n }\n\n /**\n * Remove a node builder from the graph builder.\n */\n removeExtension(id: string): GraphBuilder {\n delete this._extensions[id];\n return this;\n }\n\n destroy() {\n this._dispatcher.cleanup.forEach((fn) => fn());\n this._resolverSubscriptions.forEach((unsubscribe) => unsubscribe());\n this._connectorSubscriptions.forEach((unsubscribe) => unsubscribe());\n this._resolverSubscriptions.clear();\n this._connectorSubscriptions.clear();\n }\n\n /**\n * Traverse a graph using just the connector extensions, without subscribing to any signals or persisting any nodes.\n */\n // TODO(wittjosiah): Rename? This is not traversing the graph proper.\n async traverse({ node, relation = 'outbound', visitor }: GraphBuilderTraverseOptions, path: string[] = []) {\n // Break cycles.\n if (path.includes(node.id)) {\n return;\n }\n\n // TODO(wittjosiah): Failed in test environment. ESM only?\n // await yieldOrContinue('idle');\n visitor(node, [...path, node.id]);\n\n const nodes = Object.values(this._extensions)\n .filter((extension) => relation === (extension.relation ?? 'outbound'))\n .flatMap((extension) => extension.connector?.({ node }) ?? [])\n .map(\n (arg): Node => ({\n id: arg.id,\n type: arg.type,\n data: arg.data ?? null,\n properties: arg.properties ?? {},\n }),\n );\n\n await Promise.all(nodes.map((n) => this.traverse({ node: n, relation, visitor }, [...path, node.id])));\n }\n\n private async _onInitialNode(nodeId: string) {\n this._nodeChanged[nodeId] = this._nodeChanged[nodeId] ?? signal({});\n this._resolverSubscriptions.set(\n nodeId,\n effect(() => {\n for (const { id, resolver } of Object.values(this._extensions)) {\n if (!resolver) {\n continue;\n }\n this._dispatcher.currentExtension = id;\n this._dispatcher.stateIndex = 0;\n BuilderInternal.currentDispatcher = this._dispatcher;\n const node = resolver({ id: nodeId });\n BuilderInternal.currentDispatcher = undefined;\n if (node) {\n this.graph._addNodes([node]);\n if (this._nodeChanged[node.id]) {\n this._nodeChanged[node.id].value = {};\n }\n break;\n }\n }\n }),\n );\n }\n\n private async _onInitialNodes(node: Node, nodesRelation: Relation, nodesType?: string) {\n this._nodeChanged[node.id] = this._nodeChanged[node.id] ?? signal({});\n let first = true;\n let previous: string[] = [];\n this._connectorSubscriptions.set(\n node.id,\n effect(() => {\n // TODO(wittjosiah): This is a workaround for a race between the node removal and the effect re-running.\n // To cause this case to happen, remove a collection and then undo the removal.\n if (!first && !this._connectorSubscriptions.has(node.id)) {\n return;\n }\n first = false;\n\n // Subscribe to extensions being added.\n Object.keys(this._extensions);\n // Subscribe to connected node changes.\n this._nodeChanged[node.id].value;\n\n // TODO(wittjosiah): Consider allowing extensions to collaborate on the same node by merging their results.\n const nodes: NodeArg<any>[] = [];\n for (const { id, connector, filter, type, relation = 'outbound' } of Object.values(this._extensions)) {\n if (\n !connector ||\n relation !== nodesRelation ||\n (nodesType && type !== nodesType) ||\n (filter && !filter(node))\n ) {\n continue;\n }\n\n this._dispatcher.currentExtension = id;\n this._dispatcher.stateIndex = 0;\n BuilderInternal.currentDispatcher = this._dispatcher;\n nodes.push(...(connector({ node }) ?? []));\n BuilderInternal.currentDispatcher = undefined;\n }\n const ids = nodes.map((n) => n.id);\n const removed = previous.filter((id) => !ids.includes(id));\n previous = ids;\n\n this.graph._removeNodes(removed, true);\n this.graph._addNodes(nodes);\n this.graph._addEdges(\n nodes.map(({ id }) =>\n nodesRelation === 'outbound' ? { source: node.id, target: id } : { source: id, target: node.id },\n ),\n );\n this.graph._sortEdges(\n node.id,\n nodesRelation,\n nodes.map(({ id }) => id),\n );\n nodes.forEach((n) => {\n if (this._nodeChanged[n.id]) {\n this._nodeChanged[n.id].value = {};\n }\n });\n }),\n );\n }\n\n private async _onRemoveNode(nodeId: string) {\n this._resolverSubscriptions.get(nodeId)?.();\n this._connectorSubscriptions.get(nodeId)?.();\n this._resolverSubscriptions.delete(nodeId);\n this._connectorSubscriptions.delete(nodeId);\n }\n}\n"],
5
+ "mappings": ";AAIA,SAASA,OAAOC,QAAQC,iBAAiB;AAEzC,SAASC,cAAcC,eAAe;AACtC,SAA8BC,cAAc;AAC5C,SAASC,iBAAiB;AAC1B,SAASC,mBAAmB;;;ACgCrB,IAAMC,cAAc,CAACC,SAC1BA,QAAQ,OAAOA,SAAS,YAAY,QAAQA,QAAQ,gBAAgBA,QAAQA,KAAKC,aAC7E,OAAOD,KAAKC,eAAe,YAAY,UAAUD,OACjD;AAgCC,IAAME,WAAW,CAACF,SACvBD,YAAYC,IAAAA,IAAQ,OAAOA,KAAKA,SAAS,aAAa;AAEjD,IAAMG,oBAAoBC,OAAO,aAAA;AAQjC,IAAMC,gBAAgB,CAACL,SAC5BD,YAAYC,IAAAA,IAAQA,KAAKA,SAASG,oBAAoB;AAIjD,IAAMG,eAAe,CAACN,SAAgDE,SAASF,IAAAA,KAASK,cAAcL,IAAAA;;;;AD/E7G,IAAMO,cAAcC,OAAO,OAAA;AAIpB,IAAMC,WAAW,CAACC,SAAAA;AACvB,QAAMC,QAASD,KAAsBH,WAAAA;AACrCK,YAAUD,OAAO,wCAAA;;;;;;;;;AACjB,SAAOA;AACT;AAEO,IAAME,UAAU;AA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A,KAAM,CAAA,CAAE;AACxCY,wBAAgBI,oBAAoBpB;MACtC;AACA,YAAMiF,MAAMjB,MAAM3D,IAAI,CAACiE,MAAMA,EAAE9E,EAAE;AACjC,YAAM0F,UAAUJ,SAASpE,OAAO,CAAClB,OAAO,CAACyF,IAAIlB,SAASvE,EAAAA,CAAAA;AACtDsF,iBAAWG;AAEX,WAAK9B,MAAMgC,aAAaD,SAAS,IAAA;AACjC,WAAK/B,MAAMuB,UAAUV,KAAAA;AACrB,WAAKb,MAAMiC,UACTpB,MAAM3D,IAAI,CAAC,EAAEb,GAAE,MACbmF,kBAAkB,aAAa;QAAEU,QAAQjF,KAAKZ;QAAI8F,QAAQ9F;MAAG,IAAI;QAAE6F,QAAQ7F;QAAI8F,QAAQlF,KAAKZ;MAAG,CAAA,CAAA;AAGnG,WAAK2D,MAAMoC,WACTnF,KAAKZ,IACLmF,eACAX,MAAM3D,IAAI,CAAC,EAAEb,GAAE,MAAOA,EAAAA,CAAAA;AAExBwE,YAAMT,QAAQ,CAACe,MAAAA;AACb,YAAI,KAAK5B,aAAa4B,EAAE9E,EAAE,GAAG;AAC3B,eAAKkD,aAAa4B,EAAE9E,EAAE,EAAEyC,QAAQ,CAAC;QACnC;MACF,CAAA;IACF,CAAA,CAAA;EAEJ;EAEA,MAAciB,cAAcqB,QAAgB;AAC1C,SAAKhC,uBAAuBV,IAAI0C,MAAAA,IAAAA;AAChC,SAAK9B,wBAAwBZ,IAAI0C,MAAAA,IAAAA;AACjC,SAAKhC,uBAAuBiD,OAAOjB,MAAAA;AACnC,SAAK9B,wBAAwB+C,OAAOjB,MAAAA;EACtC;AACF;",
6
+ "names": ["batch", "effect", "untracked", "asyncTimeout", "Trigger", "create", "invariant", "nonNullable", "isGraphNode", "data", "properties", "isAction", "actionGroupSymbol", "Symbol", "isActionGroup", "isActionLike", "graphSymbol", "Symbol", "getGraph", "node", "graph", "invariant", "ROOT_ID", "ROOT_TYPE", "ACTION_TYPE", "ACTION_GROUP_TYPE", "Graph", "constructor", "onInitialNode", "onInitialNodes", "onRemoveNode", "_waitingForNodes", "_initialized", "_nodes", "_edges", "_constructNode", "create", "_onInitialNode", "_onInitialNodes", "_onRemoveNode", "id", "type", "properties", "data", "inbound", "outbound", "root", "findNode", "toJSON", "maxLength", "seen", "nodes", "obj", "length", "slice", "label", "map", "n", "nextSeen", "includes", "undefined", "filter", "nonNullable", "existingNode", "waitForNode", "timeout", "trigger", "Trigger", "wait", "asyncTimeout", "options", "relation", "expansion", "_getNodes", "untracked", "isActionLike", "edges", "actions", "expand", "key", "_key", "initialized", "traverse", "visitor", "path", "shouldContinue", "Object", "values", "forEach", "child", "subscribeTraverse", "currentPath", "effect", "result", "nodeSubscriptions", "unsubscribe", "getPath", "source", "target", "start", "found", "_addNodes", "batch", "_addNode", "_node", "wake", "subNode", "_addEdge", "_removeNodes", "ids", "_removeNode", "_removeEdge", "keys", "startsWith", "_addEdges", "edge", "sourceEdges", "push", "targetEdges", "_removeEdges", "outboundIndex", "findIndex", "splice", "inboundIndex", "_sortEdges", "nodeId", "current", "unsorted", "sorted", "effect", "signal", "create", "invariant", "nonNullable", "createExtension", "extension", "id", "resolver", "connector", "actions", "actionGroups", "rest", "getId", "key", "undefined", "type", "ACTION_GROUP_TYPE", "relation", "node", "map", "arg", "data", "actionGroupSymbol", "ACTION_TYPE", "filter", "nonNullable", "Dispatcher", "stateIndex", "state", "cleanup", "BuilderInternal", "memoize", "fn", "dispatcher", "currentDispatcher", "invariant", "currentExtension", "all", "current", "result", "push", "toSignal", "subscribe", "get", "thisSignal", "signal", "unsubscribe", "value", "GraphBuilder", "constructor", "_dispatcher", "_extensions", "create", "_resolverSubscriptions", "Map", "_connectorSubscriptions", "_nodeChanged", "_graph", "Graph", "onInitialNode", "_onInitialNode", "onInitialNodes", "_onInitialNodes", "onRemoveNode", "_onRemoveNode", "graph", "addExtension", "Array", "isArray", "forEach", "ext", "removeExtension", "destroy", "clear", "traverse", "visitor", "path", "includes", "nodes", "Object", "values", "flatMap", "properties", "Promise", "n", "nodeId", "set", "effect", "_addNodes", "nodesRelation", "nodesType", "first", "previous", "has", "keys", "ids", "removed", "_removeNodes", "_addEdges", "source", "target", "_sortEdges", "delete"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/sdk/app-graph/src/node.ts":{"bytes":5259,"imports":[],"format":"esm"},"packages/sdk/app-graph/src/graph.ts":{"bytes":50171,"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"packages/sdk/app-graph/src/node.ts","kind":"import-statement","original":"./node"}],"format":"esm"},"packages/sdk/app-graph/src/graph-builder.ts":{"bytes":38696,"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"packages/sdk/app-graph/src/graph.ts","kind":"import-statement","original":"./graph"},{"path":"packages/sdk/app-graph/src/node.ts","kind":"import-statement","original":"./node"}],"format":"esm"},"packages/sdk/app-graph/src/index.ts":{"bytes":672,"imports":[{"path":"packages/sdk/app-graph/src/graph.ts","kind":"import-statement","original":"./graph"},{"path":"packages/sdk/app-graph/src/graph-builder.ts","kind":"import-statement","original":"./graph-builder"},{"path":"packages/sdk/app-graph/src/node.ts","kind":"import-statement","original":"./node"}],"format":"esm"}},"outputs":{"packages/sdk/app-graph/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":47906},"packages/sdk/app-graph/dist/lib/browser/index.mjs":{"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["ACTION_GROUP_TYPE","ACTION_TYPE","Graph","GraphBuilder","ROOT_ID","ROOT_TYPE","actionGroupSymbol","cleanup","createExtension","getGraph","isAction","isActionGroup","isActionLike","isGraphNode","memoize","toSignal"],"entryPoint":"packages/sdk/app-graph/src/index.ts","inputs":{"packages/sdk/app-graph/src/graph.ts":{"bytesInOutput":12265},"packages/sdk/app-graph/src/node.ts":{"bytesInOutput":477},"packages/sdk/app-graph/src/index.ts":{"bytesInOutput":0},"packages/sdk/app-graph/src/graph-builder.ts":{"bytesInOutput":7747}},"bytes":20932}}}
1
+ {"inputs":{"packages/sdk/app-graph/src/node.ts":{"bytes":5259,"imports":[],"format":"esm"},"packages/sdk/app-graph/src/graph.ts":{"bytes":51067,"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"packages/sdk/app-graph/src/node.ts","kind":"import-statement","original":"./node"}],"format":"esm"},"packages/sdk/app-graph/src/graph-builder.ts":{"bytes":39059,"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"packages/sdk/app-graph/src/graph.ts","kind":"import-statement","original":"./graph"},{"path":"packages/sdk/app-graph/src/node.ts","kind":"import-statement","original":"./node"}],"format":"esm"},"packages/sdk/app-graph/src/index.ts":{"bytes":672,"imports":[{"path":"packages/sdk/app-graph/src/graph.ts","kind":"import-statement","original":"./graph"},{"path":"packages/sdk/app-graph/src/graph-builder.ts","kind":"import-statement","original":"./graph-builder"},{"path":"packages/sdk/app-graph/src/node.ts","kind":"import-statement","original":"./node"}],"format":"esm"}},"outputs":{"packages/sdk/app-graph/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":48553},"packages/sdk/app-graph/dist/lib/browser/index.mjs":{"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["ACTION_GROUP_TYPE","ACTION_TYPE","Graph","GraphBuilder","ROOT_ID","ROOT_TYPE","actionGroupSymbol","cleanup","createExtension","getGraph","isAction","isActionGroup","isActionLike","isGraphNode","memoize","toSignal"],"entryPoint":"packages/sdk/app-graph/src/index.ts","inputs":{"packages/sdk/app-graph/src/graph.ts":{"bytesInOutput":12485},"packages/sdk/app-graph/src/node.ts":{"bytesInOutput":477},"packages/sdk/app-graph/src/index.ts":{"bytesInOutput":0},"packages/sdk/app-graph/src/graph-builder.ts":{"bytesInOutput":7661}},"bytes":21066}}}
@@ -208,13 +208,16 @@ var Graph = class {
208
208
  ];
209
209
  }
210
210
  async expand(node, relation = "outbound", type) {
211
- const key = `${node.id}-${relation}-${type}`;
211
+ const key = this._key(node, relation, type);
212
212
  const initialized = this._initialized[key];
213
213
  if (!initialized && this._onInitialNodes) {
214
214
  await this._onInitialNodes(node, relation, type);
215
215
  this._initialized[key] = true;
216
216
  }
217
217
  }
218
+ _key(node, relation, type) {
219
+ return `${node.id}-${relation}-${type}`;
220
+ }
218
221
  /**
219
222
  * Recursive depth-first traversal of the graph.
220
223
  *
@@ -400,6 +403,9 @@ var Graph = class {
400
403
  delete this._edges[id];
401
404
  }
402
405
  delete this._nodes[id];
406
+ Object.keys(this._initialized).filter((key) => key.startsWith(id)).forEach((key) => {
407
+ delete this._initialized[key];
408
+ });
403
409
  void this._onRemoveNode?.(id);
404
410
  });
405
411
  }
@@ -666,12 +672,11 @@ var GraphBuilder = class {
666
672
  }
667
673
  async _onInitialNode(nodeId) {
668
674
  this._nodeChanged[nodeId] = this._nodeChanged[nodeId] ?? (0, import_signals_core2.signal)({});
669
- let resolved = false;
670
- for (const { id, resolver } of Object.values(this._extensions)) {
671
- if (resolved || !resolver) {
672
- continue;
673
- }
674
- const unsubscribe = (0, import_signals_core2.effect)(() => {
675
+ this._resolverSubscriptions.set(nodeId, (0, import_signals_core2.effect)(() => {
676
+ for (const { id, resolver } of Object.values(this._extensions)) {
677
+ if (!resolver) {
678
+ continue;
679
+ }
675
680
  this._dispatcher.currentExtension = id;
676
681
  this._dispatcher.stateIndex = 0;
677
682
  BuilderInternal.currentDispatcher = this._dispatcher;
@@ -680,28 +685,26 @@ var GraphBuilder = class {
680
685
  });
681
686
  BuilderInternal.currentDispatcher = void 0;
682
687
  if (node) {
683
- resolved = true;
684
688
  this.graph._addNodes([
685
689
  node
686
690
  ]);
687
691
  if (this._nodeChanged[node.id]) {
688
692
  this._nodeChanged[node.id].value = {};
689
693
  }
694
+ break;
690
695
  }
691
- });
692
- if (resolved) {
693
- this._resolverSubscriptions.get(nodeId)?.();
694
- this._resolverSubscriptions.set(nodeId, unsubscribe);
695
- break;
696
- } else {
697
- unsubscribe();
698
696
  }
699
- }
697
+ }));
700
698
  }
701
699
  async _onInitialNodes(node, nodesRelation, nodesType) {
702
700
  this._nodeChanged[node.id] = this._nodeChanged[node.id] ?? (0, import_signals_core2.signal)({});
701
+ let first = true;
703
702
  let previous = [];
704
703
  this._connectorSubscriptions.set(node.id, (0, import_signals_core2.effect)(() => {
704
+ if (!first && !this._connectorSubscriptions.has(node.id)) {
705
+ return;
706
+ }
707
+ first = false;
705
708
  Object.keys(this._extensions);
706
709
  this._nodeChanged[node.id].value;
707
710
  const nodes = [];
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/graph.ts", "../../../src/node.ts", "../../../src/graph-builder.ts"],
4
- "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { batch, effect, untracked } from '@preact/signals-core';\n\nimport { asyncTimeout, Trigger } from '@dxos/async';\nimport { type ReactiveObject, create } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { nonNullable } from '@dxos/util';\n\nimport { type Relation, type Node, type NodeArg, type NodeFilter, isActionLike } from './node';\n\nconst graphSymbol = Symbol('graph');\ntype DeepWriteable<T> = { -readonly [K in keyof T]: DeepWriteable<T[K]> };\ntype NodeInternal = DeepWriteable<Node> & { [graphSymbol]: Graph };\n\nexport const getGraph = (node: Node): Graph => {\n const graph = (node as NodeInternal)[graphSymbol];\n invariant(graph, 'Node is not associated with a graph.');\n return graph;\n};\n\nexport const ROOT_ID = 'root';\nexport const ROOT_TYPE = 'dxos.org/type/GraphRoot';\nexport const ACTION_TYPE = 'dxos.org/type/GraphAction';\nexport const ACTION_GROUP_TYPE = 'dxos.org/type/GraphActionGroup';\n\nexport type NodesOptions<T = any, U extends Record<string, any> = Record<string, any>> = {\n relation?: Relation;\n filter?: NodeFilter<T, U>;\n expansion?: boolean;\n type?: string;\n};\n\nexport type GraphTraversalOptions = {\n /**\n * A callback which is called for each node visited during traversal.\n *\n * If the callback returns `false`, traversal is stops recursing.\n */\n visitor: (node: Node, path: string[]) => boolean | void;\n\n /**\n * The node to start traversing from.\n *\n * @default root\n */\n node?: Node;\n\n /**\n * The relation to traverse graph edges.\n *\n * @default 'outbound'\n */\n relation?: Relation;\n\n /**\n * Allow traversal to trigger expansion of the graph via `onInitialNodes`.\n */\n expansion?: boolean;\n};\n\n/**\n * The Graph represents the structure of the application constructed via plugins.\n */\nexport class Graph {\n private readonly _onInitialNode?: (id: string) => Promise<void>;\n private readonly _onInitialNodes?: (node: Node, relation: Relation, type?: string) => Promise<void>;\n private readonly _onRemoveNode?: (id: string) => Promise<void>;\n\n private readonly _waitingForNodes: Record<string, Trigger<Node>> = {};\n private readonly _initialized: Record<string, boolean> = {};\n\n /**\n * @internal\n */\n readonly _nodes: Record<string, ReactiveObject<NodeInternal>> = {};\n\n /**\n * @internal\n */\n readonly _edges: Record<string, ReactiveObject<{ inbound: string[]; outbound: string[] }>> = {};\n\n constructor({\n onInitialNode,\n onInitialNodes,\n onRemoveNode,\n }: {\n onInitialNode?: Graph['_onInitialNode'];\n onInitialNodes?: Graph['_onInitialNodes'];\n onRemoveNode?: Graph['_onRemoveNode'];\n } = {}) {\n this._onInitialNode = onInitialNode;\n this._onInitialNodes = onInitialNodes;\n this._onRemoveNode = onRemoveNode;\n this._nodes[ROOT_ID] = this._constructNode({ id: ROOT_ID, type: ROOT_TYPE, properties: {}, data: null });\n this._edges[ROOT_ID] = create({ inbound: [], outbound: [] });\n }\n\n /**\n * Alias for `findNode('root')`.\n */\n get root() {\n return this.findNode(ROOT_ID)!;\n }\n\n /**\n * Convert the graph to a JSON object.\n */\n toJSON({ id = ROOT_ID, maxLength = 32 }: { id?: string; maxLength?: number } = {}) {\n const toJSON = (node: Node, seen: string[] = []): any => {\n const nodes = this.nodes(node);\n const obj: Record<string, any> = {\n id: node.id.length > maxLength ? `${node.id.slice(0, maxLength - 3)}...` : node.id,\n type: node.type,\n };\n if (node.properties.label) {\n obj.label = node.properties.label;\n }\n if (nodes.length) {\n obj.nodes = nodes\n .map((n) => {\n // Break cycles.\n const nextSeen = [...seen, node.id];\n return nextSeen.includes(n.id) ? undefined : toJSON(n, nextSeen);\n })\n .filter(nonNullable);\n }\n return obj;\n };\n\n const root = this.findNode(id);\n invariant(root, `Node not found: ${id}`);\n return toJSON(root);\n }\n\n /**\n * Find the node with the given id in the graph.\n *\n * If a node is not found within the graph and an `onInitialNode` callback is provided,\n * it is called with the id and type of the node, potentially initializing the node.\n */\n findNode(id: string): Node | undefined {\n const existingNode = this._nodes[id];\n if (!existingNode) {\n void this._onInitialNode?.(id);\n }\n\n return existingNode;\n }\n\n /**\n * Wait for a node to be added to the graph.\n *\n * If the node is already present in the graph, the promise resolves immediately.\n *\n * @param id The id of the node to wait for.\n * @param timeout The time in milliseconds to wait for the node to be added.\n */\n async waitForNode(id: string, timeout?: number): Promise<Node> {\n const trigger = this._waitingForNodes[id] ?? (this._waitingForNodes[id] = new Trigger<Node>());\n const node = this.findNode(id);\n if (node) {\n delete this._waitingForNodes[id];\n return node;\n }\n\n if (timeout === undefined) {\n return trigger.wait();\n } else {\n return asyncTimeout(trigger.wait(), timeout, `Node not found: ${id}`);\n }\n }\n\n /**\n * Nodes that this node is connected to in default order.\n */\n nodes<T = any, U extends Record<string, any> = Record<string, any>>(node: Node, options: NodesOptions<T, U> = {}) {\n const { relation, expansion, filter, type } = options;\n const nodes = this._getNodes({ node, relation, expansion, type });\n return nodes.filter((n) => untracked(() => !isActionLike(n))).filter((n) => filter?.(n, node) ?? true);\n }\n\n /**\n * Edges that this node is connected to in default order.\n */\n edges(node: Node, { relation = 'outbound' }: { relation?: Relation } = {}) {\n return this._edges[node.id]?.[relation] ?? [];\n }\n\n /**\n * Actions or action groups that this node is connected to in default order.\n */\n actions(node: Node, { expansion }: { expansion?: boolean } = {}) {\n return [\n ...this._getNodes({ node, expansion, type: ACTION_GROUP_TYPE }),\n ...this._getNodes({ node, expansion, type: ACTION_TYPE }),\n ];\n }\n\n async expand(node: Node, relation: Relation = 'outbound', type?: string) {\n // TODO(wittjosiah): Factor out helper.\n const key = `${node.id}-${relation}-${type}`;\n const initialized = this._initialized[key];\n if (!initialized && this._onInitialNodes) {\n await this._onInitialNodes(node, relation, type);\n this._initialized[key] = true;\n }\n }\n\n /**\n * Recursive depth-first traversal of the graph.\n *\n * @param options.node The node to start traversing from.\n * @param options.relation The relation to traverse graph edges.\n * @param options.visitor A callback which is called for each node visited during traversal.\n */\n traverse(\n { visitor, node = this.root, relation = 'outbound', expansion }: GraphTraversalOptions,\n path: string[] = [],\n ): void {\n // Break cycles.\n if (path.includes(node.id)) {\n return;\n }\n\n const shouldContinue = visitor(node, [...path, node.id]);\n if (shouldContinue === false) {\n return;\n }\n\n Object.values(this._getNodes({ node, relation, expansion })).forEach((child) =>\n this.traverse({ node: child, relation, visitor, expansion }, [...path, node.id]),\n );\n }\n\n /**\n * Recursive depth-first traversal of the graph wrapping each visitor call in an effect.\n *\n * @param options.node The node to start traversing from.\n * @param options.relation The relation to traverse graph edges.\n * @param options.visitor A callback which is called for each node visited during traversal.\n */\n subscribeTraverse(\n { visitor, node = this.root, relation = 'outbound', expansion }: GraphTraversalOptions,\n currentPath: string[] = [],\n ) {\n return effect(() => {\n const path = [...currentPath, node.id];\n const result = visitor(node, path);\n if (result === false) {\n return;\n }\n\n const nodes = this._getNodes({ node, relation, expansion });\n const nodeSubscriptions = nodes.map((n) => this.subscribeTraverse({ node: n, visitor, expansion }, path));\n\n return () => {\n nodeSubscriptions.forEach((unsubscribe) => unsubscribe());\n };\n });\n }\n\n /**\n * Get the path between two nodes in the graph.\n */\n getPath({ source = 'root', target }: { source?: string; target: string }): string[] | undefined {\n const start = this.findNode(source);\n if (!start) {\n return undefined;\n }\n\n let found: string[] | undefined;\n this.traverse({\n node: start,\n visitor: (node, path) => {\n if (found) {\n return false;\n }\n\n if (node.id === target) {\n found = path;\n }\n },\n });\n\n return found;\n }\n\n /**\n * Add nodes to the graph.\n *\n * @internal\n */\n _addNodes<TData = null, TProperties extends Record<string, any> = Record<string, any>>(\n nodes: NodeArg<TData, TProperties>[],\n ): Node<TData, TProperties>[] {\n return batch(() => nodes.map((node) => this._addNode(node)));\n }\n\n private _addNode<TData, TProperties extends Record<string, any> = Record<string, any>>({\n nodes,\n edges,\n ..._node\n }: NodeArg<TData, TProperties>): Node<TData, TProperties> {\n return untracked(() => {\n const existingNode = this._nodes[_node.id];\n const node = existingNode ?? this._constructNode({ data: null, properties: {}, ..._node });\n if (existingNode) {\n const { data, properties, type } = _node;\n if (data && data !== node.data) {\n node.data = data;\n }\n\n if (type !== node.type) {\n node.type = type;\n }\n\n for (const key in properties) {\n if (properties[key] !== node.properties[key]) {\n node.properties[key] = properties[key];\n }\n }\n } else {\n this._nodes[node.id] = node;\n this._edges[node.id] = create({ inbound: [], outbound: [] });\n }\n\n const trigger = this._waitingForNodes[node.id];\n if (trigger) {\n trigger.wake(node);\n delete this._waitingForNodes[node.id];\n }\n\n if (nodes) {\n nodes.forEach((subNode) => {\n this._addNode(subNode);\n this._addEdge({ source: node.id, target: subNode.id });\n });\n }\n\n if (edges) {\n edges.forEach(([id, relation]) =>\n relation === 'outbound'\n ? this._addEdge({ source: node.id, target: id })\n : this._addEdge({ source: id, target: node.id }),\n );\n }\n\n return node as unknown as Node<TData, TProperties>;\n });\n }\n\n /**\n * Remove nodes from the graph.\n *\n * @param ids The id of the node to remove.\n * @param edges Whether to remove edges connected to the node from the graph as well.\n * @internal\n */\n _removeNodes(ids: string[], edges = false) {\n batch(() => ids.forEach((id) => this._removeNode(id, edges)));\n }\n\n private _removeNode(id: string, edges = false) {\n untracked(() => {\n const node = this.findNode(id);\n if (!node) {\n return;\n }\n\n if (edges) {\n // Remove edges from connected nodes.\n this._getNodes({ node }).forEach((node) => {\n this._removeEdge({ source: id, target: node.id });\n });\n this._getNodes({ node, relation: 'inbound' }).forEach((node) => {\n this._removeEdge({ source: node.id, target: id });\n });\n\n // Remove edges from node.\n delete this._edges[id];\n }\n\n // Remove node.\n delete this._nodes[id];\n void this._onRemoveNode?.(id);\n });\n }\n\n /**\n * Add edges to the graph.\n *\n * @internal\n */\n _addEdges(edges: { source: string; target: string }[]) {\n batch(() => edges.forEach((edge) => this._addEdge(edge)));\n }\n\n private _addEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n if (!this._edges[source]) {\n this._edges[source] = create({ inbound: [], outbound: [] });\n }\n if (!this._edges[target]) {\n this._edges[target] = create({ inbound: [], outbound: [] });\n }\n\n const sourceEdges = this._edges[source];\n if (!sourceEdges.outbound.includes(target)) {\n sourceEdges.outbound.push(target);\n }\n\n const targetEdges = this._edges[target];\n if (!targetEdges.inbound.includes(source)) {\n targetEdges.inbound.push(source);\n }\n });\n }\n\n /**\n * Remove edges from the graph.\n * @internal\n */\n _removeEdges(edges: { source: string; target: string }[]) {\n batch(() => edges.forEach((edge) => this._removeEdge(edge)));\n }\n\n private _removeEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n batch(() => {\n const outboundIndex = this._edges[source]?.outbound.findIndex((id) => id === target);\n if (outboundIndex !== undefined && outboundIndex !== -1) {\n this._edges[source].outbound.splice(outboundIndex, 1);\n }\n\n const inboundIndex = this._edges[target]?.inbound.findIndex((id) => id === source);\n if (inboundIndex !== undefined && inboundIndex !== -1) {\n this._edges[target].inbound.splice(inboundIndex, 1);\n }\n });\n });\n }\n\n /**\n * Sort edges for a node.\n *\n * Edges not included in the sorted list are appended to the end of the list.\n *\n * @param nodeId The id of the node to sort edges for.\n * @param relation The relation of the edges from the node to sort.\n * @param edges The ordered list of edges.\n * @ignore\n */\n _sortEdges(nodeId: string, relation: Relation, edges: string[]) {\n untracked(() => {\n batch(() => {\n const current = this._edges[nodeId];\n if (current) {\n const unsorted = current[relation].filter((id) => !edges.includes(id)) ?? [];\n const sorted = edges.filter((id) => current[relation].includes(id)) ?? [];\n current[relation].splice(0, current[relation].length, ...[...sorted, ...unsorted]);\n }\n });\n });\n }\n\n private _constructNode = (node: Omit<Node, typeof graphSymbol>) => {\n return create<NodeInternal>({ ...node, [graphSymbol]: this });\n };\n\n private _getNodes({\n node,\n relation = 'outbound',\n type,\n expansion,\n }: {\n node: Node;\n relation?: Relation;\n type?: string;\n expansion?: boolean;\n }): Node[] {\n if (expansion) {\n void this.expand(node, relation, type);\n }\n\n const edges = this._edges[node.id];\n if (!edges) {\n return [];\n } else {\n return edges[relation]\n .map((id) => this._nodes[id])\n .filter(nonNullable)\n .filter((n) => !type || n.type === type);\n }\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { type MaybePromise, type MakeOptional } from '@dxos/util';\n\n/**\n * Represents a node in the graph.\n */\n// TODO(wittjosiah): Use Effect Schema.\nexport type Node<TData = any, TProperties extends Record<string, any> = Record<string, any>> = Readonly<{\n /**\n * Globally unique ID.\n */\n id: string;\n\n /**\n * Typename of the data the node represents.\n */\n type: string;\n\n /**\n * Properties of the node relevant to displaying the node.\n */\n properties: Readonly<TProperties>;\n\n /**\n * Data the node represents.\n */\n // TODO(burdon): Type system (e.g., minimally provide identifier string vs. TypedObject vs. Graph mixin type system)?\n // type field would prevent convoluted sniffing of object properties. And allow direct pass-through for ECHO TypedObjects.\n data: TData;\n}>;\n\nexport type NodeFilter<T = any, U extends Record<string, any> = Record<string, any>> = (\n node: Node<unknown, Record<string, any>>,\n connectedNode: Node,\n) => node is Node<T, U>;\n\nexport type Relation = 'outbound' | 'inbound';\n\nexport const isGraphNode = (data: unknown): data is Node =>\n data && typeof data === 'object' && 'id' in data && 'properties' in data && data.properties\n ? typeof data.properties === 'object' && 'data' in data\n : false;\n\nexport type NodeArg<TData, TProperties extends Record<string, any> = Record<string, any>> = MakeOptional<\n Node<TData, TProperties>,\n 'data' | 'properties'\n> & {\n /** Will automatically add nodes with an edge from this node to each. */\n nodes?: NodeArg<unknown>[];\n\n /** Will automatically add specified edges. */\n edges?: [string, Relation][];\n};\n\n//\n// Actions\n//\n\nexport type InvokeParams = {\n /** Node the invoked action is connected to. */\n node: Node;\n\n caller?: string;\n};\n\nexport type ActionData = (params: InvokeParams) => MaybePromise<void>;\n\nexport type Action<TProperties extends Record<string, any> = Record<string, any>> = Readonly<\n Omit<Node<ActionData, TProperties>, 'properties'> & {\n properties: Readonly<TProperties>;\n }\n>;\n\nexport const isAction = (data: unknown): data is Action =>\n isGraphNode(data) ? typeof data.data === 'function' : false;\n\nexport const actionGroupSymbol = Symbol('ActionGroup');\n\nexport type ActionGroup = Readonly<\n Omit<Node<typeof actionGroupSymbol, Record<string, any>>, 'properties'> & {\n properties: Readonly<Record<string, any>>;\n }\n>;\n\nexport const isActionGroup = (data: unknown): data is ActionGroup =>\n isGraphNode(data) ? data.data === actionGroupSymbol : false;\n\nexport type ActionLike = Action | ActionGroup;\n\nexport const isActionLike = (data: unknown): data is Action | ActionGroup => isAction(data) || isActionGroup(data);\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { type Signal, effect, signal } from '@preact/signals-core';\n// import { yieldOrContinue } from 'main-thread-scheduling';\n\nimport { type UnsubscribeCallback } from '@dxos/async';\nimport { create } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { nonNullable } from '@dxos/util';\n\nimport { ACTION_GROUP_TYPE, ACTION_TYPE, Graph } from './graph';\nimport { type Relation, type NodeArg, type Node, type ActionData, actionGroupSymbol } from './node';\n\n/**\n * Graph builder extension for adding nodes to the graph based on just the node id.\n * This is useful for creating the first node in a graph or for hydrating cached nodes with data.\n *\n * @param params.id The id of the node to resolve.\n */\nexport type ResolverExtension = (params: { id: string }) => NodeArg<any> | undefined;\n\n/**\n * Graph builder extension for adding nodes to the graph based on a connection to an existing node.\n *\n * @param params.node The existing node the returned nodes will be connected to.\n */\nexport type ConnectorExtension<T = any> = (params: { node: Node<T> }) => NodeArg<any>[] | undefined;\n\n/**\n * Constrained case of the connector extension for more easily adding actions to the graph.\n */\nexport type ActionsExtension<T = any> = (params: {\n node: Node<T>;\n}) => Omit<NodeArg<ActionData>, 'type' | 'nodes' | 'edges'>[] | undefined;\n\n/**\n * Constrained case of the connector extension for more easily adding action groups to the graph.\n */\nexport type ActionGroupsExtension<T = any> = (params: {\n node: Node<T>;\n}) => Omit<NodeArg<typeof actionGroupSymbol>, 'type' | 'data' | 'nodes' | 'edges'>[] | undefined;\n\ntype GuardedNodeType<T> = T extends (value: any) => value is infer N ? (N extends Node<infer D> ? D : unknown) : never;\n\n/**\n * A graph builder extension is used to add nodes to the graph.\n *\n * @param params.id The unique id of the extension.\n * @param params.relation The relation the graph is being expanded from the existing node.\n * @param params.type If provided, all nodes returned are expected to have this type.\n * @param params.filter A filter function to determine if an extension should act on a node.\n * @param params.resolver A function to add nodes to the graph based on just the node id.\n * @param params.connector A function to add nodes to the graph based on a connection to an existing node.\n * @param params.actions A function to add actions to the graph based on a connection to an existing node.\n * @param params.actionGroups A function to add action groups to the graph based on a connection to an existing node.\n */\nexport type CreateExtensionOptions<T = any> = {\n id: string;\n relation?: Relation;\n type?: string;\n filter?: (node: Node) => node is Node<T>;\n resolver?: ResolverExtension;\n connector?: ConnectorExtension<GuardedNodeType<CreateExtensionOptions<T>['filter']>>;\n actions?: ActionsExtension<GuardedNodeType<CreateExtensionOptions<T>['filter']>>;\n actionGroups?: ActionGroupsExtension<GuardedNodeType<CreateExtensionOptions<T>['filter']>>;\n};\n\n/**\n * Create a graph builder extension.\n */\nexport const createExtension = <T = any>(extension: CreateExtensionOptions<T>): BuilderExtension[] => {\n const { id, resolver, connector, actions, actionGroups, ...rest } = extension;\n const getId = (key: string) => `${id}/${key}`;\n return [\n resolver ? { id: getId('resolver'), resolver } : undefined,\n connector ? { ...rest, id: getId('connector'), connector } : undefined,\n actionGroups\n ? ({\n ...rest,\n id: getId('actionGroups'),\n type: ACTION_GROUP_TYPE,\n relation: 'outbound',\n connector: ({ node }) =>\n actionGroups({ node })?.map((arg) => ({ ...arg, data: actionGroupSymbol, type: ACTION_GROUP_TYPE })),\n } satisfies BuilderExtension)\n : undefined,\n actions\n ? ({\n ...rest,\n id: getId('actions'),\n type: ACTION_TYPE,\n relation: 'outbound',\n connector: ({ node }) => actions({ node })?.map((arg) => ({ ...arg, type: ACTION_TYPE })),\n } satisfies BuilderExtension)\n : undefined,\n ].filter(nonNullable);\n};\n\nexport type GraphBuilderTraverseOptions = {\n node: Node;\n relation?: Relation;\n visitor: (node: Node, path: string[]) => void;\n};\n\n/**\n * The dispatcher is used to keep track of the current extension and state when memoizing functions.\n */\nclass Dispatcher {\n currentExtension?: string;\n stateIndex = 0;\n state: Record<string, any[]> = {};\n cleanup: (() => void)[] = [];\n}\n\nclass BuilderInternal {\n // This must be static to avoid passing the dispatcher instance to every memoized function.\n // If the dispatcher is not set that means that the memoized function is being called outside of the graph builder.\n static currentDispatcher?: Dispatcher;\n}\n\n/**\n * Allows code to be memoized within the context of a graph builder extension.\n * This is useful for creating instances which should be subscribed to rather than recreated.\n */\nexport const memoize = <T>(fn: () => T, key = 'result'): T => {\n const dispatcher = BuilderInternal.currentDispatcher;\n invariant(dispatcher?.currentExtension, 'memoize must be called within an extension');\n const all = dispatcher.state[dispatcher.currentExtension][dispatcher.stateIndex] ?? {};\n const current = all[key];\n const result = current ? current.result : fn();\n dispatcher.state[dispatcher.currentExtension][dispatcher.stateIndex] = { ...all, [key]: { result } };\n dispatcher.stateIndex++;\n return result;\n};\n\n/**\n * Register a cleanup function to be called when the graph builder is destroyed.\n */\nexport const cleanup = (fn: () => void): void => {\n memoize(() => {\n const dispatcher = BuilderInternal.currentDispatcher;\n invariant(dispatcher, 'cleanup must be called within an extension');\n dispatcher.cleanup.push(fn);\n });\n};\n\n/**\n * Convert a subscribe/get pair into a signal.\n */\nexport const toSignal = <T>(\n subscribe: (onChange: () => void) => () => void,\n get: () => T | undefined,\n key?: string,\n) => {\n const thisSignal = memoize(() => {\n return signal(get());\n }, key);\n const unsubscribe = memoize(() => {\n return subscribe(() => (thisSignal.value = get()));\n }, key);\n cleanup(() => {\n unsubscribe();\n });\n return thisSignal.value;\n};\n\nexport type BuilderExtension = {\n id: string;\n resolver?: ResolverExtension;\n connector?: ConnectorExtension;\n // Only for connector.\n relation?: Relation;\n type?: string;\n filter?: (node: Node) => boolean;\n};\n\ntype ExtensionArg = BuilderExtension | BuilderExtension[] | ExtensionArg[];\n\n/**\n * The builder provides an extensible way to compose the construction of the graph.\n */\n// TODO(wittjosiah): Add api for setting subscription set and/or radius.\n// Should unsubscribe from nodes that are not in the set/radius.\n// Should track LRU nodes that are not in the set/radius and remove them beyond a certain threshold.\nexport class GraphBuilder {\n private readonly _dispatcher = new Dispatcher();\n private readonly _extensions = create<Record<string, BuilderExtension>>({});\n private readonly _resolverSubscriptions = new Map<string, UnsubscribeCallback>();\n private readonly _connectorSubscriptions = new Map<string, UnsubscribeCallback>();\n private readonly _nodeChanged: Record<string, Signal<{}>> = {};\n private _graph: Graph;\n\n constructor() {\n this._graph = new Graph({\n onInitialNode: (id) => this._onInitialNode(id),\n onInitialNodes: (node, relation, type) => this._onInitialNodes(node, relation, type),\n onRemoveNode: (id) => this._onRemoveNode(id),\n });\n }\n\n get graph() {\n return this._graph;\n }\n\n /**\n * Register a node builder which will be called in order to construct the graph.\n */\n addExtension(extension: ExtensionArg): GraphBuilder {\n if (Array.isArray(extension)) {\n extension.forEach((ext) => this.addExtension(ext));\n return this;\n }\n\n this._dispatcher.state[extension.id] = [];\n this._extensions[extension.id] = extension;\n return this;\n }\n\n /**\n * Remove a node builder from the graph builder.\n */\n removeExtension(id: string): GraphBuilder {\n delete this._extensions[id];\n return this;\n }\n\n destroy() {\n this._dispatcher.cleanup.forEach((fn) => fn());\n this._resolverSubscriptions.forEach((unsubscribe) => unsubscribe());\n this._connectorSubscriptions.forEach((unsubscribe) => unsubscribe());\n this._resolverSubscriptions.clear();\n this._connectorSubscriptions.clear();\n }\n\n /**\n * Traverse a graph using just the connector extensions, without subscribing to any signals or persisting any nodes.\n */\n // TODO(wittjosiah): Rename? This is not traversing the graph proper.\n async traverse({ node, relation = 'outbound', visitor }: GraphBuilderTraverseOptions, path: string[] = []) {\n // Break cycles.\n if (path.includes(node.id)) {\n return;\n }\n\n // TODO(wittjosiah): Failed in test environment. ESM only?\n // await yieldOrContinue('idle');\n visitor(node, [...path, node.id]);\n\n const nodes = Object.values(this._extensions)\n .filter((extension) => relation === (extension.relation ?? 'outbound'))\n .flatMap((extension) => extension.connector?.({ node }) ?? [])\n .map(\n (arg): Node => ({\n id: arg.id,\n type: arg.type,\n data: arg.data ?? null,\n properties: arg.properties ?? {},\n }),\n );\n\n await Promise.all(nodes.map((n) => this.traverse({ node: n, relation, visitor }, [...path, node.id])));\n }\n\n private async _onInitialNode(nodeId: string) {\n this._nodeChanged[nodeId] = this._nodeChanged[nodeId] ?? signal({});\n let resolved = false;\n for (const { id, resolver } of Object.values(this._extensions)) {\n if (resolved || !resolver) {\n continue;\n }\n\n const unsubscribe = effect(() => {\n this._dispatcher.currentExtension = id;\n this._dispatcher.stateIndex = 0;\n BuilderInternal.currentDispatcher = this._dispatcher;\n const node = resolver({ id: nodeId });\n BuilderInternal.currentDispatcher = undefined;\n if (node) {\n resolved = true;\n this.graph._addNodes([node]);\n if (this._nodeChanged[node.id]) {\n this._nodeChanged[node.id].value = {};\n }\n }\n });\n\n if (resolved) {\n this._resolverSubscriptions.get(nodeId)?.();\n this._resolverSubscriptions.set(nodeId, unsubscribe);\n break;\n } else {\n unsubscribe();\n }\n }\n }\n\n private async _onInitialNodes(node: Node, nodesRelation: Relation, nodesType?: string) {\n this._nodeChanged[node.id] = this._nodeChanged[node.id] ?? signal({});\n let previous: string[] = [];\n this._connectorSubscriptions.set(\n node.id,\n effect(() => {\n // Subscribe to extensions being added.\n Object.keys(this._extensions);\n // Subscribe to connected node changes.\n this._nodeChanged[node.id].value;\n\n // TODO(wittjosiah): Consider allowing extensions to collaborate on the same node by merging their results.\n const nodes: NodeArg<any>[] = [];\n for (const { id, connector, filter, type, relation = 'outbound' } of Object.values(this._extensions)) {\n if (\n !connector ||\n relation !== nodesRelation ||\n (nodesType && type !== nodesType) ||\n (filter && !filter(node))\n ) {\n continue;\n }\n\n this._dispatcher.currentExtension = id;\n this._dispatcher.stateIndex = 0;\n BuilderInternal.currentDispatcher = this._dispatcher;\n nodes.push(...(connector({ node }) ?? []));\n BuilderInternal.currentDispatcher = undefined;\n }\n const ids = nodes.map((n) => n.id);\n const removed = previous.filter((id) => !ids.includes(id));\n previous = ids;\n\n this.graph._removeNodes(removed, true);\n this.graph._addNodes(nodes);\n this.graph._addEdges(\n nodes.map(({ id }) =>\n nodesRelation === 'outbound' ? { source: node.id, target: id } : { source: id, target: node.id },\n ),\n );\n this.graph._sortEdges(\n node.id,\n nodesRelation,\n nodes.map(({ id }) => id),\n );\n nodes.forEach((n) => {\n if (this._nodeChanged[n.id]) {\n this._nodeChanged[n.id].value = {};\n }\n });\n }),\n );\n }\n\n private async _onRemoveNode(nodeId: string) {\n this._resolverSubscriptions.get(nodeId)?.();\n this._connectorSubscriptions.get(nodeId)?.();\n this._resolverSubscriptions.delete(nodeId);\n this._connectorSubscriptions.delete(nodeId);\n }\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,0BAAyC;AAEzC,mBAAsC;AACtC,yBAA4C;AAC5C,uBAA0B;AAC1B,kBAA4B;AEL5B,IAAAA,uBAA4C;AAI5C,IAAAC,sBAAuB;AACvB,IAAAC,oBAA0B;AAC1B,IAAAC,eAA4B;AD+BrB,IAAMC,cAAc,CAACC,SAC1BA,QAAQ,OAAOA,SAAS,YAAY,QAAQA,QAAQ,gBAAgBA,QAAQA,KAAKC,aAC7E,OAAOD,KAAKC,eAAe,YAAY,UAAUD,OACjD;AAgCC,IAAME,WAAW,CAACF,SACvBD,YAAYC,IAAAA,IAAQ,OAAOA,KAAKA,SAAS,aAAa;AAEjD,IAAMG,oBAAoBC,OAAO,aAAA;AAQjC,IAAMC,gBAAgB,CAACL,SAC5BD,YAAYC,IAAAA,IAAQA,KAAKA,SAASG,oBAAoB;AAIjD,IAAMG,eAAe,CAACN,SAAgDE,SAASF,IAAAA,KAASK,cAAcL,IAAAA;;AD/E7G,IAAMO,cAAcH,OAAO,OAAA;AAIpB,IAAMI,WAAW,CAACC,SAAAA;AACvB,QAAMC,QAASD,KAAsBF,WAAAA;AACrCI,kCAAUD,OAAO,wCAAA;;;;;;;;;AACjB,SAAOA;AACT;AAEO,IAAME,UAAU;AAChB,IAAMC,YAAY;AAClB,IAAMC,cAAc;AACpB,IAAMC,oBAA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AA,CAAAA;AACtDkI,iBAAWhE;AAEX,WAAKrF,MAAMoF,aAAamE,SAAS,IAAA;AACjC,WAAKvJ,MAAM6E,UAAUhD,KAAAA;AACrB,WAAK7B,MAAMwF,UACT3D,MAAMK,IAAI,CAAC,EAAEf,GAAE,MACbgI,kBAAkB,aAAa;QAAE1E,QAAQ1E,KAAKoB;QAAIuD,QAAQvD;MAAG,IAAI;QAAEsD,QAAQtD;QAAIuD,QAAQ3E,KAAKoB;MAAG,CAAA,CAAA;AAGnG,WAAKnB,MAAMkG,WACTnG,KAAKoB,IACLgI,eACAtH,MAAMK,IAAI,CAAC,EAAEf,GAAE,MAAOA,EAAAA,CAAAA;AAExBU,YAAMmC,QAAQ,CAAC7B,MAAAA;AACb,YAAI,KAAKmG,aAAanG,EAAEhB,EAAE,GAAG;AAC3B,eAAKmH,aAAanG,EAAEhB,EAAE,EAAE4G,QAAQ,CAAC;QACnC;MACF,CAAA;IACF,CAAA,CAAA;EAEJ;EAEA,MAAc9G,cAAckF,QAAgB;AAC1C,SAAKgC,uBAAuBP,IAAIzB,MAAAA,IAAAA;AAChC,SAAKkC,wBAAwBT,IAAIzB,MAAAA,IAAAA;AACjC,SAAKgC,uBAAuBqB,OAAOrD,MAAAA;AACnC,SAAKkC,wBAAwBmB,OAAOrD,MAAAA;EACtC;AACF;",
6
- "names": ["import_signals_core", "import_echo_schema", "import_invariant", "import_util", "isGraphNode", "data", "properties", "isAction", "actionGroupSymbol", "Symbol", "isActionGroup", "isActionLike", "graphSymbol", "getGraph", "node", "graph", "invariant", "ROOT_ID", "ROOT_TYPE", "ACTION_TYPE", "ACTION_GROUP_TYPE", "Graph", "constructor", "onInitialNode", "onInitialNodes", "onRemoveNode", "_waitingForNodes", "_initialized", "_constructNode", "create", "_onInitialNode", "_onInitialNodes", "_onRemoveNode", "_nodes", "id", "type", "_edges", "inbound", "outbound", "root", "findNode", "toJSON", "maxLength", "seen", "nodes", "obj", "length", "slice", "label", "map", "n", "nextSeen", "includes", "undefined", "filter", "nonNullable", "existingNode", "waitForNode", "timeout", "trigger", "Trigger", "wait", "asyncTimeout", "options", "relation", "expansion", "_getNodes", "untracked", "edges", "actions", "expand", "key", "initialized", "traverse", "visitor", "path", "shouldContinue", "Object", "values", "forEach", "child", "subscribeTraverse", "currentPath", "effect", "result", "nodeSubscriptions", "unsubscribe", "getPath", "source", "target", "start", "found", "_addNodes", "batch", "_addNode", "_node", "wake", "subNode", "_addEdge", "_removeNodes", "ids", "_removeNode", "_removeEdge", "_addEdges", "edge", "sourceEdges", "push", "targetEdges", "_removeEdges", "outboundIndex", "findIndex", "splice", "inboundIndex", "_sortEdges", "nodeId", "current", "unsorted", "sorted", "createExtension", "extension", "resolver", "connector", "actionGroups", "rest", "getId", "arg", "Dispatcher", "stateIndex", "state", "cleanup", "BuilderInternal", "memoize", "fn", "dispatcher", "currentDispatcher", "currentExtension", "all", "toSignal", "subscribe", "get", "thisSignal", "signal", "value", "GraphBuilder", "_dispatcher", "_extensions", "_resolverSubscriptions", "Map", "_connectorSubscriptions", "_nodeChanged", "_graph", "addExtension", "Array", "isArray", "ext", "removeExtension", "destroy", "clear", "flatMap", "Promise", "resolved", "set", "nodesRelation", "nodesType", "previous", "keys", "removed", "delete"]
4
+ "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { batch, effect, untracked } from '@preact/signals-core';\n\nimport { asyncTimeout, Trigger } from '@dxos/async';\nimport { type ReactiveObject, create } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { nonNullable } from '@dxos/util';\n\nimport { type Relation, type Node, type NodeArg, type NodeFilter, isActionLike } from './node';\n\nconst graphSymbol = Symbol('graph');\ntype DeepWriteable<T> = { -readonly [K in keyof T]: DeepWriteable<T[K]> };\ntype NodeInternal = DeepWriteable<Node> & { [graphSymbol]: Graph };\n\nexport const getGraph = (node: Node): Graph => {\n const graph = (node as NodeInternal)[graphSymbol];\n invariant(graph, 'Node is not associated with a graph.');\n return graph;\n};\n\nexport const ROOT_ID = 'root';\nexport const ROOT_TYPE = 'dxos.org/type/GraphRoot';\nexport const ACTION_TYPE = 'dxos.org/type/GraphAction';\nexport const ACTION_GROUP_TYPE = 'dxos.org/type/GraphActionGroup';\n\nexport type NodesOptions<T = any, U extends Record<string, any> = Record<string, any>> = {\n relation?: Relation;\n filter?: NodeFilter<T, U>;\n expansion?: boolean;\n type?: string;\n};\n\nexport type GraphTraversalOptions = {\n /**\n * A callback which is called for each node visited during traversal.\n *\n * If the callback returns `false`, traversal is stops recursing.\n */\n visitor: (node: Node, path: string[]) => boolean | void;\n\n /**\n * The node to start traversing from.\n *\n * @default root\n */\n node?: Node;\n\n /**\n * The relation to traverse graph edges.\n *\n * @default 'outbound'\n */\n relation?: Relation;\n\n /**\n * Allow traversal to trigger expansion of the graph via `onInitialNodes`.\n */\n expansion?: boolean;\n};\n\n/**\n * The Graph represents the structure of the application constructed via plugins.\n */\nexport class Graph {\n private readonly _onInitialNode?: (id: string) => Promise<void>;\n private readonly _onInitialNodes?: (node: Node, relation: Relation, type?: string) => Promise<void>;\n private readonly _onRemoveNode?: (id: string) => Promise<void>;\n\n private readonly _waitingForNodes: Record<string, Trigger<Node>> = {};\n private readonly _initialized: Record<string, boolean> = {};\n\n /**\n * @internal\n */\n readonly _nodes: Record<string, ReactiveObject<NodeInternal>> = {};\n\n /**\n * @internal\n */\n readonly _edges: Record<string, ReactiveObject<{ inbound: string[]; outbound: string[] }>> = {};\n\n constructor({\n onInitialNode,\n onInitialNodes,\n onRemoveNode,\n }: {\n onInitialNode?: Graph['_onInitialNode'];\n onInitialNodes?: Graph['_onInitialNodes'];\n onRemoveNode?: Graph['_onRemoveNode'];\n } = {}) {\n this._onInitialNode = onInitialNode;\n this._onInitialNodes = onInitialNodes;\n this._onRemoveNode = onRemoveNode;\n this._nodes[ROOT_ID] = this._constructNode({ id: ROOT_ID, type: ROOT_TYPE, properties: {}, data: null });\n this._edges[ROOT_ID] = create({ inbound: [], outbound: [] });\n }\n\n /**\n * Alias for `findNode('root')`.\n */\n get root() {\n return this.findNode(ROOT_ID)!;\n }\n\n /**\n * Convert the graph to a JSON object.\n */\n toJSON({ id = ROOT_ID, maxLength = 32 }: { id?: string; maxLength?: number } = {}) {\n const toJSON = (node: Node, seen: string[] = []): any => {\n const nodes = this.nodes(node);\n const obj: Record<string, any> = {\n id: node.id.length > maxLength ? `${node.id.slice(0, maxLength - 3)}...` : node.id,\n type: node.type,\n };\n if (node.properties.label) {\n obj.label = node.properties.label;\n }\n if (nodes.length) {\n obj.nodes = nodes\n .map((n) => {\n // Break cycles.\n const nextSeen = [...seen, node.id];\n return nextSeen.includes(n.id) ? undefined : toJSON(n, nextSeen);\n })\n .filter(nonNullable);\n }\n return obj;\n };\n\n const root = this.findNode(id);\n invariant(root, `Node not found: ${id}`);\n return toJSON(root);\n }\n\n /**\n * Find the node with the given id in the graph.\n *\n * If a node is not found within the graph and an `onInitialNode` callback is provided,\n * it is called with the id and type of the node, potentially initializing the node.\n */\n findNode(id: string): Node | undefined {\n const existingNode = this._nodes[id];\n if (!existingNode) {\n void this._onInitialNode?.(id);\n }\n\n return existingNode;\n }\n\n /**\n * Wait for a node to be added to the graph.\n *\n * If the node is already present in the graph, the promise resolves immediately.\n *\n * @param id The id of the node to wait for.\n * @param timeout The time in milliseconds to wait for the node to be added.\n */\n async waitForNode(id: string, timeout?: number): Promise<Node> {\n const trigger = this._waitingForNodes[id] ?? (this._waitingForNodes[id] = new Trigger<Node>());\n const node = this.findNode(id);\n if (node) {\n delete this._waitingForNodes[id];\n return node;\n }\n\n if (timeout === undefined) {\n return trigger.wait();\n } else {\n return asyncTimeout(trigger.wait(), timeout, `Node not found: ${id}`);\n }\n }\n\n /**\n * Nodes that this node is connected to in default order.\n */\n nodes<T = any, U extends Record<string, any> = Record<string, any>>(node: Node, options: NodesOptions<T, U> = {}) {\n const { relation, expansion, filter, type } = options;\n const nodes = this._getNodes({ node, relation, expansion, type });\n return nodes.filter((n) => untracked(() => !isActionLike(n))).filter((n) => filter?.(n, node) ?? true);\n }\n\n /**\n * Edges that this node is connected to in default order.\n */\n edges(node: Node, { relation = 'outbound' }: { relation?: Relation } = {}) {\n return this._edges[node.id]?.[relation] ?? [];\n }\n\n /**\n * Actions or action groups that this node is connected to in default order.\n */\n actions(node: Node, { expansion }: { expansion?: boolean } = {}) {\n return [\n ...this._getNodes({ node, expansion, type: ACTION_GROUP_TYPE }),\n ...this._getNodes({ node, expansion, type: ACTION_TYPE }),\n ];\n }\n\n async expand(node: Node, relation: Relation = 'outbound', type?: string) {\n const key = this._key(node, relation, type);\n const initialized = this._initialized[key];\n if (!initialized && this._onInitialNodes) {\n await this._onInitialNodes(node, relation, type);\n this._initialized[key] = true;\n }\n }\n\n private _key(node: Node, relation: Relation, type?: string) {\n return `${node.id}-${relation}-${type}`;\n }\n\n /**\n * Recursive depth-first traversal of the graph.\n *\n * @param options.node The node to start traversing from.\n * @param options.relation The relation to traverse graph edges.\n * @param options.visitor A callback which is called for each node visited during traversal.\n */\n traverse(\n { visitor, node = this.root, relation = 'outbound', expansion }: GraphTraversalOptions,\n path: string[] = [],\n ): void {\n // Break cycles.\n if (path.includes(node.id)) {\n return;\n }\n\n const shouldContinue = visitor(node, [...path, node.id]);\n if (shouldContinue === false) {\n return;\n }\n\n Object.values(this._getNodes({ node, relation, expansion })).forEach((child) =>\n this.traverse({ node: child, relation, visitor, expansion }, [...path, node.id]),\n );\n }\n\n /**\n * Recursive depth-first traversal of the graph wrapping each visitor call in an effect.\n *\n * @param options.node The node to start traversing from.\n * @param options.relation The relation to traverse graph edges.\n * @param options.visitor A callback which is called for each node visited during traversal.\n */\n subscribeTraverse(\n { visitor, node = this.root, relation = 'outbound', expansion }: GraphTraversalOptions,\n currentPath: string[] = [],\n ) {\n return effect(() => {\n const path = [...currentPath, node.id];\n const result = visitor(node, path);\n if (result === false) {\n return;\n }\n\n const nodes = this._getNodes({ node, relation, expansion });\n const nodeSubscriptions = nodes.map((n) => this.subscribeTraverse({ node: n, visitor, expansion }, path));\n\n return () => {\n nodeSubscriptions.forEach((unsubscribe) => unsubscribe());\n };\n });\n }\n\n /**\n * Get the path between two nodes in the graph.\n */\n getPath({ source = 'root', target }: { source?: string; target: string }): string[] | undefined {\n const start = this.findNode(source);\n if (!start) {\n return undefined;\n }\n\n let found: string[] | undefined;\n this.traverse({\n node: start,\n visitor: (node, path) => {\n if (found) {\n return false;\n }\n\n if (node.id === target) {\n found = path;\n }\n },\n });\n\n return found;\n }\n\n /**\n * Add nodes to the graph.\n *\n * @internal\n */\n _addNodes<TData = null, TProperties extends Record<string, any> = Record<string, any>>(\n nodes: NodeArg<TData, TProperties>[],\n ): Node<TData, TProperties>[] {\n return batch(() => nodes.map((node) => this._addNode(node)));\n }\n\n private _addNode<TData, TProperties extends Record<string, any> = Record<string, any>>({\n nodes,\n edges,\n ..._node\n }: NodeArg<TData, TProperties>): Node<TData, TProperties> {\n return untracked(() => {\n const existingNode = this._nodes[_node.id];\n const node = existingNode ?? this._constructNode({ data: null, properties: {}, ..._node });\n if (existingNode) {\n const { data, properties, type } = _node;\n if (data && data !== node.data) {\n node.data = data;\n }\n\n if (type !== node.type) {\n node.type = type;\n }\n\n for (const key in properties) {\n if (properties[key] !== node.properties[key]) {\n node.properties[key] = properties[key];\n }\n }\n } else {\n this._nodes[node.id] = node;\n this._edges[node.id] = create({ inbound: [], outbound: [] });\n }\n\n const trigger = this._waitingForNodes[node.id];\n if (trigger) {\n trigger.wake(node);\n delete this._waitingForNodes[node.id];\n }\n\n if (nodes) {\n nodes.forEach((subNode) => {\n this._addNode(subNode);\n this._addEdge({ source: node.id, target: subNode.id });\n });\n }\n\n if (edges) {\n edges.forEach(([id, relation]) =>\n relation === 'outbound'\n ? this._addEdge({ source: node.id, target: id })\n : this._addEdge({ source: id, target: node.id }),\n );\n }\n\n return node as unknown as Node<TData, TProperties>;\n });\n }\n\n /**\n * Remove nodes from the graph.\n *\n * @param ids The id of the node to remove.\n * @param edges Whether to remove edges connected to the node from the graph as well.\n * @internal\n */\n _removeNodes(ids: string[], edges = false) {\n batch(() => ids.forEach((id) => this._removeNode(id, edges)));\n }\n\n private _removeNode(id: string, edges = false) {\n untracked(() => {\n const node = this.findNode(id);\n if (!node) {\n return;\n }\n\n if (edges) {\n // Remove edges from connected nodes.\n this._getNodes({ node }).forEach((node) => {\n this._removeEdge({ source: id, target: node.id });\n });\n this._getNodes({ node, relation: 'inbound' }).forEach((node) => {\n this._removeEdge({ source: node.id, target: id });\n });\n\n // Remove edges from node.\n delete this._edges[id];\n }\n\n // Remove node.\n delete this._nodes[id];\n Object.keys(this._initialized)\n .filter((key) => key.startsWith(id))\n .forEach((key) => {\n delete this._initialized[key];\n });\n void this._onRemoveNode?.(id);\n });\n }\n\n /**\n * Add edges to the graph.\n *\n * @internal\n */\n _addEdges(edges: { source: string; target: string }[]) {\n batch(() => edges.forEach((edge) => this._addEdge(edge)));\n }\n\n private _addEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n if (!this._edges[source]) {\n this._edges[source] = create({ inbound: [], outbound: [] });\n }\n if (!this._edges[target]) {\n this._edges[target] = create({ inbound: [], outbound: [] });\n }\n\n const sourceEdges = this._edges[source];\n if (!sourceEdges.outbound.includes(target)) {\n sourceEdges.outbound.push(target);\n }\n\n const targetEdges = this._edges[target];\n if (!targetEdges.inbound.includes(source)) {\n targetEdges.inbound.push(source);\n }\n });\n }\n\n /**\n * Remove edges from the graph.\n * @internal\n */\n _removeEdges(edges: { source: string; target: string }[]) {\n batch(() => edges.forEach((edge) => this._removeEdge(edge)));\n }\n\n private _removeEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n batch(() => {\n const outboundIndex = this._edges[source]?.outbound.findIndex((id) => id === target);\n if (outboundIndex !== undefined && outboundIndex !== -1) {\n this._edges[source].outbound.splice(outboundIndex, 1);\n }\n\n const inboundIndex = this._edges[target]?.inbound.findIndex((id) => id === source);\n if (inboundIndex !== undefined && inboundIndex !== -1) {\n this._edges[target].inbound.splice(inboundIndex, 1);\n }\n });\n });\n }\n\n /**\n * Sort edges for a node.\n *\n * Edges not included in the sorted list are appended to the end of the list.\n *\n * @param nodeId The id of the node to sort edges for.\n * @param relation The relation of the edges from the node to sort.\n * @param edges The ordered list of edges.\n * @ignore\n */\n _sortEdges(nodeId: string, relation: Relation, edges: string[]) {\n untracked(() => {\n batch(() => {\n const current = this._edges[nodeId];\n if (current) {\n const unsorted = current[relation].filter((id) => !edges.includes(id)) ?? [];\n const sorted = edges.filter((id) => current[relation].includes(id)) ?? [];\n current[relation].splice(0, current[relation].length, ...[...sorted, ...unsorted]);\n }\n });\n });\n }\n\n private _constructNode = (node: Omit<Node, typeof graphSymbol>) => {\n return create<NodeInternal>({ ...node, [graphSymbol]: this });\n };\n\n private _getNodes({\n node,\n relation = 'outbound',\n type,\n expansion,\n }: {\n node: Node;\n relation?: Relation;\n type?: string;\n expansion?: boolean;\n }): Node[] {\n if (expansion) {\n void this.expand(node, relation, type);\n }\n\n const edges = this._edges[node.id];\n if (!edges) {\n return [];\n } else {\n return edges[relation]\n .map((id) => this._nodes[id])\n .filter(nonNullable)\n .filter((n) => !type || n.type === type);\n }\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { type MaybePromise, type MakeOptional } from '@dxos/util';\n\n/**\n * Represents a node in the graph.\n */\n// TODO(wittjosiah): Use Effect Schema.\nexport type Node<TData = any, TProperties extends Record<string, any> = Record<string, any>> = Readonly<{\n /**\n * Globally unique ID.\n */\n id: string;\n\n /**\n * Typename of the data the node represents.\n */\n type: string;\n\n /**\n * Properties of the node relevant to displaying the node.\n */\n properties: Readonly<TProperties>;\n\n /**\n * Data the node represents.\n */\n // TODO(burdon): Type system (e.g., minimally provide identifier string vs. TypedObject vs. Graph mixin type system)?\n // type field would prevent convoluted sniffing of object properties. And allow direct pass-through for ECHO TypedObjects.\n data: TData;\n}>;\n\nexport type NodeFilter<T = any, U extends Record<string, any> = Record<string, any>> = (\n node: Node<unknown, Record<string, any>>,\n connectedNode: Node,\n) => node is Node<T, U>;\n\nexport type Relation = 'outbound' | 'inbound';\n\nexport const isGraphNode = (data: unknown): data is Node =>\n data && typeof data === 'object' && 'id' in data && 'properties' in data && data.properties\n ? typeof data.properties === 'object' && 'data' in data\n : false;\n\nexport type NodeArg<TData, TProperties extends Record<string, any> = Record<string, any>> = MakeOptional<\n Node<TData, TProperties>,\n 'data' | 'properties'\n> & {\n /** Will automatically add nodes with an edge from this node to each. */\n nodes?: NodeArg<unknown>[];\n\n /** Will automatically add specified edges. */\n edges?: [string, Relation][];\n};\n\n//\n// Actions\n//\n\nexport type InvokeParams = {\n /** Node the invoked action is connected to. */\n node: Node;\n\n caller?: string;\n};\n\nexport type ActionData = (params: InvokeParams) => MaybePromise<void>;\n\nexport type Action<TProperties extends Record<string, any> = Record<string, any>> = Readonly<\n Omit<Node<ActionData, TProperties>, 'properties'> & {\n properties: Readonly<TProperties>;\n }\n>;\n\nexport const isAction = (data: unknown): data is Action =>\n isGraphNode(data) ? typeof data.data === 'function' : false;\n\nexport const actionGroupSymbol = Symbol('ActionGroup');\n\nexport type ActionGroup = Readonly<\n Omit<Node<typeof actionGroupSymbol, Record<string, any>>, 'properties'> & {\n properties: Readonly<Record<string, any>>;\n }\n>;\n\nexport const isActionGroup = (data: unknown): data is ActionGroup =>\n isGraphNode(data) ? data.data === actionGroupSymbol : false;\n\nexport type ActionLike = Action | ActionGroup;\n\nexport const isActionLike = (data: unknown): data is Action | ActionGroup => isAction(data) || isActionGroup(data);\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { type Signal, effect, signal } from '@preact/signals-core';\n// import { yieldOrContinue } from 'main-thread-scheduling';\n\nimport { type UnsubscribeCallback } from '@dxos/async';\nimport { create } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { nonNullable } from '@dxos/util';\n\nimport { ACTION_GROUP_TYPE, ACTION_TYPE, Graph } from './graph';\nimport { type Relation, type NodeArg, type Node, type ActionData, actionGroupSymbol } from './node';\n\n/**\n * Graph builder extension for adding nodes to the graph based on just the node id.\n * This is useful for creating the first node in a graph or for hydrating cached nodes with data.\n *\n * @param params.id The id of the node to resolve.\n */\nexport type ResolverExtension = (params: { id: string }) => NodeArg<any> | undefined;\n\n/**\n * Graph builder extension for adding nodes to the graph based on a connection to an existing node.\n *\n * @param params.node The existing node the returned nodes will be connected to.\n */\nexport type ConnectorExtension<T = any> = (params: { node: Node<T> }) => NodeArg<any>[] | undefined;\n\n/**\n * Constrained case of the connector extension for more easily adding actions to the graph.\n */\nexport type ActionsExtension<T = any> = (params: {\n node: Node<T>;\n}) => Omit<NodeArg<ActionData>, 'type' | 'nodes' | 'edges'>[] | undefined;\n\n/**\n * Constrained case of the connector extension for more easily adding action groups to the graph.\n */\nexport type ActionGroupsExtension<T = any> = (params: {\n node: Node<T>;\n}) => Omit<NodeArg<typeof actionGroupSymbol>, 'type' | 'data' | 'nodes' | 'edges'>[] | undefined;\n\ntype GuardedNodeType<T> = T extends (value: any) => value is infer N ? (N extends Node<infer D> ? D : unknown) : never;\n\n/**\n * A graph builder extension is used to add nodes to the graph.\n *\n * @param params.id The unique id of the extension.\n * @param params.relation The relation the graph is being expanded from the existing node.\n * @param params.type If provided, all nodes returned are expected to have this type.\n * @param params.filter A filter function to determine if an extension should act on a node.\n * @param params.resolver A function to add nodes to the graph based on just the node id.\n * @param params.connector A function to add nodes to the graph based on a connection to an existing node.\n * @param params.actions A function to add actions to the graph based on a connection to an existing node.\n * @param params.actionGroups A function to add action groups to the graph based on a connection to an existing node.\n */\nexport type CreateExtensionOptions<T = any> = {\n id: string;\n relation?: Relation;\n type?: string;\n filter?: (node: Node) => node is Node<T>;\n resolver?: ResolverExtension;\n connector?: ConnectorExtension<GuardedNodeType<CreateExtensionOptions<T>['filter']>>;\n actions?: ActionsExtension<GuardedNodeType<CreateExtensionOptions<T>['filter']>>;\n actionGroups?: ActionGroupsExtension<GuardedNodeType<CreateExtensionOptions<T>['filter']>>;\n};\n\n/**\n * Create a graph builder extension.\n */\nexport const createExtension = <T = any>(extension: CreateExtensionOptions<T>): BuilderExtension[] => {\n const { id, resolver, connector, actions, actionGroups, ...rest } = extension;\n const getId = (key: string) => `${id}/${key}`;\n return [\n resolver ? { id: getId('resolver'), resolver } : undefined,\n connector ? { ...rest, id: getId('connector'), connector } : undefined,\n actionGroups\n ? ({\n ...rest,\n id: getId('actionGroups'),\n type: ACTION_GROUP_TYPE,\n relation: 'outbound',\n connector: ({ node }) =>\n actionGroups({ node })?.map((arg) => ({ ...arg, data: actionGroupSymbol, type: ACTION_GROUP_TYPE })),\n } satisfies BuilderExtension)\n : undefined,\n actions\n ? ({\n ...rest,\n id: getId('actions'),\n type: ACTION_TYPE,\n relation: 'outbound',\n connector: ({ node }) => actions({ node })?.map((arg) => ({ ...arg, type: ACTION_TYPE })),\n } satisfies BuilderExtension)\n : undefined,\n ].filter(nonNullable);\n};\n\nexport type GraphBuilderTraverseOptions = {\n node: Node;\n relation?: Relation;\n visitor: (node: Node, path: string[]) => void;\n};\n\n/**\n * The dispatcher is used to keep track of the current extension and state when memoizing functions.\n */\nclass Dispatcher {\n currentExtension?: string;\n stateIndex = 0;\n state: Record<string, any[]> = {};\n cleanup: (() => void)[] = [];\n}\n\nclass BuilderInternal {\n // This must be static to avoid passing the dispatcher instance to every memoized function.\n // If the dispatcher is not set that means that the memoized function is being called outside of the graph builder.\n static currentDispatcher?: Dispatcher;\n}\n\n/**\n * Allows code to be memoized within the context of a graph builder extension.\n * This is useful for creating instances which should be subscribed to rather than recreated.\n */\nexport const memoize = <T>(fn: () => T, key = 'result'): T => {\n const dispatcher = BuilderInternal.currentDispatcher;\n invariant(dispatcher?.currentExtension, 'memoize must be called within an extension');\n const all = dispatcher.state[dispatcher.currentExtension][dispatcher.stateIndex] ?? {};\n const current = all[key];\n const result = current ? current.result : fn();\n dispatcher.state[dispatcher.currentExtension][dispatcher.stateIndex] = { ...all, [key]: { result } };\n dispatcher.stateIndex++;\n return result;\n};\n\n/**\n * Register a cleanup function to be called when the graph builder is destroyed.\n */\nexport const cleanup = (fn: () => void): void => {\n memoize(() => {\n const dispatcher = BuilderInternal.currentDispatcher;\n invariant(dispatcher, 'cleanup must be called within an extension');\n dispatcher.cleanup.push(fn);\n });\n};\n\n/**\n * Convert a subscribe/get pair into a signal.\n */\nexport const toSignal = <T>(\n subscribe: (onChange: () => void) => () => void,\n get: () => T | undefined,\n key?: string,\n) => {\n const thisSignal = memoize(() => {\n return signal(get());\n }, key);\n const unsubscribe = memoize(() => {\n return subscribe(() => (thisSignal.value = get()));\n }, key);\n cleanup(() => {\n unsubscribe();\n });\n return thisSignal.value;\n};\n\nexport type BuilderExtension = {\n id: string;\n resolver?: ResolverExtension;\n connector?: ConnectorExtension;\n // Only for connector.\n relation?: Relation;\n type?: string;\n filter?: (node: Node) => boolean;\n};\n\ntype ExtensionArg = BuilderExtension | BuilderExtension[] | ExtensionArg[];\n\n/**\n * The builder provides an extensible way to compose the construction of the graph.\n */\n// TODO(wittjosiah): Add api for setting subscription set and/or radius.\n// Should unsubscribe from nodes that are not in the set/radius.\n// Should track LRU nodes that are not in the set/radius and remove them beyond a certain threshold.\nexport class GraphBuilder {\n private readonly _dispatcher = new Dispatcher();\n private readonly _extensions = create<Record<string, BuilderExtension>>({});\n private readonly _resolverSubscriptions = new Map<string, UnsubscribeCallback>();\n private readonly _connectorSubscriptions = new Map<string, UnsubscribeCallback>();\n private readonly _nodeChanged: Record<string, Signal<{}>> = {};\n private _graph: Graph;\n\n constructor() {\n this._graph = new Graph({\n onInitialNode: (id) => this._onInitialNode(id),\n onInitialNodes: (node, relation, type) => this._onInitialNodes(node, relation, type),\n onRemoveNode: (id) => this._onRemoveNode(id),\n });\n }\n\n get graph() {\n return this._graph;\n }\n\n /**\n * Register a node builder which will be called in order to construct the graph.\n */\n addExtension(extension: ExtensionArg): GraphBuilder {\n if (Array.isArray(extension)) {\n extension.forEach((ext) => this.addExtension(ext));\n return this;\n }\n\n this._dispatcher.state[extension.id] = [];\n this._extensions[extension.id] = extension;\n return this;\n }\n\n /**\n * Remove a node builder from the graph builder.\n */\n removeExtension(id: string): GraphBuilder {\n delete this._extensions[id];\n return this;\n }\n\n destroy() {\n this._dispatcher.cleanup.forEach((fn) => fn());\n this._resolverSubscriptions.forEach((unsubscribe) => unsubscribe());\n this._connectorSubscriptions.forEach((unsubscribe) => unsubscribe());\n this._resolverSubscriptions.clear();\n this._connectorSubscriptions.clear();\n }\n\n /**\n * Traverse a graph using just the connector extensions, without subscribing to any signals or persisting any nodes.\n */\n // TODO(wittjosiah): Rename? This is not traversing the graph proper.\n async traverse({ node, relation = 'outbound', visitor }: GraphBuilderTraverseOptions, path: string[] = []) {\n // Break cycles.\n if (path.includes(node.id)) {\n return;\n }\n\n // TODO(wittjosiah): Failed in test environment. ESM only?\n // await yieldOrContinue('idle');\n visitor(node, [...path, node.id]);\n\n const nodes = Object.values(this._extensions)\n .filter((extension) => relation === (extension.relation ?? 'outbound'))\n .flatMap((extension) => extension.connector?.({ node }) ?? [])\n .map(\n (arg): Node => ({\n id: arg.id,\n type: arg.type,\n data: arg.data ?? null,\n properties: arg.properties ?? {},\n }),\n );\n\n await Promise.all(nodes.map((n) => this.traverse({ node: n, relation, visitor }, [...path, node.id])));\n }\n\n private async _onInitialNode(nodeId: string) {\n this._nodeChanged[nodeId] = this._nodeChanged[nodeId] ?? signal({});\n this._resolverSubscriptions.set(\n nodeId,\n effect(() => {\n for (const { id, resolver } of Object.values(this._extensions)) {\n if (!resolver) {\n continue;\n }\n this._dispatcher.currentExtension = id;\n this._dispatcher.stateIndex = 0;\n BuilderInternal.currentDispatcher = this._dispatcher;\n const node = resolver({ id: nodeId });\n BuilderInternal.currentDispatcher = undefined;\n if (node) {\n this.graph._addNodes([node]);\n if (this._nodeChanged[node.id]) {\n this._nodeChanged[node.id].value = {};\n }\n break;\n }\n }\n }),\n );\n }\n\n private async _onInitialNodes(node: Node, nodesRelation: Relation, nodesType?: string) {\n this._nodeChanged[node.id] = this._nodeChanged[node.id] ?? signal({});\n let first = true;\n let previous: string[] = [];\n this._connectorSubscriptions.set(\n node.id,\n effect(() => {\n // TODO(wittjosiah): This is a workaround for a race between the node removal and the effect re-running.\n // To cause this case to happen, remove a collection and then undo the removal.\n if (!first && !this._connectorSubscriptions.has(node.id)) {\n return;\n }\n first = false;\n\n // Subscribe to extensions being added.\n Object.keys(this._extensions);\n // Subscribe to connected node changes.\n this._nodeChanged[node.id].value;\n\n // TODO(wittjosiah): Consider allowing extensions to collaborate on the same node by merging their results.\n const nodes: NodeArg<any>[] = [];\n for (const { id, connector, filter, type, relation = 'outbound' } of Object.values(this._extensions)) {\n if (\n !connector ||\n relation !== nodesRelation ||\n (nodesType && type !== nodesType) ||\n (filter && !filter(node))\n ) {\n continue;\n }\n\n this._dispatcher.currentExtension = id;\n this._dispatcher.stateIndex = 0;\n BuilderInternal.currentDispatcher = this._dispatcher;\n nodes.push(...(connector({ node }) ?? []));\n BuilderInternal.currentDispatcher = undefined;\n }\n const ids = nodes.map((n) => n.id);\n const removed = previous.filter((id) => !ids.includes(id));\n previous = ids;\n\n this.graph._removeNodes(removed, true);\n this.graph._addNodes(nodes);\n this.graph._addEdges(\n nodes.map(({ id }) =>\n nodesRelation === 'outbound' ? { source: node.id, target: id } : { source: id, target: node.id },\n ),\n );\n this.graph._sortEdges(\n node.id,\n nodesRelation,\n nodes.map(({ id }) => id),\n );\n nodes.forEach((n) => {\n if (this._nodeChanged[n.id]) {\n this._nodeChanged[n.id].value = {};\n }\n });\n }),\n );\n }\n\n private async _onRemoveNode(nodeId: string) {\n this._resolverSubscriptions.get(nodeId)?.();\n this._connectorSubscriptions.get(nodeId)?.();\n this._resolverSubscriptions.delete(nodeId);\n this._connectorSubscriptions.delete(nodeId);\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,0BAAyC;AAEzC,mBAAsC;AACtC,yBAA4C;AAC5C,uBAA0B;AAC1B,kBAA4B;AEL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AEuD,QAAQvD;QAAIwD,QAAQ5E,KAAKoB;MAAG,CAAA,CAAA;AAGnG,WAAKnB,MAAMqG,WACTtG,KAAKoB,IACLkI,eACAxH,MAAMK,IAAI,CAAC,EAAEf,GAAE,MAAOA,EAAAA,CAAAA;AAExBU,YAAMoC,QAAQ,CAAC9B,MAAAA;AACb,YAAI,KAAKsG,aAAatG,EAAEhB,EAAE,GAAG;AAC3B,eAAKsH,aAAatG,EAAEhB,EAAE,EAAE+G,QAAQ,CAAC;QACnC;MACF,CAAA;IACF,CAAA,CAAA;EAEJ;EAEA,MAAcjH,cAAcqF,QAAgB;AAC1C,SAAKgC,uBAAuBP,IAAIzB,MAAAA,IAAAA;AAChC,SAAKkC,wBAAwBT,IAAIzB,MAAAA,IAAAA;AACjC,SAAKgC,uBAAuBqB,OAAOrD,MAAAA;AACnC,SAAKkC,wBAAwBmB,OAAOrD,MAAAA;EACtC;AACF;",
6
+ "names": ["import_signals_core", "import_echo_schema", "import_invariant", "import_util", "isGraphNode", "data", "properties", "isAction", "actionGroupSymbol", "Symbol", "isActionGroup", "isActionLike", "graphSymbol", "getGraph", "node", "graph", "invariant", "ROOT_ID", "ROOT_TYPE", "ACTION_TYPE", "ACTION_GROUP_TYPE", "Graph", "constructor", "onInitialNode", "onInitialNodes", "onRemoveNode", "_waitingForNodes", "_initialized", "_constructNode", "create", "_onInitialNode", "_onInitialNodes", "_onRemoveNode", "_nodes", "id", "type", "_edges", "inbound", "outbound", "root", "findNode", "toJSON", "maxLength", "seen", "nodes", "obj", "length", "slice", "label", "map", "n", "nextSeen", "includes", "undefined", "filter", "nonNullable", "existingNode", "waitForNode", "timeout", "trigger", "Trigger", "wait", "asyncTimeout", "options", "relation", "expansion", "_getNodes", "untracked", "edges", "actions", "expand", "key", "_key", "initialized", "traverse", "visitor", "path", "shouldContinue", "Object", "values", "forEach", "child", "subscribeTraverse", "currentPath", "effect", "result", "nodeSubscriptions", "unsubscribe", "getPath", "source", "target", "start", "found", "_addNodes", "batch", "_addNode", "_node", "wake", "subNode", "_addEdge", "_removeNodes", "ids", "_removeNode", "_removeEdge", "keys", "startsWith", "_addEdges", "edge", "sourceEdges", "push", "targetEdges", "_removeEdges", "outboundIndex", "findIndex", "splice", "inboundIndex", "_sortEdges", "nodeId", "current", "unsorted", "sorted", "createExtension", "extension", "resolver", "connector", "actionGroups", "rest", "getId", "arg", "Dispatcher", "stateIndex", "state", "cleanup", "BuilderInternal", "memoize", "fn", "dispatcher", "currentDispatcher", "currentExtension", "all", "toSignal", "subscribe", "get", "thisSignal", "signal", "value", "GraphBuilder", "_dispatcher", "_extensions", "_resolverSubscriptions", "Map", "_connectorSubscriptions", "_nodeChanged", "_graph", "addExtension", "Array", "isArray", "ext", "removeExtension", "destroy", "clear", "flatMap", "Promise", "set", "nodesRelation", "nodesType", "first", "previous", "has", "removed", "delete"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/sdk/app-graph/src/node.ts":{"bytes":5259,"imports":[],"format":"esm"},"packages/sdk/app-graph/src/graph.ts":{"bytes":50171,"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"packages/sdk/app-graph/src/node.ts","kind":"import-statement","original":"./node"}],"format":"esm"},"packages/sdk/app-graph/src/graph-builder.ts":{"bytes":38696,"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"packages/sdk/app-graph/src/graph.ts","kind":"import-statement","original":"./graph"},{"path":"packages/sdk/app-graph/src/node.ts","kind":"import-statement","original":"./node"}],"format":"esm"},"packages/sdk/app-graph/src/index.ts":{"bytes":672,"imports":[{"path":"packages/sdk/app-graph/src/graph.ts","kind":"import-statement","original":"./graph"},{"path":"packages/sdk/app-graph/src/graph-builder.ts","kind":"import-statement","original":"./graph-builder"},{"path":"packages/sdk/app-graph/src/node.ts","kind":"import-statement","original":"./node"}],"format":"esm"}},"outputs":{"packages/sdk/app-graph/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":47906},"packages/sdk/app-graph/dist/lib/node/index.cjs":{"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["ACTION_GROUP_TYPE","ACTION_TYPE","Graph","GraphBuilder","ROOT_ID","ROOT_TYPE","actionGroupSymbol","cleanup","createExtension","getGraph","isAction","isActionGroup","isActionLike","isGraphNode","memoize","toSignal"],"entryPoint":"packages/sdk/app-graph/src/index.ts","inputs":{"packages/sdk/app-graph/src/graph.ts":{"bytesInOutput":12265},"packages/sdk/app-graph/src/node.ts":{"bytesInOutput":477},"packages/sdk/app-graph/src/index.ts":{"bytesInOutput":0},"packages/sdk/app-graph/src/graph-builder.ts":{"bytesInOutput":7747}},"bytes":20932}}}
1
+ {"inputs":{"packages/sdk/app-graph/src/node.ts":{"bytes":5259,"imports":[],"format":"esm"},"packages/sdk/app-graph/src/graph.ts":{"bytes":51067,"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"packages/sdk/app-graph/src/node.ts","kind":"import-statement","original":"./node"}],"format":"esm"},"packages/sdk/app-graph/src/graph-builder.ts":{"bytes":39059,"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"packages/sdk/app-graph/src/graph.ts","kind":"import-statement","original":"./graph"},{"path":"packages/sdk/app-graph/src/node.ts","kind":"import-statement","original":"./node"}],"format":"esm"},"packages/sdk/app-graph/src/index.ts":{"bytes":672,"imports":[{"path":"packages/sdk/app-graph/src/graph.ts","kind":"import-statement","original":"./graph"},{"path":"packages/sdk/app-graph/src/graph-builder.ts","kind":"import-statement","original":"./graph-builder"},{"path":"packages/sdk/app-graph/src/node.ts","kind":"import-statement","original":"./node"}],"format":"esm"}},"outputs":{"packages/sdk/app-graph/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":48553},"packages/sdk/app-graph/dist/lib/node/index.cjs":{"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["ACTION_GROUP_TYPE","ACTION_TYPE","Graph","GraphBuilder","ROOT_ID","ROOT_TYPE","actionGroupSymbol","cleanup","createExtension","getGraph","isAction","isActionGroup","isActionLike","isGraphNode","memoize","toSignal"],"entryPoint":"packages/sdk/app-graph/src/index.ts","inputs":{"packages/sdk/app-graph/src/graph.ts":{"bytesInOutput":12485},"packages/sdk/app-graph/src/node.ts":{"bytesInOutput":477},"packages/sdk/app-graph/src/index.ts":{"bytesInOutput":0},"packages/sdk/app-graph/src/graph-builder.ts":{"byt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I,KAAK,MAAM,IAAI,OAC1C,MAAM,CAAC,GAAG,SAAS,QAClB,MAAM,kBAYb,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,CAAC,EAAE,iBAAiB,CAAC;IAC7B,SAAS,CAAC,EAAE,kBAAkB,CAAC;IAE/B,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC;CAClC,CAAC;AAEF,KAAK,YAAY,GAAG,gBAAgB,GAAG,gBAAgB,EAAE,GAAG,YAAY,EAAE,CAAC;AAE3E;;GAEG;AAIH,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAoB;IAChD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAgD;IAC5E,OAAO,CAAC,QAAQ,CAAC,sBAAsB,CAA0C;IACjF,OAAO,CAAC,QAAQ,CAAC,uBAAuB,CAA0C;IAClF,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAkC;IAC/D,OAAO,CAAC,MAAM,CAAQ;;IAUtB,IAAI,KAAK,UAER;IAED;;OAEG;IACH,YAAY,CAAC,SAAS,EAAE,YAAY,GAAG,YAAY;IAWnD;;OAEG;IACH,eAAe,CAAC,EAAE,EAAE,MAAM,GAAG,YAAY;IAKzC,OAAO;IAQP;;OAEG;IAEG,QAAQ,CAAC,EAAE,IAAI,EAAE,QAAqB,EAAE,OAAO,EAAE,EAAE,2BAA2B,EAAE,IAAI,GAAE,MAAM,EAAO;YAyB3F,cAAc;YA0Bd,eAAe;YA8Df,aAAa;CAM5B"}
@@ -107,6 +107,7 @@ export declare class Graph {
107
107
  data: any;
108
108
  }>[];
109
109
  expand(node: Node, relation?: Relation, type?: string): Promise<void>;
110
+ private _key;
110
111
  /**
111
112
  * Recursive depth-first traversal of the graph.
112
113
  *
@@ -1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AE,MAAM,CAAA;KAAE,GAAG,MAAM,EAAE,GAAG,SAAS;IAkC/F,OAAO,CAAC,QAAQ;IAgEhB,OAAO,CAAC,WAAW;IAwCnB,OAAO,CAAC,QAAQ;IA6BhB,OAAO,CAAC,WAAW;IAgBnB;;;;;;;;;OASG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE;IAa9D,OAAO,CAAC,cAAc,CAEpB;IAEF,OAAO,CAAC,SAAS;CAyBlB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/app-graph",
3
- "version": "0.6.4",
3
+ "version": "0.6.5-staging.435ed25",
4
4
  "description": "Constructs knowledge graphs for the purpose of building applications on top of",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -9,12 +9,16 @@
9
9
  "exports": {
10
10
  ".": {
11
11
  "browser": "./dist/lib/browser/index.mjs",
12
- "import": "./dist/lib/browser/index.mjs",
13
- "require": "./dist/lib/node/index.cjs",
14
- "node": "./dist/lib/node/index.cjs"
12
+ "node": {
13
+ "default": "./dist/lib/node/index.cjs"
14
+ },
15
+ "types": "./dist/types/src/index.d.ts"
15
16
  }
16
17
  },
17
18
  "types": "dist/types/src/index.d.ts",
19
+ "typesVersions": {
20
+ "*": {}
21
+ },
18
22
  "files": [
19
23
  "dist",
20
24
  "src"
@@ -22,30 +26,30 @@
22
26
  "dependencies": {
23
27
  "@preact/signals-core": "^1.6.0",
24
28
  "main-thread-scheduling": "^14.1.1",
25
- "@dxos/async": "0.6.4",
26
- "@dxos/echo-schema": "0.6.4",
27
- "@dxos/echo-signals": "0.6.4",
28
- "@dxos/invariant": "0.6.4",
29
- "@dxos/debug": "0.6.4",
30
- "@dxos/log": "0.6.4",
31
- "@dxos/util": "0.6.4"
29
+ "@dxos/echo-schema": "0.6.5-staging.435ed25",
30
+ "@dxos/invariant": "0.6.5-staging.435ed25",
31
+ "@dxos/async": "0.6.5-staging.435ed25",
32
+ "@dxos/debug": "0.6.5-staging.435ed25",
33
+ "@dxos/echo-signals": "0.6.5-staging.435ed25",
34
+ "@dxos/log": "0.6.5-staging.435ed25",
35
+ "@dxos/util": "0.6.5-staging.435ed25"
32
36
  },
33
37
  "devDependencies": {
34
38
  "@phosphor-icons/react": "^2.1.5",
35
39
  "@types/react": "^18.0.21",
36
40
  "@types/react-dom": "^18.0.6",
37
- "react": "^18.2.0",
38
- "react-dom": "^18.2.0",
41
+ "react": "~18.2.0",
42
+ "react-dom": "~18.2.0",
39
43
  "vite": "^5.3.4",
40
- "@dxos/random": "0.6.4",
41
- "@dxos/react-client": "0.6.4",
42
- "@dxos/react-ui-theme": "0.6.4",
43
- "@dxos/react-ui": "0.6.4",
44
- "@dxos/storybook-utils": "0.6.4"
44
+ "@dxos/random": "0.6.5-staging.435ed25",
45
+ "@dxos/react-ui": "0.6.5-staging.435ed25",
46
+ "@dxos/react-client": "0.6.5-staging.435ed25",
47
+ "@dxos/react-ui-theme": "0.6.5-staging.435ed25",
48
+ "@dxos/storybook-utils": "0.6.5-staging.435ed25"
45
49
  },
46
50
  "peerDependencies": {
47
- "react": "^18.2.0",
48
- "react-dom": "^18.2.0"
51
+ "react": "~18.2.0",
52
+ "react-dom": "~18.2.0"
49
53
  },
50
54
  "publishConfig": {
51
55
  "access": "public"
@@ -2,7 +2,7 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { signal } from '@preact/signals-core';
5
+ import { batch, signal } from '@preact/signals-core';
6
6
  import chai, { expect } from 'chai';
7
7
  import chaiAsPromised from 'chai-as-promised';
8
8
 
@@ -174,6 +174,74 @@ describe('GraphBuilder', () => {
174
174
  }
175
175
  });
176
176
 
177
+ test('unsubscribes', async () => {
178
+ let count = 0;
179
+ const name = signal('default');
180
+ const sub = signal('default');
181
+ const builder = new GraphBuilder();
182
+ builder.addExtension([
183
+ createExtension({
184
+ id: 'root',
185
+ filter: (node): node is Node<null> => node.id === 'root',
186
+ connector: () =>
187
+ name.value === 'removed'
188
+ ? []
189
+ : [{ id: EXAMPLE_ID, type: EXAMPLE_TYPE, data: name, properties: { label: name.value } }],
190
+ }),
191
+ createExtension({
192
+ id: 'connector',
193
+ filter: (node): node is Node<string> => node.id === EXAMPLE_ID,
194
+ connector: () => {
195
+ count++;
196
+ sub.value;
197
+
198
+ return [];
199
+ },
200
+ }),
201
+ ]);
202
+
203
+ // Count should not increment until the node is expanded.
204
+ const graph = builder.graph;
205
+ await graph.expand(graph.root);
206
+ expect(count).to.equal(0);
207
+
208
+ // Count should increment when the node is expanded.
209
+ const [node] = graph.nodes(graph.root);
210
+ await graph.expand(node!);
211
+ expect(count).to.equal(1);
212
+
213
+ // Count should increment when the parent changes.
214
+ name.value = 'updated';
215
+ expect(count).to.equal(2);
216
+
217
+ // Count should increment when the signal changes.
218
+ sub.value = 'updated';
219
+ expect(count).to.equal(3);
220
+
221
+ // Count will still increment if the node is removed in a batch.
222
+ batch(() => {
223
+ name.value = 'removed';
224
+ sub.value = 'batch';
225
+ });
226
+ expect(count).to.equal(4);
227
+
228
+ // Count should not increment after the node is removed.
229
+ sub.value = 'removed';
230
+ expect(count).to.equal(4);
231
+
232
+ // Count will not increment when node is added back.
233
+ name.value = 'added';
234
+ expect(count).to.equal(4);
235
+
236
+ // Count should increment when the node is expanded again.
237
+ await graph.expand(node!);
238
+ expect(count).to.equal(5);
239
+
240
+ // Count should increment when signal changes again.
241
+ sub.value = 'added';
242
+ expect(count).to.equal(6);
243
+ });
244
+
177
245
  test('filters by type', async () => {
178
246
  const builder = new GraphBuilder();
179
247
  builder.addExtension(
@@ -265,43 +265,44 @@ export class GraphBuilder {
265
265
 
266
266
  private async _onInitialNode(nodeId: string) {
267
267
  this._nodeChanged[nodeId] = this._nodeChanged[nodeId] ?? signal({});
268
- let resolved = false;
269
- for (const { id, resolver } of Object.values(this._extensions)) {
270
- if (resolved || !resolver) {
271
- continue;
272
- }
273
-
274
- const unsubscribe = effect(() => {
275
- this._dispatcher.currentExtension = id;
276
- this._dispatcher.stateIndex = 0;
277
- BuilderInternal.currentDispatcher = this._dispatcher;
278
- const node = resolver({ id: nodeId });
279
- BuilderInternal.currentDispatcher = undefined;
280
- if (node) {
281
- resolved = true;
282
- this.graph._addNodes([node]);
283
- if (this._nodeChanged[node.id]) {
284
- this._nodeChanged[node.id].value = {};
268
+ this._resolverSubscriptions.set(
269
+ nodeId,
270
+ effect(() => {
271
+ for (const { id, resolver } of Object.values(this._extensions)) {
272
+ if (!resolver) {
273
+ continue;
274
+ }
275
+ this._dispatcher.currentExtension = id;
276
+ this._dispatcher.stateIndex = 0;
277
+ BuilderInternal.currentDispatcher = this._dispatcher;
278
+ const node = resolver({ id: nodeId });
279
+ BuilderInternal.currentDispatcher = undefined;
280
+ if (node) {
281
+ this.graph._addNodes([node]);
282
+ if (this._nodeChanged[node.id]) {
283
+ this._nodeChanged[node.id].value = {};
284
+ }
285
+ break;
285
286
  }
286
287
  }
287
- });
288
-
289
- if (resolved) {
290
- this._resolverSubscriptions.get(nodeId)?.();
291
- this._resolverSubscriptions.set(nodeId, unsubscribe);
292
- break;
293
- } else {
294
- unsubscribe();
295
- }
296
- }
288
+ }),
289
+ );
297
290
  }
298
291
 
299
292
  private async _onInitialNodes(node: Node, nodesRelation: Relation, nodesType?: string) {
300
293
  this._nodeChanged[node.id] = this._nodeChanged[node.id] ?? signal({});
294
+ let first = true;
301
295
  let previous: string[] = [];
302
296
  this._connectorSubscriptions.set(
303
297
  node.id,
304
298
  effect(() => {
299
+ // TODO(wittjosiah): This is a workaround for a race between the node removal and the effect re-running.
300
+ // To cause this case to happen, remove a collection and then undo the removal.
301
+ if (!first && !this._connectorSubscriptions.has(node.id)) {
302
+ return;
303
+ }
304
+ first = false;
305
+
305
306
  // Subscribe to extensions being added.
306
307
  Object.keys(this._extensions);
307
308
  // Subscribe to connected node changes.
package/src/graph.test.ts CHANGED
@@ -110,6 +110,37 @@ describe('Graph', () => {
110
110
  expect(graph.nodes(root)).to.have.length(1);
111
111
  });
112
112
 
113
+ test('re-add node', () => {
114
+ const graph = new Graph();
115
+
116
+ graph._addNodes([
117
+ {
118
+ id: ROOT_ID,
119
+ type: ROOT_TYPE,
120
+ nodes: [{ id: 'test1', type: 'test' }],
121
+ },
122
+ ]);
123
+
124
+ expect(graph.root.id).to.equal('root');
125
+ expect(graph.nodes(graph.root)).to.have.length(1);
126
+ expect(graph.findNode('test1')?.id).to.equal('test1');
127
+
128
+ graph._removeNodes(['test1']);
129
+ expect(graph.findNode('test1')).to.be.undefined;
130
+ expect(graph.nodes(graph.root)).to.be.empty;
131
+
132
+ graph._addNodes([
133
+ {
134
+ id: ROOT_ID,
135
+ type: ROOT_TYPE,
136
+ nodes: [{ id: 'test1', type: 'test' }],
137
+ },
138
+ ]);
139
+ expect(graph.root.id).to.equal('root');
140
+ expect(graph.nodes(graph.root)).to.have.length(1);
141
+ expect(graph.findNode('test1')?.id).to.equal('test1');
142
+ });
143
+
113
144
  test('add edge', () => {
114
145
  const graph = new Graph();
115
146
 
package/src/graph.ts CHANGED
@@ -200,8 +200,7 @@ export class Graph {
200
200
  }
201
201
 
202
202
  async expand(node: Node, relation: Relation = 'outbound', type?: string) {
203
- // TODO(wittjosiah): Factor out helper.
204
- const key = `${node.id}-${relation}-${type}`;
203
+ const key = this._key(node, relation, type);
205
204
  const initialized = this._initialized[key];
206
205
  if (!initialized && this._onInitialNodes) {
207
206
  await this._onInitialNodes(node, relation, type);
@@ -209,6 +208,10 @@ export class Graph {
209
208
  }
210
209
  }
211
210
 
211
+ private _key(node: Node, relation: Relation, type?: string) {
212
+ return `${node.id}-${relation}-${type}`;
213
+ }
214
+
212
215
  /**
213
216
  * Recursive depth-first traversal of the graph.
214
217
  *
@@ -385,6 +388,11 @@ export class Graph {
385
388
 
386
389
  // Remove node.
387
390
  delete this._nodes[id];
391
+ Object.keys(this._initialized)
392
+ .filter((key) => key.startsWith(id))
393
+ .forEach((key) => {
394
+ delete this._initialized[key];
395
+ });
388
396
  void this._onRemoveNode?.(id);
389
397
  });
390
398
  }