|
| 1 | +import { shallowReactive } from 'vue' |
| 2 | + |
| 3 | +import { |
| 4 | + canTransferLayoutAttachment, |
| 5 | + transferLayoutAttachment |
| 6 | +} from '@/renderer/core/layout/operations/graphLayoutAttachment' |
| 7 | +import { useNodeDataStore } from '@/stores/nodeDataStore' |
| 8 | +import { graphScopeOf } from '@/types/graphScopeId' |
| 9 | +import { UNASSIGNED_NODE_ID } from '@/types/nodeId' |
| 10 | +import { LGraphEventMode } from '@/lib/litegraph/src/types/globalEnums' |
| 11 | +import { zeroUuid } from '@/utils/uuid' |
| 12 | + |
| 13 | +import type { |
| 14 | + INodeInputSlot, |
| 15 | + INodeOutputSlot |
| 16 | +} from '@/lib/litegraph/src/interfaces' |
| 17 | +import type { LGraph } from '@/lib/litegraph/src/LGraph' |
| 18 | +import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' |
| 19 | +import type { TitleMode } from '@/lib/litegraph/src/types/globalEnums' |
| 20 | +import type { NodeState } from '@/types/nodeState' |
| 21 | + |
| 22 | +/** |
| 23 | + * Builds the shell state a node carries from construction until it adopts the |
| 24 | + * {@link useNodeDataStore} proxy in {@link registerNodeState}. |
| 25 | + */ |
| 26 | +export function createNodeShellState( |
| 27 | + title: string, |
| 28 | + type: string | undefined, |
| 29 | + titleMode: TitleMode | undefined |
| 30 | +): NodeState { |
| 31 | + return { |
| 32 | + flags: {}, |
| 33 | + graphId: zeroUuid, |
| 34 | + id: UNASSIGNED_NODE_ID, |
| 35 | + inputs: shallowReactive<INodeInputSlot[]>([]), |
| 36 | + mode: LGraphEventMode.ALWAYS, |
| 37 | + outputs: shallowReactive<INodeOutputSlot[]>([]), |
| 38 | + title: title || 'Unnamed', |
| 39 | + type: type ?? '', |
| 40 | + titleMode |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** Writes a shell-state field, emitting `node:property:changed` on change. */ |
| 45 | +export function setTrackedNodeState<K extends keyof NodeState>( |
| 46 | + node: LGraphNode, |
| 47 | + property: K, |
| 48 | + value: NodeState[K] |
| 49 | +): void { |
| 50 | + const oldValue = node._state[property] |
| 51 | + if (oldValue === value) return |
| 52 | + |
| 53 | + node._state[property] = value |
| 54 | + node.graph?.trigger('node:property:changed', { |
| 55 | + nodeId: node.id, |
| 56 | + property, |
| 57 | + oldValue, |
| 58 | + newValue: value |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Registers a node's shell state into {@link useNodeDataStore} and adopts the |
| 64 | + * store's proxy as {@link LGraphNode._state}. Call wherever a node joins a |
| 65 | + * graph. Returns `false` on an id collision within the owning root graph — |
| 66 | + * the caller must mint a new id and retry. |
| 67 | + */ |
| 68 | +export function registerNodeState( |
| 69 | + graph: Pick<LGraph, 'rootGraph' | 'id'>, |
| 70 | + node: LGraphNode |
| 71 | +): boolean { |
| 72 | + const graphScope = graphScopeOf(graph) |
| 73 | + node._state.graphId = graph.id |
| 74 | + const registered = useNodeDataStore().registerNode(graphScope, node._state) |
| 75 | + if (!registered) return false |
| 76 | + node._state = registered |
| 77 | + node._graphScope = graphScope |
| 78 | + return true |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Removes a node's shell state from {@link useNodeDataStore} and detaches the |
| 83 | + * node. No-op for nodes that were never registered. |
| 84 | + * @param node The node to unregister |
| 85 | + */ |
| 86 | +export function unregisterNodeState(node: LGraphNode): void { |
| 87 | + if (!node._graphScope) return |
| 88 | + useNodeDataStore().deleteNode(node._graphScope, node._state) |
| 89 | + node._graphScope = undefined |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Unregisters every node a graph owns, including those inside the subgraph |
| 94 | + * definitions it holds. Used when a graph's nodes leave the store without a |
| 95 | + * whole-bucket wipe: subgraph-definition removal, and clearing a graph that |
| 96 | + * shares its bucket with other graphs. |
| 97 | + * @param graph The graph whose nodes should be unregistered |
| 98 | + */ |
| 99 | +export function unregisterAllNodeStates( |
| 100 | + graph: Pick<LGraph, '_nodes' | '_subgraphs'> |
| 101 | +): void { |
| 102 | + for (const node of graph._nodes) unregisterNodeState(node) |
| 103 | + for (const subgraph of graph._subgraphs.values()) { |
| 104 | + unregisterAllNodeStates(subgraph) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +function canTransferNodeState( |
| 109 | + node: LGraphNode, |
| 110 | + replacement: LGraphNode |
| 111 | +): boolean { |
| 112 | + return ( |
| 113 | + node.id === replacement.id && |
| 114 | + replacement._graphScope === undefined && |
| 115 | + node._graphScope !== undefined && |
| 116 | + useNodeDataStore().ownsNode(node._graphScope, node._state) |
| 117 | + ) |
| 118 | +} |
| 119 | + |
| 120 | +function transferNodeState(node: LGraphNode, replacement: LGraphNode): void { |
| 121 | + const registeredState = node._state |
| 122 | + const detachedState = { ...registeredState } |
| 123 | + const { graphId: _graphId, id: _id, ...replacementState } = replacement._state |
| 124 | + Object.assign(registeredState, { |
| 125 | + bgcolor: undefined, |
| 126 | + color: undefined, |
| 127 | + resizable: undefined, |
| 128 | + shape: undefined, |
| 129 | + showAdvanced: undefined, |
| 130 | + titleMode: undefined, |
| 131 | + ...replacementState |
| 132 | + } satisfies { |
| 133 | + [K in Exclude<keyof NodeState, 'graphId' | 'id'>]-?: |
| 134 | + | NodeState[K] |
| 135 | + | undefined |
| 136 | + }) |
| 137 | + replacement._state = registeredState |
| 138 | + replacement._graphScope = node._graphScope |
| 139 | + node._state = detachedState |
| 140 | + node._graphScope = undefined |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * Whether `replacement` may adopt `node`'s registered shell state and layout |
| 145 | + * attachment during an in-place node-type replacement. |
| 146 | + */ |
| 147 | +export function canTransferReplacementOwnership( |
| 148 | + node: LGraphNode, |
| 149 | + replacement: LGraphNode |
| 150 | +): boolean { |
| 151 | + return ( |
| 152 | + canTransferNodeState(node, replacement) && |
| 153 | + canTransferLayoutAttachment(node, replacement) |
| 154 | + ) |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * Transfers `node`'s registered shell state and layout attachment to |
| 159 | + * `replacement`, detaching `node`. Returns `false` (no-op) when the transfer |
| 160 | + * preconditions no longer hold. |
| 161 | + */ |
| 162 | +export function transferReplacementOwnership( |
| 163 | + node: LGraphNode, |
| 164 | + replacement: LGraphNode |
| 165 | +): boolean { |
| 166 | + if (!canTransferReplacementOwnership(node, replacement)) return false |
| 167 | + if (!transferLayoutAttachment(node, replacement)) return false |
| 168 | + transferNodeState(node, replacement) |
| 169 | + return true |
| 170 | +} |
0 commit comments