@dxos/app-graph 0.4.6 → 0.4.7-main.0a4f1cd
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 +289 -229
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +282 -239
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/graph-builder.d.ts +8 -16
- package/dist/types/src/graph-builder.d.ts.map +1 -1
- package/dist/types/src/graph.d.ts +71 -14
- package/dist/types/src/graph.d.ts.map +1 -1
- package/dist/types/src/helpers.d.ts +14 -0
- package/dist/types/src/helpers.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/node.d.ts +57 -47
- package/dist/types/src/node.d.ts.map +1 -1
- package/dist/types/src/stories/EchoGraph.stories.d.ts.map +1 -1
- package/package.json +10 -29
- package/src/graph-builder.ts +18 -206
- package/src/graph.test.ts +242 -146
- package/src/graph.ts +241 -48
- package/src/helpers.ts +27 -0
- package/src/index.ts +1 -1
- package/src/node.ts +78 -66
- package/src/stories/EchoGraph.stories.tsx +59 -27
- package/dist/lib/browser/testing.mjs +0 -98
- package/dist/lib/browser/testing.mjs.map +0 -7
- package/dist/lib/node/testing.cjs +0 -122
- package/dist/lib/node/testing.cjs.map +0 -7
- package/dist/types/src/action.d.ts +0 -68
- package/dist/types/src/action.d.ts.map +0 -1
- package/dist/types/src/testing.d.ts +0 -52
- package/dist/types/src/testing.d.ts.map +0 -1
- package/src/action.ts +0 -92
- package/src/testing.ts +0 -152
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/graph.ts", "../../../src/graph-builder.ts", "../../../src/
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { deepSignal } from 'deepsignal/react';\n// TODO(wittjosiah): Remove lodash dependency.\nimport get from 'lodash.get';\n\nimport { invariant } from '@dxos/invariant';\n\nimport { type Label } from './action';\nimport { type Node } from './node';\n\nexport type TraversalOptions = {\n /**\n * The node to start traversing from. Defaults to the root node.\n */\n node?: Node;\n\n /**\n * The direction to traverse the graph. Defaults to 'down'.\n */\n direction?: 'up' | 'down';\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 // TODO(wittjosiah): Should this support multiple paths to the same node?\n private readonly _index = deepSignal<Record<string, string[]>>({});\n\n constructor(private readonly _root: Node) {}\n\n toJSON() {\n const toLabel = (label: Label) => (Array.isArray(label) ? `${label[1].ns}[${label[0]}]` : label);\n const toJSON = (node: Node): any => {\n return {\n id: node.id.slice(0, 16),\n label: toLabel(node.label),\n children: node.children.length ? node.children.map((node) => toJSON(node)) : undefined,\n actions: node.actions.length\n ? node.actions.map(({ id, label }) => ({\n id,\n label: toLabel(label),\n }))\n : undefined,\n };\n };\n\n return toJSON(this._root);\n }\n\n /**\n * The root node of the graph which is the entry point for all knowledge.\n */\n get root(): Node {\n return this._root;\n }\n\n /**\n * Get the path through the graph from the root to the node with the given id.\n */\n getPath(id: string): string[] | undefined {\n return this._index[id];\n }\n\n /**\n * @internal\n */\n _setPath(id: string, path: string[]) {\n invariant(id && path, 'Invalid path.');\n this._index[id] = path;\n }\n\n /**\n * Find the node with the given id in the graph.\n */\n findNode(id: string): Node | undefined {\n const path = this.getPath(id);\n if (!path) {\n return undefined;\n }\n\n return path.length > 0 ? get(this._root, path) : this._root;\n }\n\n /**\n * Recursive breadth-first traversal.\n */\n traverse({ node = this._root, direction = 'down', filter, visitor }: TraversalOptions, depth = 0): void {\n if (!filter || filter(node)) {\n visitor?.(node, this.getPath(node.id)!);\n }\n\n if (direction === 'down') {\n Object.values(node.children).forEach((child) => this.traverse({ node: child, filter, visitor }));\n } else if (direction === 'up' && node.parent) {\n this.traverse({ node: node.parent, direction, filter, visitor }, depth + 1);\n }\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { untracked } from '@preact/signals-core';\nimport { type RevertDeepSignal, deepSignal } from 'deepsignal/react';\n\nimport { EventSubscriptions } from '@dxos/async';\nimport { Keyboard } from '@dxos/keyboard';\nimport { getHostPlatform } from '@dxos/util';\n\nimport type { ActionArg, Action } from './action';\nimport { Graph } from './graph';\nimport type { NodeArg, Node, NodeBuilder } from './node';\n\nexport const KEY_BINDING = 'KeyBinding';\n\n/**\n * The builder...\n */\nexport class GraphBuilder {\n private readonly _nodeBuilders = new Map<string, NodeBuilder>();\n private readonly _unsubscribe = new Map<string, EventSubscriptions>();\n\n /**\n * Register a node builder which will be called in order to construct the graph.\n */\n addNodeBuilder(id: string, builder: NodeBuilder): GraphBuilder {\n this._nodeBuilders.set(id, builder);\n return this;\n }\n\n /**\n * Remove a node builder from the graph builder.\n */\n removeNodeBuilder(id: string): GraphBuilder {\n this._nodeBuilders.delete(id);\n return this;\n }\n\n /**\n * Construct the graph, starting by calling all registered node builders on the root node.\n * Node builders will be filtered out as they are used such that they are only used once on any given path.\n * @param previousGraph If provided, the graph will be updated in place.\n * @param startingPath If provided, the graph will be updated starting at the given path.\n */\n build(previousGraph?: Graph, startingPath: string[] = []): Graph {\n const graph: Graph = previousGraph ?? new Graph(this._createNode(() => graph, { id: 'root', label: 'Root' }));\n return this._build(graph, graph.root, startingPath);\n }\n\n /**\n * Called recursively.\n */\n private _build(graph: Graph, node: Node, path: string[] = [], ignoreBuilders: string[] = []): Graph {\n // TODO(wittjosiah): Should this support multiple paths to the same node?\n graph._setPath(node.id, path);\n\n // TODO(burdon): Document.\n const subscriptions = this._unsubscribe.get(node.id) ?? new EventSubscriptions();\n subscriptions.clear();\n\n Array.from(this._nodeBuilders.entries())\n .filter(([id]) => ignoreBuilders.findIndex((ignore) => ignore === id) === -1)\n .forEach(([_, builder]) => {\n const unsubscribe = builder(node);\n unsubscribe && subscriptions.add(unsubscribe);\n });\n\n this._unsubscribe.set(node.id, subscriptions);\n\n return graph;\n }\n\n private _createNode<TData = null, TProperties extends Record<string, any> = Record<string, any>>(\n getGraph: () => Graph,\n partial: NodeArg<TData, TProperties>,\n path: string[] = [],\n ignoreBuilders: string[] = [],\n ): Node<TData, TProperties> {\n // TODO(burdon): Document implications and rationale of deepSignal.\n const node: Node<TData, TProperties> = deepSignal({\n parent: null,\n data: null as TData, // TODO(burdon): Allow null property?\n properties: {} as TProperties,\n childrenMap: {},\n actionsMap: {},\n // TODO(burdon): Document.\n ...partial,\n\n get children() {\n return Object.values(node.childrenMap);\n },\n get actions() {\n return Object.values(node.actionsMap);\n },\n\n //\n // Properties\n //\n\n addProperty: (key, value) => {\n untracked(() => {\n (node.properties as Record<string, any>)[key] = value;\n });\n },\n removeProperty: (key) => {\n untracked(() => {\n delete (node.properties as Record<string, any>)[key];\n });\n },\n\n //\n // Nodes\n //\n\n addNode: (builder, ...partials) => {\n return untracked(() => {\n return partials.map((partial) => {\n const builders = [...ignoreBuilders, builder];\n const childPath = [...path, 'childrenMap', partial.id];\n const child = this._createNode(getGraph, { ...partial, parent: node }, childPath, builders);\n node.childrenMap[child.id] = child;\n // TODO(burdon): Defer triggering recursive updates until task has completed.\n this._build(getGraph(), child, childPath, builders);\n return child;\n });\n });\n },\n removeNode: (id) => {\n return untracked(() => {\n const child = node.childrenMap[id];\n delete node.childrenMap[id];\n return child;\n });\n },\n\n //\n // Actions\n //\n\n addAction: (...partials) => {\n return untracked(() => {\n return partials.map((partial) => {\n const action = this._createAction(partial);\n let shortcut: string | undefined;\n if (typeof action.keyBinding === 'object') {\n const availablePlatforms = Object.keys(action.keyBinding);\n const platform = getHostPlatform();\n shortcut = availablePlatforms.includes(platform)\n ? action.keyBinding[platform]\n : platform === 'ios'\n ? action.keyBinding.macos // Fallback to macos if ios-specific bindings not provided.\n : platform === 'linux' || platform === 'unknown'\n ? action.keyBinding.windows // Fallback to windows if platform-specific bindings not provided.\n : undefined;\n } else {\n shortcut = action.keyBinding;\n }\n if (shortcut) {\n Keyboard.singleton.getContext(path.join('/')).bind({\n shortcut,\n handler: () => {\n action.invoke({ caller: KEY_BINDING });\n },\n data: action.label,\n });\n }\n\n node.actionsMap[action.id] = action;\n return action;\n });\n });\n },\n removeAction: (id) => {\n return untracked(() => {\n const action = node.actionsMap[id];\n if (action.keyBinding) {\n // keyboardjs.unbind(action.keyBinding);\n }\n\n delete node.actionsMap[id];\n return action;\n });\n },\n }) as RevertDeepSignal<Node<TData, TProperties>>;\n\n // Only actions added at this stage are available to subsequent builders.\n // `addNode` immediately passes the new node to other builders.\n // As such, actions added later with `addAction` are not available to those builders.\n // Having actions available to subsequent builders is useful for building groups.\n partial.actions && partial.actions.forEach((action) => node.addAction(action));\n\n return node;\n }\n\n private _createAction<TProperties extends Record<string, any> = Record<string, any>>(\n partial: ActionArg<TProperties>,\n ): Action<TProperties> {\n const action: Action<TProperties> = deepSignal({\n properties: {} as TProperties,\n ...partial,\n actionsMap: {},\n get actions() {\n return Object.values(action.actionsMap);\n },\n addAction: (...partials) => {\n return untracked(() => {\n return partials.map((partial) => {\n const subAction = this._createAction(partial);\n action.actionsMap[subAction.id] = subAction;\n return subAction;\n });\n });\n },\n removeAction: (id) => {\n return untracked(() => {\n const subAction = action.actionsMap[id];\n delete action.actionsMap[id];\n return subAction;\n });\n },\n addProperty: (key, value) => {\n return untracked(() => {\n (action.properties as Record<string, any>)[key] = value;\n });\n },\n removeProperty: (key) => {\n return untracked(() => {\n delete (action.properties as Record<string, any>)[key];\n });\n },\n }) as RevertDeepSignal<Action<TProperties>>;\n\n partial.actions && partial.actions.forEach((subAction) => action.addAction(subAction));\n\n return action;\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport type { IconProps } from '@phosphor-icons/react';\nimport { type FC } from 'react';\n\nimport type { UnsubscribeCallback } from '@dxos/async';\n\nimport { type ActionArg, type Action, type Label } from './action';\n\n/**\n * Called when a node is added to the graph, allowing other node builders to add children, actions or properties.\n */\nexport type NodeBuilder = (parent: Node) => UnsubscribeCallback | void;\n\n/**\n * Represents a node in the graph.\n */\nexport type Node<TData = any, TProperties extends Record<string, any> = Record<string, any>> = {\n /**\n * Globally unique ID.\n */\n id: string;\n\n /**\n * Parent node in the graph.\n */\n parent: Node | null;\n\n /**\n * Label to be used when displaying the node.\n * For default labels, use a translated string.\n *\n * @example 'My Node'\n * @example ['unknown node label, { ns: 'example-plugin' }]\n */\n label: Label;\n\n /**\n * Description to be used when displaying a detailed view of the node.\n * For default descriptions, use a translated string.\n */\n description?: Label;\n\n /**\n * Icon to be used when displaying the node.\n */\n icon?: FC<IconProps>;\n\n /**\n * Properties of the node relevant to displaying the node.\n */\n // TODO(burdon): Make this extensible and move label, description, and icon into here?\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 // TODO(burdon): In some places `null` is cast to TData so make optional?\n data: TData;\n\n /**\n * Children of the node stored by their id.\n */\n // TODO(burdon): Rename nodes/nodeMap?\n childrenMap: Record<string, Node>;\n\n /**\n * Actions of the node stored by their id.\n */\n actionsMap: Record<string, Action>;\n\n /**\n * Children of the node in default order.\n */\n get children(): Node[];\n\n /**\n * Actions of the node in default order.\n */\n get actions(): Action[];\n\n addProperty(key: string, value: any): void;\n removeProperty(key: string): void;\n\n addNode<TChildData = null, TChildProperties extends Record<string, any> = Record<string, any>>(\n id: string,\n ...node: NodeArg<TChildData, TChildProperties>[]\n ): Node<TChildData, TChildProperties>[];\n removeNode(id: string): Node;\n\n addAction<TActionProperties extends Record<string, any> = Record<string, any>>(\n ...action: ActionArg<TActionProperties>[]\n ): Action<TActionProperties>[];\n removeAction(id: string): Action;\n};\n\nexport type NodeArg<TData = null, TProperties extends Record<string, any> = Record<string, any>> = Pick<\n Node,\n 'id' | 'label'\n> &\n Partial<Omit<Node<TData, TProperties>, 'id' | 'label' | 'actions'>> & { actions?: ActionArg[] };\n\nexport const isGraphNode = (data: unknown): data is Node =>\n data && typeof data === 'object' ? 'id' in data && 'label' in data : false;\n"],
|
|
5
|
-
"mappings": ";AAIA,SAASA,
|
|
6
|
-
"names": ["
|
|
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';\nimport { type DeepSignal, deepSignal } from 'deepsignal/react';\n\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 = deepSignal<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: DeepSignal<Record<string, string[]>> = deepSignal({});\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;AAC1B,SAA0BC,kBAAkB;AAE5C,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,WAAqC;MACrD,CAACH,OAAAA,GAAU;QAAEI,IAAIJ;QAASK,YAAY,CAAC;QAAGC,MAAM;MAAK;IACvD,CAAA;AAOSC;;;;;kBAA+CJ,WAAW,CAAC,CAAA;AA4C5DK,0BAAiB,CAACC,aAAAA;AACxB,YAAMC,OAAa;QACjB,GAAGD;QACHE,OAAO,CAAC,EAAEC,YAAY,WAAU,IAAK,CAAC,MAAC;AACrC,iBAAO,KAAKL,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xCO,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", "deepSignal", "invariant", "nonNullable", "isGraphNode", "data", "properties", "isAction", "actionGroupSymbol", "Symbol", "isActionGroup", "isActionLike", "ROOT_ID", "Graph", "_nodes", "deepSignal", "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", "invariant", "undefined", "nonNullable", "addNodes", "_addNode", "_node", "untracked", "forEach", "subNode", "addEdge", "source", "target", "removeNode", "removeEdge", "outbound", "includes", "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/
|
|
1
|
+
{"inputs":{"packages/sdk/app-graph/src/node.ts":{"bytes":6411,"imports":[],"format":"esm"},"packages/sdk/app-graph/src/graph.ts":{"bytes":30406,"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"deepsignal/react","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":4608,"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":2039,"imports":[],"format":"esm"},"packages/sdk/app-graph/src/index.ts":{"bytes":723,"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":22576},"packages/sdk/app-graph/dist/lib/browser/index.mjs":{"imports":[{"path":"@preact/signals-core","kind":"import-statement","external":true},{"path":"deepsignal/react","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":7685},"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":9737}}}
|