@dxos/app-graph 0.5.9-next.a50ff17 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/index.mjs +9 -3
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +9 -3
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/graph.d.ts.map +1 -1
- package/package.json +12 -12
- package/src/graph.ts +8 -2
|
@@ -69,7 +69,7 @@ var Graph = class {
|
|
|
69
69
|
* Convert the graph to a JSON object.
|
|
70
70
|
*/
|
|
71
71
|
toJSON({ id = ROOT_ID, maxLength = 32 } = {}) {
|
|
72
|
-
const toJSON = (node) => {
|
|
72
|
+
const toJSON = (node, seen = []) => {
|
|
73
73
|
const nodes = node.nodes();
|
|
74
74
|
const obj = {
|
|
75
75
|
id: node.id.length > maxLength ? `${node.id.slice(0, maxLength - 3)}...` : node.id
|
|
@@ -78,14 +78,20 @@ var Graph = class {
|
|
|
78
78
|
obj.label = node.properties.label;
|
|
79
79
|
}
|
|
80
80
|
if (nodes.length) {
|
|
81
|
-
obj.nodes = nodes.map((
|
|
81
|
+
obj.nodes = nodes.map((n) => {
|
|
82
|
+
const nextSeen = [
|
|
83
|
+
...seen,
|
|
84
|
+
node.id
|
|
85
|
+
];
|
|
86
|
+
return nextSeen.includes(n.id) ? void 0 : toJSON(n, nextSeen);
|
|
87
|
+
}).filter(nonNullable);
|
|
82
88
|
}
|
|
83
89
|
return obj;
|
|
84
90
|
};
|
|
85
91
|
const root = this.findNode(id);
|
|
86
92
|
invariant(root, `Node not found: ${id}`, {
|
|
87
93
|
F: __dxlog_file,
|
|
88
|
-
L:
|
|
94
|
+
L: 91,
|
|
89
95
|
S: this,
|
|
90
96
|
A: [
|
|
91
97
|
"root",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/graph.ts", "../../../src/node.ts", "../../../src/graph-builder.ts", "../../../src/helpers.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { untracked } from '@preact/signals-core';\n\nimport { create } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { nonNullable } from '@dxos/util';\n\nimport { isActionLike, type EdgeDirection, type Node, type NodeArg, type NodeBase } from './node';\n\nexport const ROOT_ID = 'root';\n\nexport type TraversalOptions = {\n /**\n * The node to start traversing from.\n *\n * @default root\n */\n node?: Node;\n\n /**\n * The direction to traverse graph edges.\n *\n * @default 'outbound'\n */\n direction?: EdgeDirection;\n\n /**\n * A predicate to filter nodes which are passed to the `visitor` callback.\n */\n filter?: (node: Node) => boolean;\n\n /**\n * A callback which is called for each node visited during traversal.\n */\n visitor?: (node: Node, path: string[]) => void;\n};\n\n/**\n * The Graph represents the structure of the application constructed via plugins.\n */\nexport class Graph {\n /**\n * @internal\n */\n readonly _nodes = create<Record<string, NodeBase>>({\n [ROOT_ID]: { id: ROOT_ID, properties: {}, data: null },\n });\n\n /**\n * @internal\n */\n // Key is the `${node.id}-${direction}` and value is an ordered list of node ids.\n // Explicit type required because TS says this is not portable.\n readonly _edges = create<Record<string, string[]>>({});\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): any => {\n const nodes = node.nodes();\n const obj: Record<string, any> = {\n id: node.id.length > maxLength ? `${node.id.slice(0, maxLength - 3)}...` : node.id,\n };\n if (node.properties.label) {\n obj.label = node.properties.label;\n }\n if (nodes.length) {\n obj.nodes = nodes.map((node) => toJSON(node));\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 findNode(id: string): Node | undefined {\n const nodeBase = this._nodes[id];\n if (!nodeBase) {\n return undefined;\n }\n\n return this._constructNode(nodeBase);\n }\n\n private _constructNode = (nodeBase: NodeBase): Node => {\n const node: Node = {\n ...nodeBase,\n edges: ({ direction = 'outbound' } = {}) => {\n return this._edges[this.getEdgeKey(node.id, direction)];\n },\n nodes: ({ direction, filter } = {}) => {\n const nodes = this._getNodes({ id: node.id, direction }).filter((n) => !isActionLike(n));\n return filter ? nodes.filter((n) => filter(n, node)) : nodes;\n },\n node: (id: string) => {\n return this._getNodes({ id }).find((node) => node.id === id);\n },\n actions: () => {\n return this._getNodes({ id: node.id }).filter(isActionLike);\n },\n };\n\n return node;\n };\n\n private _getNodes({ id, direction = 'outbound' }: { id: string; direction?: EdgeDirection }): Node[] {\n const edges = this._edges[this.getEdgeKey(id, direction)];\n if (!edges) {\n return [];\n }\n\n return edges.map((id) => this.findNode(id)).filter(nonNullable);\n }\n\n private getEdgeKey(id: string, direction: EdgeDirection) {\n return `${id}-${direction}`;\n }\n\n /**\n * Add nodes to the graph.\n */\n addNodes<TData = null, TProperties extends Record<string, any> = Record<string, any>>(\n ...nodes: NodeArg<TData, TProperties>[]\n ): Node<TData, TProperties>[] {\n return 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 node = { data: null, properties: {}, ..._node };\n this._nodes[node.id] = node;\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, direction]) =>\n direction === 'outbound'\n ? this.addEdge({ source: node.id, target: id })\n : this.addEdge({ source: id, target: node.id }),\n );\n }\n\n return this._constructNode(node) as Node<TData, TProperties>;\n });\n }\n\n /**\n * Remove nodes from the graph.\n *\n * @param id The id of the node to remove.\n * @param edges Whether to remove edges connected to the node from the graph as well.\n */\n 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 node.\n delete this._edges[this.getEdgeKey(id, 'outbound')];\n delete this._edges[this.getEdgeKey(id, 'inbound')];\n\n // Remove edges from connected nodes.\n this._getNodes({ id }).forEach((node) => this.removeEdge({ source: id, target: node.id }));\n this._getNodes({ id, direction: 'inbound' }).forEach((node) =>\n this.removeEdge({ source: node.id, target: id }),\n );\n }\n\n // Remove node.\n delete this._nodes[id];\n });\n }\n\n /**\n * Add an edge to the graph.\n */\n addEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n const outbound = this._edges[this.getEdgeKey(source, 'outbound')];\n if (!outbound) {\n this._edges[this.getEdgeKey(source, 'outbound')] = [target];\n } else if (!outbound.includes(target)) {\n outbound.push(target);\n }\n\n const inbound = this._edges[this.getEdgeKey(target, 'inbound')];\n if (!inbound) {\n this._edges[this.getEdgeKey(target, 'inbound')] = [source];\n } else if (!inbound.includes(source)) {\n inbound.push(source);\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 direction The direction of the edges from the node to sort.\n * @param edges The ordered list of edges.\n */\n sortEdges(nodeId: string, direction: EdgeDirection, edges: string[]) {\n untracked(() => {\n const current = this._edges[this.getEdgeKey(nodeId, direction)];\n if (current) {\n const unsorted = current.filter((id) => !edges.includes(id)) ?? [];\n const sorted = edges.filter((id) => current.includes(id)) ?? [];\n current.splice(0, current.length, ...[...sorted, ...unsorted]);\n }\n });\n }\n\n /**\n * Remove an edge from the graph.\n */\n removeEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n const outboundIndex = this._edges[this.getEdgeKey(source, 'outbound')]?.findIndex((id) => id === target);\n if (outboundIndex !== -1) {\n this._edges[this.getEdgeKey(source, 'outbound')].splice(outboundIndex, 1);\n }\n\n const inboundIndex = this._edges[this.getEdgeKey(target, 'inbound')]?.findIndex((id) => id === source);\n if (inboundIndex !== -1) {\n this._edges[this.getEdgeKey(target, 'inbound')].splice(inboundIndex, 1);\n }\n });\n }\n\n /**\n * Recursive depth-first traversal.\n *\n * @param options.node The node to start traversing from.\n * @param options.direction The direction to traverse graph edges.\n * @param options.filter A predicate to filter nodes which are passed to the `visitor` callback.\n * @param options.visitor A callback which is called for each node visited during traversal.\n */\n traverse({ node = this.root, direction = 'outbound', filter, visitor }: TraversalOptions, path: string[] = []): void {\n // Break cycles.\n if (path.includes(node.id)) {\n return;\n }\n\n if (!filter || filter(node)) {\n visitor?.(node, [...path, node.id]);\n }\n\n Object.values(this._getNodes({ id: node.id, direction })).forEach((child) =>\n this.traverse({ node: child, direction, filter, visitor }, [...path, node.id]),\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 filter: () => !found,\n visitor: (node, path) => {\n if (node.id === target) {\n found = path;\n }\n },\n });\n\n return found;\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 NodeBase<TData = any, TProperties extends Record<string, any> = Record<string, any>> = {\n /**\n * Globally unique ID.\n */\n id: string;\n\n /**\n * Properties of the node relevant to displaying the node.\n */\n properties: 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 EdgeDirection = 'outbound' | 'inbound';\n\nexport type ConnectedNodes = {\n /**\n * Edges that this node is connected to in default order.\n */\n edges(params?: { direction?: EdgeDirection }): Readonly<string[]>;\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>>(params?: {\n direction?: EdgeDirection;\n filter?: NodeFilter<T, U>;\n }): Node<T>[];\n\n /**\n * Get a specific connected node by id.\n */\n node(id: string): Node | undefined;\n};\n\nexport type ConnectedActions = {\n /**\n * Actions or action groups that this node is connected to in default order.\n */\n actions(): ActionLike[];\n};\n\nexport type Node<TData = any, TProperties extends Record<string, any> = Record<string, any>> = Readonly<\n Omit<NodeBase<TData, TProperties>, 'properties'> & { properties: Readonly<TProperties> } & ConnectedNodes &\n ConnectedActions\n>;\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 NodeBase<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, EdgeDirection][];\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<NodeBase<ActionData, TProperties>, 'properties'> & {\n properties: Readonly<TProperties>;\n } & ConnectedNodes\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<NodeBase<typeof actionGroupSymbol, Record<string, any>>, 'properties'> & {\n properties: Readonly<Record<string, any>>;\n } & ConnectedActions\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 { EventSubscriptions, type UnsubscribeCallback } from '@dxos/async';\n\nimport { Graph } from './graph';\n\nexport type BuilderExtension = (graph: Graph) => UnsubscribeCallback | void;\n\n/**\n * The builder provides an extensible way to compose the construction of the graph.\n */\nexport class GraphBuilder {\n private readonly _extensions = new Map<string, BuilderExtension>();\n private readonly _unsubscribe = new EventSubscriptions();\n\n /**\n * Register a node builder which will be called in order to construct the graph.\n */\n addExtension(id: string, extension: BuilderExtension): GraphBuilder {\n this._extensions.set(id, extension);\n return this;\n }\n\n /**\n * Remove a node builder from the graph builder.\n */\n removeExtension(id: string): GraphBuilder {\n this._extensions.delete(id);\n return this;\n }\n\n /**\n * Construct the graph, starting by calling all registered extensions.\n * @param previousGraph If provided, the graph will be updated in place.\n */\n build(previousGraph?: Graph): Graph {\n // Clear previous extension subscriptions.\n this._unsubscribe.clear();\n\n const graph: Graph = previousGraph ?? new Graph();\n\n Array.from(this._extensions.values()).forEach((builder) => {\n const unsubscribe = builder(graph);\n unsubscribe && this._unsubscribe.add(unsubscribe);\n });\n\n return graph;\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Graph } from './graph';\nimport { type Node, type NodeArg } from './node';\n\n/**\n * If the condition is true, adds the nodes to the graph, otherwise removes the nodes from the graph.\n */\nexport const manageNodes = <TData = null, TProperties extends Record<string, any> = Record<string, any>>({\n graph,\n condition,\n nodes,\n removeEdges,\n}: {\n graph: Graph;\n condition: boolean;\n nodes: NodeArg<TData, TProperties>[];\n removeEdges?: boolean;\n}): Node<TData, TProperties>[] | void => {\n if (condition) {\n return graph.addNodes(...nodes);\n } else {\n nodes.forEach(({ id }) => graph.removeNode(id, removeEdges));\n }\n};\n"],
|
|
5
|
-
"mappings": ";AAIA,SAASA,iBAAiB;AAE1B,SAASC,cAAc;AACvB,SAASC,iBAAiB;AAC1B,SAASC,mBAAmB;;;AC4DrB,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;;;;AD3GtG,IAAMO,UAAU;AA+BhB,IAAMC,QAAN,MAAMA;EAAN;AAIIC;;;kBAASC,OAAiC;MACjD,CAACH,OAAAA,GAAU;QAAEI,IAAIJ;QAASK,YAAY,CAAC;QAAGC,MAAM;MAAK;IACvD,CAAA;AAOSC;;;;;kBAASJ,OAAiC,CAAC,CAAA;
|
|
6
|
-
"names": ["untracked", "create", "invariant", "nonNullable", "isGraphNode", "data", "properties", "isAction", "actionGroupSymbol", "Symbol", "isActionGroup", "isActionLike", "ROOT_ID", "Graph", "_nodes", "create", "id", "properties", "data", "_edges", "_constructNode", "nodeBase", "node", "edges", "direction", "getEdgeKey", "nodes", "filter", "_getNodes", "n", "isActionLike", "find", "actions", "root", "findNode", "toJSON", "maxLength", "obj", "length", "slice", "label", "map", "
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { untracked } from '@preact/signals-core';\n\nimport { create } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { nonNullable } from '@dxos/util';\n\nimport { isActionLike, type EdgeDirection, type Node, type NodeArg, type NodeBase } from './node';\n\nexport const ROOT_ID = 'root';\n\nexport type TraversalOptions = {\n /**\n * The node to start traversing from.\n *\n * @default root\n */\n node?: Node;\n\n /**\n * The direction to traverse graph edges.\n *\n * @default 'outbound'\n */\n direction?: EdgeDirection;\n\n /**\n * A predicate to filter nodes which are passed to the `visitor` callback.\n */\n filter?: (node: Node) => boolean;\n\n /**\n * A callback which is called for each node visited during traversal.\n */\n visitor?: (node: Node, path: string[]) => void;\n};\n\n/**\n * The Graph represents the structure of the application constructed via plugins.\n */\nexport class Graph {\n /**\n * @internal\n */\n readonly _nodes = create<Record<string, NodeBase>>({\n [ROOT_ID]: { id: ROOT_ID, properties: {}, data: null },\n });\n\n /**\n * @internal\n */\n // Key is the `${node.id}-${direction}` and value is an ordered list of node ids.\n // Explicit type required because TS says this is not portable.\n readonly _edges = create<Record<string, string[]>>({});\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 = node.nodes();\n const obj: Record<string, any> = {\n id: node.id.length > maxLength ? `${node.id.slice(0, maxLength - 3)}...` : node.id,\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 findNode(id: string): Node | undefined {\n const nodeBase = this._nodes[id];\n if (!nodeBase) {\n return undefined;\n }\n\n return this._constructNode(nodeBase);\n }\n\n private _constructNode = (nodeBase: NodeBase): Node => {\n const node: Node = {\n ...nodeBase,\n edges: ({ direction = 'outbound' } = {}) => {\n return this._edges[this.getEdgeKey(node.id, direction)];\n },\n nodes: ({ direction, filter } = {}) => {\n const nodes = this._getNodes({ id: node.id, direction }).filter((n) => !isActionLike(n));\n return filter ? nodes.filter((n) => filter(n, node)) : nodes;\n },\n node: (id: string) => {\n return this._getNodes({ id }).find((node) => node.id === id);\n },\n actions: () => {\n return this._getNodes({ id: node.id }).filter(isActionLike);\n },\n };\n\n return node;\n };\n\n private _getNodes({ id, direction = 'outbound' }: { id: string; direction?: EdgeDirection }): Node[] {\n const edges = this._edges[this.getEdgeKey(id, direction)];\n if (!edges) {\n return [];\n }\n\n return edges.map((id) => this.findNode(id)).filter(nonNullable);\n }\n\n private getEdgeKey(id: string, direction: EdgeDirection) {\n return `${id}-${direction}`;\n }\n\n /**\n * Add nodes to the graph.\n */\n addNodes<TData = null, TProperties extends Record<string, any> = Record<string, any>>(\n ...nodes: NodeArg<TData, TProperties>[]\n ): Node<TData, TProperties>[] {\n return 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 node = { data: null, properties: {}, ..._node };\n this._nodes[node.id] = node;\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, direction]) =>\n direction === 'outbound'\n ? this.addEdge({ source: node.id, target: id })\n : this.addEdge({ source: id, target: node.id }),\n );\n }\n\n return this._constructNode(node) as Node<TData, TProperties>;\n });\n }\n\n /**\n * Remove nodes from the graph.\n *\n * @param id The id of the node to remove.\n * @param edges Whether to remove edges connected to the node from the graph as well.\n */\n 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 node.\n delete this._edges[this.getEdgeKey(id, 'outbound')];\n delete this._edges[this.getEdgeKey(id, 'inbound')];\n\n // Remove edges from connected nodes.\n this._getNodes({ id }).forEach((node) => this.removeEdge({ source: id, target: node.id }));\n this._getNodes({ id, direction: 'inbound' }).forEach((node) =>\n this.removeEdge({ source: node.id, target: id }),\n );\n }\n\n // Remove node.\n delete this._nodes[id];\n });\n }\n\n /**\n * Add an edge to the graph.\n */\n addEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n const outbound = this._edges[this.getEdgeKey(source, 'outbound')];\n if (!outbound) {\n this._edges[this.getEdgeKey(source, 'outbound')] = [target];\n } else if (!outbound.includes(target)) {\n outbound.push(target);\n }\n\n const inbound = this._edges[this.getEdgeKey(target, 'inbound')];\n if (!inbound) {\n this._edges[this.getEdgeKey(target, 'inbound')] = [source];\n } else if (!inbound.includes(source)) {\n inbound.push(source);\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 direction The direction of the edges from the node to sort.\n * @param edges The ordered list of edges.\n */\n sortEdges(nodeId: string, direction: EdgeDirection, edges: string[]) {\n untracked(() => {\n const current = this._edges[this.getEdgeKey(nodeId, direction)];\n if (current) {\n const unsorted = current.filter((id) => !edges.includes(id)) ?? [];\n const sorted = edges.filter((id) => current.includes(id)) ?? [];\n current.splice(0, current.length, ...[...sorted, ...unsorted]);\n }\n });\n }\n\n /**\n * Remove an edge from the graph.\n */\n removeEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n const outboundIndex = this._edges[this.getEdgeKey(source, 'outbound')]?.findIndex((id) => id === target);\n if (outboundIndex !== -1) {\n this._edges[this.getEdgeKey(source, 'outbound')].splice(outboundIndex, 1);\n }\n\n const inboundIndex = this._edges[this.getEdgeKey(target, 'inbound')]?.findIndex((id) => id === source);\n if (inboundIndex !== -1) {\n this._edges[this.getEdgeKey(target, 'inbound')].splice(inboundIndex, 1);\n }\n });\n }\n\n /**\n * Recursive depth-first traversal.\n *\n * @param options.node The node to start traversing from.\n * @param options.direction The direction to traverse graph edges.\n * @param options.filter A predicate to filter nodes which are passed to the `visitor` callback.\n * @param options.visitor A callback which is called for each node visited during traversal.\n */\n traverse({ node = this.root, direction = 'outbound', filter, visitor }: TraversalOptions, path: string[] = []): void {\n // Break cycles.\n if (path.includes(node.id)) {\n return;\n }\n\n if (!filter || filter(node)) {\n visitor?.(node, [...path, node.id]);\n }\n\n Object.values(this._getNodes({ id: node.id, direction })).forEach((child) =>\n this.traverse({ node: child, direction, filter, visitor }, [...path, node.id]),\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 filter: () => !found,\n visitor: (node, path) => {\n if (node.id === target) {\n found = path;\n }\n },\n });\n\n return found;\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 NodeBase<TData = any, TProperties extends Record<string, any> = Record<string, any>> = {\n /**\n * Globally unique ID.\n */\n id: string;\n\n /**\n * Properties of the node relevant to displaying the node.\n */\n properties: 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 EdgeDirection = 'outbound' | 'inbound';\n\nexport type ConnectedNodes = {\n /**\n * Edges that this node is connected to in default order.\n */\n edges(params?: { direction?: EdgeDirection }): Readonly<string[]>;\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>>(params?: {\n direction?: EdgeDirection;\n filter?: NodeFilter<T, U>;\n }): Node<T>[];\n\n /**\n * Get a specific connected node by id.\n */\n node(id: string): Node | undefined;\n};\n\nexport type ConnectedActions = {\n /**\n * Actions or action groups that this node is connected to in default order.\n */\n actions(): ActionLike[];\n};\n\nexport type Node<TData = any, TProperties extends Record<string, any> = Record<string, any>> = Readonly<\n Omit<NodeBase<TData, TProperties>, 'properties'> & { properties: Readonly<TProperties> } & ConnectedNodes &\n ConnectedActions\n>;\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 NodeBase<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, EdgeDirection][];\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<NodeBase<ActionData, TProperties>, 'properties'> & {\n properties: Readonly<TProperties>;\n } & ConnectedNodes\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<NodeBase<typeof actionGroupSymbol, Record<string, any>>, 'properties'> & {\n properties: Readonly<Record<string, any>>;\n } & ConnectedActions\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 { EventSubscriptions, type UnsubscribeCallback } from '@dxos/async';\n\nimport { Graph } from './graph';\n\nexport type BuilderExtension = (graph: Graph) => UnsubscribeCallback | void;\n\n/**\n * The builder provides an extensible way to compose the construction of the graph.\n */\nexport class GraphBuilder {\n private readonly _extensions = new Map<string, BuilderExtension>();\n private readonly _unsubscribe = new EventSubscriptions();\n\n /**\n * Register a node builder which will be called in order to construct the graph.\n */\n addExtension(id: string, extension: BuilderExtension): GraphBuilder {\n this._extensions.set(id, extension);\n return this;\n }\n\n /**\n * Remove a node builder from the graph builder.\n */\n removeExtension(id: string): GraphBuilder {\n this._extensions.delete(id);\n return this;\n }\n\n /**\n * Construct the graph, starting by calling all registered extensions.\n * @param previousGraph If provided, the graph will be updated in place.\n */\n build(previousGraph?: Graph): Graph {\n // Clear previous extension subscriptions.\n this._unsubscribe.clear();\n\n const graph: Graph = previousGraph ?? new Graph();\n\n Array.from(this._extensions.values()).forEach((builder) => {\n const unsubscribe = builder(graph);\n unsubscribe && this._unsubscribe.add(unsubscribe);\n });\n\n return graph;\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Graph } from './graph';\nimport { type Node, type NodeArg } from './node';\n\n/**\n * If the condition is true, adds the nodes to the graph, otherwise removes the nodes from the graph.\n */\nexport const manageNodes = <TData = null, TProperties extends Record<string, any> = Record<string, any>>({\n graph,\n condition,\n nodes,\n removeEdges,\n}: {\n graph: Graph;\n condition: boolean;\n nodes: NodeArg<TData, TProperties>[];\n removeEdges?: boolean;\n}): Node<TData, TProperties>[] | void => {\n if (condition) {\n return graph.addNodes(...nodes);\n } else {\n nodes.forEach(({ id }) => graph.removeNode(id, removeEdges));\n }\n};\n"],
|
|
5
|
+
"mappings": ";AAIA,SAASA,iBAAiB;AAE1B,SAASC,cAAc;AACvB,SAASC,iBAAiB;AAC1B,SAASC,mBAAmB;;;AC4DrB,IAAMC,cAAc,CAACC,SAC1BA,QA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A+D;AAC9F,UAAMsB,QAAQ,KAAK5C,SAASqB,MAAAA;AAC5B,QAAI,CAACuB,OAAO;AACV,aAAOjC;IACT;AAEA,QAAIkC;AACJ,SAAKR,SAAS;MACZjD,MAAMwD;MACNnD,QAAQ,MAAM,CAACoD;MACfP,SAAS,CAAClD,MAAMmD,SAAAA;AACd,YAAInD,KAAKN,OAAOwC,QAAQ;AACtBuB,kBAAQN;QACV;MACF;IACF,CAAA;AAEA,WAAOM;EACT;AACF;;;AElTA,SAASC,0BAAoD;AAStD,IAAMC,eAAN,MAAMA;EAAN;AACYC,uBAAc,oBAAIC,IAAAA;AAClBC,wBAAe,IAAIC,mBAAAA;;;;;EAKpCC,aAAaC,IAAYC,WAA2C;AAClE,SAAKN,YAAYO,IAAIF,IAAIC,SAAAA;AACzB,WAAO;EACT;;;;EAKAE,gBAAgBH,IAA0B;AACxC,SAAKL,YAAYS,OAAOJ,EAAAA;AACxB,WAAO;EACT;;;;;EAMAK,MAAMC,eAA8B;AAElC,SAAKT,aAAaU,MAAK;AAEvB,UAAMC,QAAeF,iBAAiB,IAAIG,MAAAA;AAE1CC,UAAMC,KAAK,KAAKhB,YAAYiB,OAAM,CAAA,EAAIC,QAAQ,CAACC,YAAAA;AAC7C,YAAMC,cAAcD,QAAQN,KAAAA;AAC5BO,qBAAe,KAAKlB,aAAamB,IAAID,WAAAA;IACvC,CAAA;AAEA,WAAOP;EACT;AACF;;;ACxCO,IAAMS,cAAc,CAA8E,EACvGC,OACAC,WACAC,OACAC,YAAW,MAMZ;AACC,MAAIF,WAAW;AACb,WAAOD,MAAMI,SAAQ,GAAIF,KAAAA;EAC3B,OAAO;AACLA,UAAMG,QAAQ,CAAC,EAAEC,GAAE,MAAON,MAAMO,WAAWD,IAAIH,WAAAA,CAAAA;EACjD;AACF;",
|
|
6
|
+
"names": ["untracked", "create", "invariant", "nonNullable", "isGraphNode", "data", "properties", "isAction", "actionGroupSymbol", "Symbol", "isActionGroup", "isActionLike", "ROOT_ID", "Graph", "_nodes", "create", "id", "properties", "data", "_edges", "_constructNode", "nodeBase", "node", "edges", "direction", "getEdgeKey", "nodes", "filter", "_getNodes", "n", "isActionLike", "find", "actions", "root", "findNode", "toJSON", "maxLength", "seen", "obj", "length", "slice", "label", "map", "nextSeen", "includes", "undefined", "nonNullable", "invariant", "addNodes", "_addNode", "_node", "untracked", "forEach", "subNode", "addEdge", "source", "target", "removeNode", "removeEdge", "outbound", "push", "inbound", "sortEdges", "nodeId", "current", "unsorted", "sorted", "splice", "outboundIndex", "findIndex", "inboundIndex", "traverse", "visitor", "path", "Object", "values", "child", "getPath", "start", "found", "EventSubscriptions", "GraphBuilder", "_extensions", "Map", "_unsubscribe", "EventSubscriptions", "addExtension", "id", "extension", "set", "removeExtension", "delete", "build", "previousGraph", "clear", "graph", "Graph", "Array", "from", "values", "forEach", "builder", "unsubscribe", "add", "manageNodes", "graph", "condition", "nodes", "removeEdges", "addNodes", "forEach", "id", "removeNode"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/sdk/app-graph/src/node.ts":{"bytes":6447,"imports":[],"format":"esm"},"packages/sdk/app-graph/src/graph.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/sdk/app-graph/src/node.ts":{"bytes":6447,"imports":[],"format":"esm"},"packages/sdk/app-graph/src/graph.ts":{"bytes":31580,"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/node.ts","kind":"import-statement","original":"./node"}],"format":"esm"},"packages/sdk/app-graph/src/graph-builder.ts":{"bytes":4684,"imports":[{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"packages/sdk/app-graph/src/graph.ts","kind":"import-statement","original":"./graph"}],"format":"esm"},"packages/sdk/app-graph/src/helpers.ts":{"bytes":2079,"imports":[],"format":"esm"},"packages/sdk/app-graph/src/index.ts":{"bytes":759,"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/helpers.ts","kind":"import-statement","original":"./helpers"},{"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":22904},"packages/sdk/app-graph/dist/lib/browser/index.mjs":{"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":"@dxos/async","kind":"import-statement","external":true}],"exports":["Graph","GraphBuilder","ROOT_ID","actionGroupSymbol","isAction","isActionGroup","isActionLike","isGraphNode","manageNodes"],"entryPoint":"packages/sdk/app-graph/src/index.ts","inputs":{"packages/sdk/app-graph/src/graph.ts":{"bytesInOutput":7855},"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":983},"packages/sdk/app-graph/src/helpers.ts":{"bytesInOutput":206}},"bytes":9907}}}
|
package/dist/lib/node/index.cjs
CHANGED
|
@@ -88,7 +88,7 @@ var Graph = class {
|
|
|
88
88
|
* Convert the graph to a JSON object.
|
|
89
89
|
*/
|
|
90
90
|
toJSON({ id = ROOT_ID, maxLength = 32 } = {}) {
|
|
91
|
-
const toJSON = (node) => {
|
|
91
|
+
const toJSON = (node, seen = []) => {
|
|
92
92
|
const nodes = node.nodes();
|
|
93
93
|
const obj = {
|
|
94
94
|
id: node.id.length > maxLength ? `${node.id.slice(0, maxLength - 3)}...` : node.id
|
|
@@ -97,14 +97,20 @@ var Graph = class {
|
|
|
97
97
|
obj.label = node.properties.label;
|
|
98
98
|
}
|
|
99
99
|
if (nodes.length) {
|
|
100
|
-
obj.nodes = nodes.map((
|
|
100
|
+
obj.nodes = nodes.map((n) => {
|
|
101
|
+
const nextSeen = [
|
|
102
|
+
...seen,
|
|
103
|
+
node.id
|
|
104
|
+
];
|
|
105
|
+
return nextSeen.includes(n.id) ? void 0 : toJSON(n, nextSeen);
|
|
106
|
+
}).filter(import_util.nonNullable);
|
|
101
107
|
}
|
|
102
108
|
return obj;
|
|
103
109
|
};
|
|
104
110
|
const root = this.findNode(id);
|
|
105
111
|
(0, import_invariant.invariant)(root, `Node not found: ${id}`, {
|
|
106
112
|
F: __dxlog_file,
|
|
107
|
-
L:
|
|
113
|
+
L: 91,
|
|
108
114
|
S: this,
|
|
109
115
|
A: [
|
|
110
116
|
"root",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/graph.ts", "../../../src/node.ts", "../../../src/graph-builder.ts", "../../../src/helpers.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { untracked } from '@preact/signals-core';\n\nimport { create } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { nonNullable } from '@dxos/util';\n\nimport { isActionLike, type EdgeDirection, type Node, type NodeArg, type NodeBase } from './node';\n\nexport const ROOT_ID = 'root';\n\nexport type TraversalOptions = {\n /**\n * The node to start traversing from.\n *\n * @default root\n */\n node?: Node;\n\n /**\n * The direction to traverse graph edges.\n *\n * @default 'outbound'\n */\n direction?: EdgeDirection;\n\n /**\n * A predicate to filter nodes which are passed to the `visitor` callback.\n */\n filter?: (node: Node) => boolean;\n\n /**\n * A callback which is called for each node visited during traversal.\n */\n visitor?: (node: Node, path: string[]) => void;\n};\n\n/**\n * The Graph represents the structure of the application constructed via plugins.\n */\nexport class Graph {\n /**\n * @internal\n */\n readonly _nodes = create<Record<string, NodeBase>>({\n [ROOT_ID]: { id: ROOT_ID, properties: {}, data: null },\n });\n\n /**\n * @internal\n */\n // Key is the `${node.id}-${direction}` and value is an ordered list of node ids.\n // Explicit type required because TS says this is not portable.\n readonly _edges = create<Record<string, string[]>>({});\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): any => {\n const nodes = node.nodes();\n const obj: Record<string, any> = {\n id: node.id.length > maxLength ? `${node.id.slice(0, maxLength - 3)}...` : node.id,\n };\n if (node.properties.label) {\n obj.label = node.properties.label;\n }\n if (nodes.length) {\n obj.nodes = nodes.map((node) => toJSON(node));\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 findNode(id: string): Node | undefined {\n const nodeBase = this._nodes[id];\n if (!nodeBase) {\n return undefined;\n }\n\n return this._constructNode(nodeBase);\n }\n\n private _constructNode = (nodeBase: NodeBase): Node => {\n const node: Node = {\n ...nodeBase,\n edges: ({ direction = 'outbound' } = {}) => {\n return this._edges[this.getEdgeKey(node.id, direction)];\n },\n nodes: ({ direction, filter } = {}) => {\n const nodes = this._getNodes({ id: node.id, direction }).filter((n) => !isActionLike(n));\n return filter ? nodes.filter((n) => filter(n, node)) : nodes;\n },\n node: (id: string) => {\n return this._getNodes({ id }).find((node) => node.id === id);\n },\n actions: () => {\n return this._getNodes({ id: node.id }).filter(isActionLike);\n },\n };\n\n return node;\n };\n\n private _getNodes({ id, direction = 'outbound' }: { id: string; direction?: EdgeDirection }): Node[] {\n const edges = this._edges[this.getEdgeKey(id, direction)];\n if (!edges) {\n return [];\n }\n\n return edges.map((id) => this.findNode(id)).filter(nonNullable);\n }\n\n private getEdgeKey(id: string, direction: EdgeDirection) {\n return `${id}-${direction}`;\n }\n\n /**\n * Add nodes to the graph.\n */\n addNodes<TData = null, TProperties extends Record<string, any> = Record<string, any>>(\n ...nodes: NodeArg<TData, TProperties>[]\n ): Node<TData, TProperties>[] {\n return 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 node = { data: null, properties: {}, ..._node };\n this._nodes[node.id] = node;\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, direction]) =>\n direction === 'outbound'\n ? this.addEdge({ source: node.id, target: id })\n : this.addEdge({ source: id, target: node.id }),\n );\n }\n\n return this._constructNode(node) as Node<TData, TProperties>;\n });\n }\n\n /**\n * Remove nodes from the graph.\n *\n * @param id The id of the node to remove.\n * @param edges Whether to remove edges connected to the node from the graph as well.\n */\n 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 node.\n delete this._edges[this.getEdgeKey(id, 'outbound')];\n delete this._edges[this.getEdgeKey(id, 'inbound')];\n\n // Remove edges from connected nodes.\n this._getNodes({ id }).forEach((node) => this.removeEdge({ source: id, target: node.id }));\n this._getNodes({ id, direction: 'inbound' }).forEach((node) =>\n this.removeEdge({ source: node.id, target: id }),\n );\n }\n\n // Remove node.\n delete this._nodes[id];\n });\n }\n\n /**\n * Add an edge to the graph.\n */\n addEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n const outbound = this._edges[this.getEdgeKey(source, 'outbound')];\n if (!outbound) {\n this._edges[this.getEdgeKey(source, 'outbound')] = [target];\n } else if (!outbound.includes(target)) {\n outbound.push(target);\n }\n\n const inbound = this._edges[this.getEdgeKey(target, 'inbound')];\n if (!inbound) {\n this._edges[this.getEdgeKey(target, 'inbound')] = [source];\n } else if (!inbound.includes(source)) {\n inbound.push(source);\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 direction The direction of the edges from the node to sort.\n * @param edges The ordered list of edges.\n */\n sortEdges(nodeId: string, direction: EdgeDirection, edges: string[]) {\n untracked(() => {\n const current = this._edges[this.getEdgeKey(nodeId, direction)];\n if (current) {\n const unsorted = current.filter((id) => !edges.includes(id)) ?? [];\n const sorted = edges.filter((id) => current.includes(id)) ?? [];\n current.splice(0, current.length, ...[...sorted, ...unsorted]);\n }\n });\n }\n\n /**\n * Remove an edge from the graph.\n */\n removeEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n const outboundIndex = this._edges[this.getEdgeKey(source, 'outbound')]?.findIndex((id) => id === target);\n if (outboundIndex !== -1) {\n this._edges[this.getEdgeKey(source, 'outbound')].splice(outboundIndex, 1);\n }\n\n const inboundIndex = this._edges[this.getEdgeKey(target, 'inbound')]?.findIndex((id) => id === source);\n if (inboundIndex !== -1) {\n this._edges[this.getEdgeKey(target, 'inbound')].splice(inboundIndex, 1);\n }\n });\n }\n\n /**\n * Recursive depth-first traversal.\n *\n * @param options.node The node to start traversing from.\n * @param options.direction The direction to traverse graph edges.\n * @param options.filter A predicate to filter nodes which are passed to the `visitor` callback.\n * @param options.visitor A callback which is called for each node visited during traversal.\n */\n traverse({ node = this.root, direction = 'outbound', filter, visitor }: TraversalOptions, path: string[] = []): void {\n // Break cycles.\n if (path.includes(node.id)) {\n return;\n }\n\n if (!filter || filter(node)) {\n visitor?.(node, [...path, node.id]);\n }\n\n Object.values(this._getNodes({ id: node.id, direction })).forEach((child) =>\n this.traverse({ node: child, direction, filter, visitor }, [...path, node.id]),\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 filter: () => !found,\n visitor: (node, path) => {\n if (node.id === target) {\n found = path;\n }\n },\n });\n\n return found;\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 NodeBase<TData = any, TProperties extends Record<string, any> = Record<string, any>> = {\n /**\n * Globally unique ID.\n */\n id: string;\n\n /**\n * Properties of the node relevant to displaying the node.\n */\n properties: 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 EdgeDirection = 'outbound' | 'inbound';\n\nexport type ConnectedNodes = {\n /**\n * Edges that this node is connected to in default order.\n */\n edges(params?: { direction?: EdgeDirection }): Readonly<string[]>;\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>>(params?: {\n direction?: EdgeDirection;\n filter?: NodeFilter<T, U>;\n }): Node<T>[];\n\n /**\n * Get a specific connected node by id.\n */\n node(id: string): Node | undefined;\n};\n\nexport type ConnectedActions = {\n /**\n * Actions or action groups that this node is connected to in default order.\n */\n actions(): ActionLike[];\n};\n\nexport type Node<TData = any, TProperties extends Record<string, any> = Record<string, any>> = Readonly<\n Omit<NodeBase<TData, TProperties>, 'properties'> & { properties: Readonly<TProperties> } & ConnectedNodes &\n ConnectedActions\n>;\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 NodeBase<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, EdgeDirection][];\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<NodeBase<ActionData, TProperties>, 'properties'> & {\n properties: Readonly<TProperties>;\n } & ConnectedNodes\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<NodeBase<typeof actionGroupSymbol, Record<string, any>>, 'properties'> & {\n properties: Readonly<Record<string, any>>;\n } & ConnectedActions\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 { EventSubscriptions, type UnsubscribeCallback } from '@dxos/async';\n\nimport { Graph } from './graph';\n\nexport type BuilderExtension = (graph: Graph) => UnsubscribeCallback | void;\n\n/**\n * The builder provides an extensible way to compose the construction of the graph.\n */\nexport class GraphBuilder {\n private readonly _extensions = new Map<string, BuilderExtension>();\n private readonly _unsubscribe = new EventSubscriptions();\n\n /**\n * Register a node builder which will be called in order to construct the graph.\n */\n addExtension(id: string, extension: BuilderExtension): GraphBuilder {\n this._extensions.set(id, extension);\n return this;\n }\n\n /**\n * Remove a node builder from the graph builder.\n */\n removeExtension(id: string): GraphBuilder {\n this._extensions.delete(id);\n return this;\n }\n\n /**\n * Construct the graph, starting by calling all registered extensions.\n * @param previousGraph If provided, the graph will be updated in place.\n */\n build(previousGraph?: Graph): Graph {\n // Clear previous extension subscriptions.\n this._unsubscribe.clear();\n\n const graph: Graph = previousGraph ?? new Graph();\n\n Array.from(this._extensions.values()).forEach((builder) => {\n const unsubscribe = builder(graph);\n unsubscribe && this._unsubscribe.add(unsubscribe);\n });\n\n return graph;\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Graph } from './graph';\nimport { type Node, type NodeArg } from './node';\n\n/**\n * If the condition is true, adds the nodes to the graph, otherwise removes the nodes from the graph.\n */\nexport const manageNodes = <TData = null, TProperties extends Record<string, any> = Record<string, any>>({\n graph,\n condition,\n nodes,\n removeEdges,\n}: {\n graph: Graph;\n condition: boolean;\n nodes: NodeArg<TData, TProperties>[];\n removeEdges?: boolean;\n}): Node<TData, TProperties>[] | void => {\n if (condition) {\n return graph.addNodes(...nodes);\n } else {\n nodes.forEach(({ id }) => graph.removeNode(id, removeEdges));\n }\n};\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,0BAA0B;AAE1B,yBAAuB;AACvB,uBAA0B;AAC1B,kBAA4B;AEJ5B,mBAA6D;ADgEtD,IAAMA,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;;AD3GtG,IAAMO,UAAU;AA+BhB,IAAMC,QAAN,MAAMA;EAAN,cAAA;sBAIaC,2BAAiC;MACjD,CAACF,OAAAA,GAAU;QAAEG,IAAIH;QAASN,YAAY,CAAC;QAAGD,MAAM;MAAK;IACvD,CAAA;sBAOkBS,2BAAiC,CAAC,CAAA;
|
|
6
|
-
"names": ["isGraphNode", "data", "properties", "isAction", "actionGroupSymbol", "Symbol", "isActionGroup", "isActionLike", "ROOT_ID", "Graph", "create", "id", "_constructNode", "nodeBase", "node", "edges", "direction", "_edges", "getEdgeKey", "nodes", "filter", "_getNodes", "n", "find", "actions", "root", "findNode", "toJSON", "maxLength", "obj", "length", "slice", "label", "map", "
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { untracked } from '@preact/signals-core';\n\nimport { create } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { nonNullable } from '@dxos/util';\n\nimport { isActionLike, type EdgeDirection, type Node, type NodeArg, type NodeBase } from './node';\n\nexport const ROOT_ID = 'root';\n\nexport type TraversalOptions = {\n /**\n * The node to start traversing from.\n *\n * @default root\n */\n node?: Node;\n\n /**\n * The direction to traverse graph edges.\n *\n * @default 'outbound'\n */\n direction?: EdgeDirection;\n\n /**\n * A predicate to filter nodes which are passed to the `visitor` callback.\n */\n filter?: (node: Node) => boolean;\n\n /**\n * A callback which is called for each node visited during traversal.\n */\n visitor?: (node: Node, path: string[]) => void;\n};\n\n/**\n * The Graph represents the structure of the application constructed via plugins.\n */\nexport class Graph {\n /**\n * @internal\n */\n readonly _nodes = create<Record<string, NodeBase>>({\n [ROOT_ID]: { id: ROOT_ID, properties: {}, data: null },\n });\n\n /**\n * @internal\n */\n // Key is the `${node.id}-${direction}` and value is an ordered list of node ids.\n // Explicit type required because TS says this is not portable.\n readonly _edges = create<Record<string, string[]>>({});\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 = node.nodes();\n const obj: Record<string, any> = {\n id: node.id.length > maxLength ? `${node.id.slice(0, maxLength - 3)}...` : node.id,\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 findNode(id: string): Node | undefined {\n const nodeBase = this._nodes[id];\n if (!nodeBase) {\n return undefined;\n }\n\n return this._constructNode(nodeBase);\n }\n\n private _constructNode = (nodeBase: NodeBase): Node => {\n const node: Node = {\n ...nodeBase,\n edges: ({ direction = 'outbound' } = {}) => {\n return this._edges[this.getEdgeKey(node.id, direction)];\n },\n nodes: ({ direction, filter } = {}) => {\n const nodes = this._getNodes({ id: node.id, direction }).filter((n) => !isActionLike(n));\n return filter ? nodes.filter((n) => filter(n, node)) : nodes;\n },\n node: (id: string) => {\n return this._getNodes({ id }).find((node) => node.id === id);\n },\n actions: () => {\n return this._getNodes({ id: node.id }).filter(isActionLike);\n },\n };\n\n return node;\n };\n\n private _getNodes({ id, direction = 'outbound' }: { id: string; direction?: EdgeDirection }): Node[] {\n const edges = this._edges[this.getEdgeKey(id, direction)];\n if (!edges) {\n return [];\n }\n\n return edges.map((id) => this.findNode(id)).filter(nonNullable);\n }\n\n private getEdgeKey(id: string, direction: EdgeDirection) {\n return `${id}-${direction}`;\n }\n\n /**\n * Add nodes to the graph.\n */\n addNodes<TData = null, TProperties extends Record<string, any> = Record<string, any>>(\n ...nodes: NodeArg<TData, TProperties>[]\n ): Node<TData, TProperties>[] {\n return 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 node = { data: null, properties: {}, ..._node };\n this._nodes[node.id] = node;\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, direction]) =>\n direction === 'outbound'\n ? this.addEdge({ source: node.id, target: id })\n : this.addEdge({ source: id, target: node.id }),\n );\n }\n\n return this._constructNode(node) as Node<TData, TProperties>;\n });\n }\n\n /**\n * Remove nodes from the graph.\n *\n * @param id The id of the node to remove.\n * @param edges Whether to remove edges connected to the node from the graph as well.\n */\n 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 node.\n delete this._edges[this.getEdgeKey(id, 'outbound')];\n delete this._edges[this.getEdgeKey(id, 'inbound')];\n\n // Remove edges from connected nodes.\n this._getNodes({ id }).forEach((node) => this.removeEdge({ source: id, target: node.id }));\n this._getNodes({ id, direction: 'inbound' }).forEach((node) =>\n this.removeEdge({ source: node.id, target: id }),\n );\n }\n\n // Remove node.\n delete this._nodes[id];\n });\n }\n\n /**\n * Add an edge to the graph.\n */\n addEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n const outbound = this._edges[this.getEdgeKey(source, 'outbound')];\n if (!outbound) {\n this._edges[this.getEdgeKey(source, 'outbound')] = [target];\n } else if (!outbound.includes(target)) {\n outbound.push(target);\n }\n\n const inbound = this._edges[this.getEdgeKey(target, 'inbound')];\n if (!inbound) {\n this._edges[this.getEdgeKey(target, 'inbound')] = [source];\n } else if (!inbound.includes(source)) {\n inbound.push(source);\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 direction The direction of the edges from the node to sort.\n * @param edges The ordered list of edges.\n */\n sortEdges(nodeId: string, direction: EdgeDirection, edges: string[]) {\n untracked(() => {\n const current = this._edges[this.getEdgeKey(nodeId, direction)];\n if (current) {\n const unsorted = current.filter((id) => !edges.includes(id)) ?? [];\n const sorted = edges.filter((id) => current.includes(id)) ?? [];\n current.splice(0, current.length, ...[...sorted, ...unsorted]);\n }\n });\n }\n\n /**\n * Remove an edge from the graph.\n */\n removeEdge({ source, target }: { source: string; target: string }) {\n untracked(() => {\n const outboundIndex = this._edges[this.getEdgeKey(source, 'outbound')]?.findIndex((id) => id === target);\n if (outboundIndex !== -1) {\n this._edges[this.getEdgeKey(source, 'outbound')].splice(outboundIndex, 1);\n }\n\n const inboundIndex = this._edges[this.getEdgeKey(target, 'inbound')]?.findIndex((id) => id === source);\n if (inboundIndex !== -1) {\n this._edges[this.getEdgeKey(target, 'inbound')].splice(inboundIndex, 1);\n }\n });\n }\n\n /**\n * Recursive depth-first traversal.\n *\n * @param options.node The node to start traversing from.\n * @param options.direction The direction to traverse graph edges.\n * @param options.filter A predicate to filter nodes which are passed to the `visitor` callback.\n * @param options.visitor A callback which is called for each node visited during traversal.\n */\n traverse({ node = this.root, direction = 'outbound', filter, visitor }: TraversalOptions, path: string[] = []): void {\n // Break cycles.\n if (path.includes(node.id)) {\n return;\n }\n\n if (!filter || filter(node)) {\n visitor?.(node, [...path, node.id]);\n }\n\n Object.values(this._getNodes({ id: node.id, direction })).forEach((child) =>\n this.traverse({ node: child, direction, filter, visitor }, [...path, node.id]),\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 filter: () => !found,\n visitor: (node, path) => {\n if (node.id === target) {\n found = path;\n }\n },\n });\n\n return found;\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 NodeBase<TData = any, TProperties extends Record<string, any> = Record<string, any>> = {\n /**\n * Globally unique ID.\n */\n id: string;\n\n /**\n * Properties of the node relevant to displaying the node.\n */\n properties: 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 EdgeDirection = 'outbound' | 'inbound';\n\nexport type ConnectedNodes = {\n /**\n * Edges that this node is connected to in default order.\n */\n edges(params?: { direction?: EdgeDirection }): Readonly<string[]>;\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>>(params?: {\n direction?: EdgeDirection;\n filter?: NodeFilter<T, U>;\n }): Node<T>[];\n\n /**\n * Get a specific connected node by id.\n */\n node(id: string): Node | undefined;\n};\n\nexport type ConnectedActions = {\n /**\n * Actions or action groups that this node is connected to in default order.\n */\n actions(): ActionLike[];\n};\n\nexport type Node<TData = any, TProperties extends Record<string, any> = Record<string, any>> = Readonly<\n Omit<NodeBase<TData, TProperties>, 'properties'> & { properties: Readonly<TProperties> } & ConnectedNodes &\n ConnectedActions\n>;\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 NodeBase<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, EdgeDirection][];\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<NodeBase<ActionData, TProperties>, 'properties'> & {\n properties: Readonly<TProperties>;\n } & ConnectedNodes\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<NodeBase<typeof actionGroupSymbol, Record<string, any>>, 'properties'> & {\n properties: Readonly<Record<string, any>>;\n } & ConnectedActions\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 { EventSubscriptions, type UnsubscribeCallback } from '@dxos/async';\n\nimport { Graph } from './graph';\n\nexport type BuilderExtension = (graph: Graph) => UnsubscribeCallback | void;\n\n/**\n * The builder provides an extensible way to compose the construction of the graph.\n */\nexport class GraphBuilder {\n private readonly _extensions = new Map<string, BuilderExtension>();\n private readonly _unsubscribe = new EventSubscriptions();\n\n /**\n * Register a node builder which will be called in order to construct the graph.\n */\n addExtension(id: string, extension: BuilderExtension): GraphBuilder {\n this._extensions.set(id, extension);\n return this;\n }\n\n /**\n * Remove a node builder from the graph builder.\n */\n removeExtension(id: string): GraphBuilder {\n this._extensions.delete(id);\n return this;\n }\n\n /**\n * Construct the graph, starting by calling all registered extensions.\n * @param previousGraph If provided, the graph will be updated in place.\n */\n build(previousGraph?: Graph): Graph {\n // Clear previous extension subscriptions.\n this._unsubscribe.clear();\n\n const graph: Graph = previousGraph ?? new Graph();\n\n Array.from(this._extensions.values()).forEach((builder) => {\n const unsubscribe = builder(graph);\n unsubscribe && this._unsubscribe.add(unsubscribe);\n });\n\n return graph;\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Graph } from './graph';\nimport { type Node, type NodeArg } from './node';\n\n/**\n * If the condition is true, adds the nodes to the graph, otherwise removes the nodes from the graph.\n */\nexport const manageNodes = <TData = null, TProperties extends Record<string, any> = Record<string, any>>({\n graph,\n condition,\n nodes,\n removeEdges,\n}: {\n graph: Graph;\n condition: boolean;\n nodes: NodeArg<TData, TProperties>[];\n removeEdges?: boolean;\n}): Node<TData, TProperties>[] | void => {\n if (condition) {\n return graph.addNodes(...nodes);\n } else {\n nodes.forEach(({ id }) => graph.removeNode(id, removeEdges));\n }\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,0BAA0B;AAE1B,yBAAuB;AACvB,uBAA0B;AAC1B,kBAA4B;AEJ5B,mBAA6D;ADgEtD,IAAMA,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;;AD3GtG,IAAMO,UAAU;AA+BhB,IAAMC,QAAN,MAAMA;EAAN,cAAA;sBAIaC,2BAAiC;MACjD,CAACF,OAAAA,GAAU;QAAEG,IAAIH;QAASN,YAAY,CAAC;QAAGD,MAAM;MAAK;IACvD,CAAA;sBAOkBS,2BAAiC,CAAC,CAAA;AAkD5CE,SAAAA,iBAAiB,CAACC,aAAAA;AACxB,YAAMC,OAAa;QACjB,GAAGD;QACHE,OAAO,CAAC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AE1C8E,UAAMC,KAAK,KAAKd,YAAYN,OAAM,CAAA,EAAIvB,QAAQ,CAAC4C,YAAAA;AAC7C,YAAMC,cAAcD,QAAQH,KAAAA;AAC5BI,qBAAe,KAAKd,aAAae,IAAID,WAAAA;IACvC,CAAA;AAEA,WAAOJ;EACT;AACF;ACxCO,IAAMM,cAAc,CAA8E,EACvGN,OACAO,WACA1E,OACA2E,YAAW,MAMZ;AACC,MAAID,WAAW;AACb,WAAOP,MAAM7C,SAAQ,GAAItB,KAAAA;EAC3B,OAAO;AACLA,UAAM0B,QAAQ,CAAC,EAAElC,GAAE,MAAO2E,MAAMpC,WAAWvC,IAAImF,WAAAA,CAAAA;EACjD;AACF;",
|
|
6
|
+
"names": ["isGraphNode", "data", "properties", "isAction", "actionGroupSymbol", "Symbol", "isActionGroup", "isActionLike", "ROOT_ID", "Graph", "create", "id", "_constructNode", "nodeBase", "node", "edges", "direction", "_edges", "getEdgeKey", "nodes", "filter", "_getNodes", "n", "find", "actions", "root", "findNode", "toJSON", "maxLength", "seen", "obj", "length", "slice", "label", "map", "nextSeen", "includes", "undefined", "nonNullable", "invariant", "_nodes", "addNodes", "_addNode", "_node", "untracked", "forEach", "subNode", "addEdge", "source", "target", "removeNode", "removeEdge", "outbound", "push", "inbound", "sortEdges", "nodeId", "current", "unsorted", "sorted", "splice", "outboundIndex", "findIndex", "inboundIndex", "traverse", "visitor", "path", "Object", "values", "child", "getPath", "start", "found", "GraphBuilder", "_extensions", "Map", "_unsubscribe", "EventSubscriptions", "addExtension", "extension", "set", "removeExtension", "delete", "build", "previousGraph", "clear", "graph", "Array", "from", "builder", "unsubscribe", "add", "manageNodes", "condition", "removeEdges"]
|
|
7
7
|
}
|
package/dist/lib/node/meta.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/sdk/app-graph/src/node.ts":{"bytes":6447,"imports":[],"format":"esm"},"packages/sdk/app-graph/src/graph.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/sdk/app-graph/src/node.ts":{"bytes":6447,"imports":[],"format":"esm"},"packages/sdk/app-graph/src/graph.ts":{"bytes":31580,"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/node.ts","kind":"import-statement","original":"./node"}],"format":"esm"},"packages/sdk/app-graph/src/graph-builder.ts":{"bytes":4684,"imports":[{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"packages/sdk/app-graph/src/graph.ts","kind":"import-statement","original":"./graph"}],"format":"esm"},"packages/sdk/app-graph/src/helpers.ts":{"bytes":2079,"imports":[],"format":"esm"},"packages/sdk/app-graph/src/index.ts":{"bytes":759,"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/helpers.ts","kind":"import-statement","original":"./helpers"},{"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":22904},"packages/sdk/app-graph/dist/lib/node/index.cjs":{"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":"@dxos/async","kind":"import-statement","external":true}],"exports":["Graph","GraphBuilder","ROOT_ID","actionGroupSymbol","isAction","isActionGroup","isActionLike","isGraphNode","manageNodes"],"entryPoint":"packages/sdk/app-graph/src/index.ts","inputs":{"packages/sdk/app-graph/src/graph.ts":{"bytesInOutput":7855},"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":983},"packages/sdk/app-graph/src/helpers.ts":{"bytesInOutput":206}},"bytes":9907}}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph.d.ts","sourceRoot":"","sources":["../../../src/graph.ts"],"names":[],"mappings":"AAUA,OAAO,EAAgB,KAAK,aAAa,EAAE,KAAK,IAAI,EAAE,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAElG,eAAO,MAAM,OAAO,SAAS,CAAC;AAE9B,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;;;OAIG;IACH,IAAI,CAAC,EAAE,IAAI,CAAC;IAEZ;;;;OAIG;IACH,SAAS,CAAC,EAAE,aAAa,CAAC;IAE1B;;OAEG;IACH,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC;IAEjC;;OAEG;IACH,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC;CAChD,CAAC;AAEF;;GAEG;AACH,qBAAa,KAAK;IAehB;;OAEG;IACH,IAAI,IAAI;;6EAEP;IAED;;OAEG;IACH,MAAM,CAAC,EAAE,EAAY,EAAE,SAAc,EAAE,GAAE;QAAE,EAAE,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAA;KAAO;
|
|
1
|
+
{"version":3,"file":"graph.d.ts","sourceRoot":"","sources":["../../../src/graph.ts"],"names":[],"mappings":"AAUA,OAAO,EAAgB,KAAK,aAAa,EAAE,KAAK,IAAI,EAAE,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAElG,eAAO,MAAM,OAAO,SAAS,CAAC;AAE9B,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;;;OAIG;IACH,IAAI,CAAC,EAAE,IAAI,CAAC;IAEZ;;;;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;IACH,UAAU,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE;IAcjE;;;;;;;OAOG;IACH,QAAQ,CAAC,EAAE,IAAgB,EAAE,SAAsB,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,gBAAgB,EAAE,IAAI,GAAE,MAAM,EAAO,GAAG,IAAI;IAepH;;OAEG;IACH,OAAO,CAAC,EAAE,MAAe,EAAE,MAAM,EAAE,EAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,GAAG,MAAM,EAAE,GAAG,SAAS;CAmBhG"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/app-graph",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
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",
|
|
@@ -21,12 +21,12 @@
|
|
|
21
21
|
],
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@preact/signals-core": "^1.6.0",
|
|
24
|
-
"@dxos/async": "0.
|
|
25
|
-
"@dxos/
|
|
26
|
-
"@dxos/echo-signals": "0.
|
|
27
|
-
"@dxos/
|
|
28
|
-
"@dxos/
|
|
29
|
-
"@dxos/
|
|
24
|
+
"@dxos/async": "0.6.0",
|
|
25
|
+
"@dxos/echo-schema": "0.6.0",
|
|
26
|
+
"@dxos/echo-signals": "0.6.0",
|
|
27
|
+
"@dxos/debug": "0.6.0",
|
|
28
|
+
"@dxos/invariant": "0.6.0",
|
|
29
|
+
"@dxos/util": "0.6.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@phosphor-icons/react": "^2.1.5",
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"react": "^18.2.0",
|
|
36
36
|
"react-dom": "^18.2.0",
|
|
37
37
|
"vite": "^5.2.9",
|
|
38
|
-
"@dxos/random": "0.
|
|
39
|
-
"@dxos/react-client": "0.
|
|
40
|
-
"@dxos/react-ui": "0.
|
|
41
|
-
"@dxos/react-ui-theme": "0.
|
|
42
|
-
"@dxos/storybook-utils": "0.
|
|
38
|
+
"@dxos/random": "0.6.0",
|
|
39
|
+
"@dxos/react-client": "0.6.0",
|
|
40
|
+
"@dxos/react-ui": "0.6.0",
|
|
41
|
+
"@dxos/react-ui-theme": "0.6.0",
|
|
42
|
+
"@dxos/storybook-utils": "0.6.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"react": "^18.2.0",
|
package/src/graph.ts
CHANGED
|
@@ -67,7 +67,7 @@ export class Graph {
|
|
|
67
67
|
* Convert the graph to a JSON object.
|
|
68
68
|
*/
|
|
69
69
|
toJSON({ id = ROOT_ID, maxLength = 32 }: { id?: string; maxLength?: number } = {}) {
|
|
70
|
-
const toJSON = (node: Node): any => {
|
|
70
|
+
const toJSON = (node: Node, seen: string[] = []): any => {
|
|
71
71
|
const nodes = node.nodes();
|
|
72
72
|
const obj: Record<string, any> = {
|
|
73
73
|
id: node.id.length > maxLength ? `${node.id.slice(0, maxLength - 3)}...` : node.id,
|
|
@@ -76,7 +76,13 @@ export class Graph {
|
|
|
76
76
|
obj.label = node.properties.label;
|
|
77
77
|
}
|
|
78
78
|
if (nodes.length) {
|
|
79
|
-
obj.nodes = nodes
|
|
79
|
+
obj.nodes = nodes
|
|
80
|
+
.map((n) => {
|
|
81
|
+
// Break cycles.
|
|
82
|
+
const nextSeen = [...seen, node.id];
|
|
83
|
+
return nextSeen.includes(n.id) ? undefined : toJSON(n, nextSeen);
|
|
84
|
+
})
|
|
85
|
+
.filter(nonNullable);
|
|
80
86
|
}
|
|
81
87
|
return obj;
|
|
82
88
|
};
|