LCOV - code coverage report
Current view: top level - src/core/graph/subgraph - createPromotionErrorReconciler.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 100.0 % 47 47
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : /**
       2              :  * Moves the missing-media error surface between the interior widget and its
       3              :  * host as promotion transfers value ownership.
       4              :  */
       5              : import type {
       6              :   LGraphNode,
       7              :   Subgraph,
       8              :   SubgraphEventMap,
       9              :   SubgraphNode
      10              : } from '@/lib/litegraph/src/litegraph'
      11              : 
      12              : interface PromotionErrorReconcilerHandlers {
      13              :   /** Drops candidates whose widget is no longer the editable value owner. */
      14              :   dropOutOfScope: () => void
      15              :   /** Rescans a host and its interior nodes for missing media. */
      16              :   rescanHost: (subgraphNode: SubgraphNode) => void
      17              :   /** Removes the host-keyed candidate for a widget that is being demoted. */
      18              :   removeHostWidgetCandidate: (
      19              :     subgraphNode: SubgraphNode,
      20              :     widgetName: string
      21              :   ) => void
      22              : }
      23              : 
      24              : export interface PromotionErrorReconciler {
      25              :   /** Subscribes a subgraph host and every definition nested inside it. */
      26              :   attachNode: (node: LGraphNode) => void
      27              :   /** Releases a subgraph host's subscriptions, and its nested ones. */
      28              :   detachNode: (node: LGraphNode) => void
      29              :   /** Subscribes a subgraph definition directly, for hooks installed on one. */
      30              :   attach: (subgraph: Subgraph) => void
      31              :   /** Releases everything still subscribed. */
      32              :   dispose: () => void
      33              : }
      34              : 
      35              : interface Subscription {
      36              :   unsubscribe: () => void
      37              :   hostCount: number
      38              : }
      39              : 
      40         4924 : export function createPromotionErrorReconciler({
      41              :   dropOutOfScope,
      42              :   rescanHost,
      43              :   removeHostWidgetCandidate
      44              : }: PromotionErrorReconcilerHandlers): PromotionErrorReconciler {
      45         4924 :   const subscriptions = new Map<Subgraph, Subscription>()
      46              :   /** Hosts already counted, since onNodeAdded is replayed for existing nodes. */
      47         4924 :   const attachedHosts = new WeakSet<LGraphNode>()
      48              : 
      49              :   /** Re-derives both sides once ownership has settled. */
      50         4924 :   const reconcile = (subgraphNode: SubgraphNode) => {
      51          478 :     queueMicrotask(() => {
      52          478 :       dropOutOfScope()
      53          478 :       rescanHost(subgraphNode)
      54              :     })
      55              :   }
      56              : 
      57         4924 :   const subscribe = (subgraph: Subgraph): (() => void) => {
      58          680 :     const onDemoted = (
      59              :       event: CustomEvent<SubgraphEventMap['widget-demoted']>
      60              :     ) => {
      61           29 :       const { subgraphNode, widget } = event.detail
      62              :       // Fires before the widget leaves the host input, so the scope filter
      63              :       // cannot see it gone yet.
      64           29 :       removeHostWidgetCandidate(subgraphNode, widget.name)
      65           29 :       reconcile(subgraphNode)
      66              :     }
      67          680 :     const onPromoted = (
      68              :       event: CustomEvent<SubgraphEventMap['widget-promoted']>
      69              :     ) => {
      70          449 :       reconcile(event.detail.subgraphNode)
      71              :     }
      72              : 
      73          680 :     subgraph.events.addEventListener('widget-demoted', onDemoted)
      74          680 :     subgraph.events.addEventListener('widget-promoted', onPromoted)
      75          680 :     return () => {
      76          352 :       subgraph.events.removeEventListener('widget-demoted', onDemoted)
      77          352 :       subgraph.events.removeEventListener('widget-promoted', onPromoted)
      78              :     }
      79              :   }
      80              : 
      81         4924 :   const attach = (subgraph: Subgraph): void => {
      82          690 :     const existing = subscriptions.get(subgraph)
      83          690 :     if (existing) {
      84           10 :       existing.hostCount += 1
      85           10 :       return
      86              :     }
      87              : 
      88          680 :     subscriptions.set(subgraph, {
      89              :       unsubscribe: subscribe(subgraph),
      90              :       hostCount: 1
      91              :     })
      92         1398 :     for (const node of subgraph.nodes) attachNode(node)
      93              :   }
      94              : 
      95         4924 :   const detach = (subgraph: Subgraph): void => {
      96           96 :     const subscription = subscriptions.get(subgraph)
      97           96 :     if (!subscription) return
      98              : 
      99           96 :     subscription.hostCount -= 1
     100           96 :     if (subscription.hostCount > 0) return
     101              : 
     102           94 :     subscription.unsubscribe()
     103           94 :     subscriptions.delete(subgraph)
     104          185 :     for (const node of subgraph.nodes) detachNode(node)
     105              :   }
     106              : 
     107         4924 :   const attachNode = (node: LGraphNode): void => {
     108        23511 :     if (!node.isSubgraphNode()) return
     109         8143 :     if (attachedHosts.has(node)) return
     110          549 :     attachedHosts.add(node)
     111          549 :     attach(node.subgraph)
     112              :   }
     113              : 
     114         4924 :   const detachNode = (node: LGraphNode): void => {
     115        10272 :     if (!node.isSubgraphNode()) return
     116          836 :     if (!attachedHosts.delete(node)) return
     117           96 :     detach(node.subgraph)
     118              :   }
     119              : 
     120         4924 :   const dispose = (): void => {
     121         2569 :     for (const { unsubscribe } of subscriptions.values()) unsubscribe()
     122         2569 :     subscriptions.clear()
     123              :   }
     124              : 
     125         4924 :   return { attachNode, detachNode, attach, dispose }
     126              : }
        

Generated by: LCOV version 2.0-1