LCOV - code coverage report
Current view: top level - src/core/graph/nodeShell - nodeShellState.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 95.7 % 46 44
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { LGraph, LGraphNode } from '@comfyorg/graph-model/entityRegistry'
       2              : import { shallowReactive } from 'vue'
       3              : 
       4              : import { assert } from '@/base/assert'
       5              : import {
       6              :   canTransferLayoutAttachment,
       7              :   transferLayoutAttachment
       8              : } from '@/renderer/core/layout/operations/graphLayoutAttachment'
       9              : import { useNodeDataStore } from '@/stores/nodeDataStore'
      10              : import { graphScopeOf } from '@comfyorg/graph-model/graphScopeId'
      11              : import { UNASSIGNED_NODE_ID } from '@comfyorg/graph-model/nodeId'
      12              : import { LGraphEventMode } from '@comfyorg/graph-model/globalEnums'
      13              : import { zeroUuid } from '@comfyorg/graph-model/uuid'
      14              : 
      15              : import type {
      16              :   INodeInputSlot,
      17              :   INodeOutputSlot
      18              : } from '@comfyorg/graph-model/slots'
      19              : 
      20              : import type { TitleMode } from '@comfyorg/graph-model/globalEnums'
      21              : import type { NodeState } from '@comfyorg/graph-model/nodeState'
      22              : 
      23              : /**
      24              :  * Wraps a node's `inputs` array with a rehydration view — e.g.
      25              :  * {@link createInputSlotView} — that upgrades plain input-slot writes into
      26              :  * class instances. Injected by the caller (rather than imported directly)
      27              :  * to keep this module out of the litegraph value-import graph: `LGraphNode`
      28              :  * sits in a cycle with the slot/subgraph modules that back that view, and a
      29              :  * static import here resolves at a point in that cycle where `LGraphNode`
      30              :  * isn't defined yet.
      31              :  */
      32              : export type InputSlotViewFactory = (
      33              :   node: LGraphNode,
      34              :   inputs: INodeInputSlot[]
      35              : ) => INodeInputSlot[]
      36              : 
      37              : /**
      38              :  * Builds the shell state a node carries from construction until it adopts the
      39              :  * {@link useNodeDataStore} proxy in {@link registerNodeState}.
      40              :  *
      41              :  * `inputs` is wrapped by `createInputSlotView` here — the one place that
      42              :  * builds a node's slot arrays — so every `NodeState` producer, not just
      43              :  * {@link LGraphNode}'s constructor, gets plain input-slot writes rehydrated
      44              :  * into `NodeInputSlot` instances. Callers pass their own view factory (see
      45              :  * {@link InputSlotViewFactory}) to avoid a static import cycle.
      46              :  */
      47        23337 : export function createNodeShellState(
      48              :   node: LGraphNode,
      49              :   createInputSlotView: InputSlotViewFactory,
      50              :   title: string,
      51              :   type: string | undefined,
      52              :   titleMode: TitleMode | undefined
      53              : ): NodeState {
      54        23337 :   return {
      55              :     flags: {},
      56              :     graphId: zeroUuid,
      57              :     id: UNASSIGNED_NODE_ID,
      58              :     inputs: createInputSlotView(node, shallowReactive<INodeInputSlot[]>([])),
      59              :     mode: LGraphEventMode.ALWAYS,
      60              :     outputs: shallowReactive<INodeOutputSlot[]>([]),
      61              :     properties: {},
      62              :     title: title || 'Unnamed',
      63              :     type: type ?? '',
      64              :     titleMode
      65              :   }
      66              : }
      67              : 
      68              : /**
      69              :  * Writes a shell-state field, emitting `node:property:changed` on change.
      70              :  * Returns whether the value changed.
      71              :  */
      72        70961 : export function setTrackedNodeState<K extends keyof NodeState>(
      73              :   node: LGraphNode,
      74              :   property: K,
      75              :   value: NodeState[K]
      76              : ): boolean {
      77        70961 :   const oldValue = node._state[property]
      78        70961 :   if (oldValue === value) return false
      79              : 
      80          704 :   node._state[property] = value
      81        70961 :   node.graph?.trigger('node:property:changed', {
      82              :     nodeId: node.id,
      83              :     property,
      84              :     oldValue,
      85              :     newValue: value
      86              :   })
      87        70961 :   return true
      88              : }
      89              : 
      90              : /**
      91              :  * Registers a node's shell state into {@link useNodeDataStore} and adopts the
      92              :  * store's proxy as {@link LGraphNode._state}. Call wherever a node joins a
      93              :  * graph. Returns `false` on an id collision within the owning root graph —
      94              :  * the caller must mint a new id and retry.
      95              :  */
      96        23237 : export function registerNodeState(
      97              :   graph: Pick<LGraph, 'rootGraph' | 'id'>,
      98              :   node: LGraphNode
      99              : ): boolean {
     100        23237 :   const graphScope = graphScopeOf(graph)
     101        23237 :   const store = useNodeDataStore()
     102        23237 :   const strandedScope =
     103              :     node._graphScope === undefined ||
     104              :     node._graphScope.rootGraphId === graphScope.rootGraphId
     105              :       ? undefined
     106              :       : node._graphScope
     107              : 
     108        23237 :   if (strandedScope !== undefined) {
     109            0 :     store.deleteNode(strandedScope, node._state)
     110            0 :     node._graphScope = undefined
     111              :   }
     112        23237 :   assert(
     113              :     strandedScope === undefined,
     114              :     'registerNodeState: node already registered under a different root graph',
     115              :     {
     116              :       nodeId: node.id,
     117              :       previousRootGraphId: strandedScope?.rootGraphId,
     118              :       nextRootGraphId: graphScope.rootGraphId
     119              :     }
     120              :   )
     121              : 
     122        23237 :   node._state.graphId = graph.id
     123        23237 :   const registered = store.registerNode(graphScope, node._state)
     124        23237 :   if (!registered) return false
     125        23236 :   node._state = registered
     126        23236 :   node._graphScope = graphScope
     127        23236 :   return true
     128              : }
     129              : 
     130              : /**
     131              :  * Removes a node's shell state from {@link useNodeDataStore} and detaches the
     132              :  * node. No-op for nodes that were never registered.
     133              :  * @param node The node to unregister
     134              :  */
     135        10979 : export function unregisterNodeState(node: LGraphNode): void {
     136        10979 :   if (!node._graphScope) return
     137        10979 :   const graphScope = node._graphScope
     138        10979 :   const deleted = useNodeDataStore().deleteNode(graphScope, node._state)
     139        10979 :   node._graphScope = undefined
     140        10979 :   assert(
     141              :     deleted,
     142              :     'unregisterNodeState: node state not found in bucket (identity drift)',
     143              :     { nodeId: node.id, rootGraphId: graphScope.rootGraphId }
     144              :   )
     145              : }
     146              : 
     147           57 : function canTransferNodeState(
     148              :   node: LGraphNode,
     149              :   replacement: LGraphNode
     150              : ): boolean {
     151           57 :   return (
     152              :     node.id === replacement.id &&
     153              :     replacement._graphScope === undefined &&
     154              :     node._graphScope !== undefined &&
     155              :     useNodeDataStore().ownsNode(node._graphScope, node._state)
     156              :   )
     157              : }
     158              : 
     159           19 : function transferNodeState(node: LGraphNode, replacement: LGraphNode): void {
     160           19 :   const registeredState = node._state
     161           19 :   const detachedState = { ...registeredState }
     162           19 :   const { graphId: _graphId, id: _id, ...replacementState } = replacement._state
     163           19 :   Object.assign(registeredState, {
     164              :     bgcolor: undefined,
     165              :     boxcolor: undefined,
     166              :     color: undefined,
     167              :     lastSerialization: undefined,
     168              :     resizable: undefined,
     169              :     shape: undefined,
     170              :     showAdvanced: undefined,
     171              :     titleMode: undefined,
     172              :     ...replacementState
     173              :   } satisfies {
     174              :     [K in Exclude<keyof NodeState, 'graphId' | 'id'>]-?:
     175              :       | NodeState[K]
     176              :       | undefined
     177              :   })
     178           19 :   replacement._state = registeredState
     179           19 :   replacement._graphScope = node._graphScope
     180           19 :   node._state = detachedState
     181           19 :   node._graphScope = undefined
     182              : }
     183              : 
     184              : /**
     185              :  * Whether `replacement` may adopt `node`'s registered shell state and layout
     186              :  * attachment during an in-place node-type replacement.
     187              :  */
     188           57 : export function canTransferReplacementOwnership(
     189              :   node: LGraphNode,
     190              :   replacement: LGraphNode
     191              : ): boolean {
     192           57 :   return (
     193              :     canTransferNodeState(node, replacement) &&
     194              :     canTransferLayoutAttachment(node, replacement)
     195              :   )
     196              : }
     197              : 
     198              : /**
     199              :  * Transfers `node`'s registered shell state and layout attachment to
     200              :  * `replacement`, detaching `node`. Returns `false` (no-op) when the transfer
     201              :  * preconditions no longer hold.
     202              :  */
     203           19 : export function transferReplacementOwnership(
     204              :   node: LGraphNode,
     205              :   replacement: LGraphNode
     206              : ): boolean {
     207           19 :   if (!canTransferReplacementOwnership(node, replacement)) return false
     208           19 :   if (!transferLayoutAttachment(node, replacement)) return false
     209           19 :   transferNodeState(node, replacement)
     210           19 :   return true
     211              : }
        

Generated by: LCOV version 2.0-1