Line data Source code
1 : import { promotedInputSource } from '@/core/graph/subgraph/promotedInputWidget'
2 : import { resolveConcretePromotedWidget } from '@/core/graph/subgraph/resolveConcretePromotedWidget'
3 : import {
4 : isValueControlWidget,
5 : nextValueForLinkedTarget
6 : } from '@/core/graph/widgets/valueControl'
7 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
8 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
9 : import { useSettingStore } from '@/platform/settings/settingStore'
10 :
11 : /**
12 : * A promoted subgraph host widget whose interior source widget is driven by a
13 : * `control_after_generate` widget. The host owns the value (store-backed), while
14 : * the interior linked widgets supply the control mode and filter (ADR-SUBGRAPH-PROMOTION-0009).
15 : */
16 : interface PromotedControlTarget {
17 : hostWidget: IBaseWidget
18 : linkedWidgets: IBaseWidget[]
19 : }
20 :
21 599 : function collectPromotedControlTargets(
22 : node: LGraphNode
23 : ): PromotedControlTarget[] {
24 599 : if (!node.isSubgraphNode()) return []
25 :
26 29 : const targets: PromotedControlTarget[] = []
27 274 : for (const [index, input] of node.inputs.entries()) {
28 : // Promoted inputs are store-backed and addressed by widgetId.
29 59 : if (!input.widgetId) continue
30 :
31 : // An incoming link means the value is fed externally (or by an outer
32 : // subgraph boundary in the nested case), so the host store is not the
33 : // authoritative source and control must not run here.
34 55 : if (node.isInputConnected(index)) continue
35 :
36 30 : const source = promotedInputSource(node, input)
37 52 : if (!source) continue
38 :
39 30 : const resolution = resolveConcretePromotedWidget(
40 : node,
41 : source.nodeId,
42 : source.widgetName
43 : )
44 52 : if (resolution.status !== 'resolved') continue
45 :
46 30 : const { linkedWidgets } = resolution.resolved.widget
47 59 : if (!linkedWidgets?.some(isValueControlWidget)) continue
48 :
49 4 : const hostWidget = node.getWidgetFromSlot(input)
50 20 : if (!hostWidget) continue
51 :
52 4 : targets.push({ hostWidget, linkedWidgets })
53 : }
54 29 : return targets
55 : }
56 :
57 4 : function applyTarget(target: PromotedControlTarget, nodeId: unknown): void {
58 4 : const next = nextValueForLinkedTarget({
59 : target: target.hostWidget,
60 : linkedWidgets: target.linkedWidgets,
61 : nodeId
62 : })
63 4 : if (next === undefined) return
64 :
65 4 : target.hostWidget.value = next
66 4 : target.hostWidget.callback?.(next)
67 : }
68 :
69 1360 : function controlValueRunBefore(): boolean {
70 1360 : return useSettingStore().get('Comfy.WidgetControlMode') === 'before'
71 : }
72 :
73 : /**
74 : * Host widgets that have run control at least once, so `before` mode can skip
75 : * the first execution (mirroring the interior control widget's HAS_EXECUTED).
76 : *
77 : * Keyed by the widget instance so entries are released when the graph is
78 : * cleared and its widgets are recreated, avoiding stale state across reloads.
79 : */
80 2356 : const executedWidgets = new WeakSet<IBaseWidget>()
81 :
82 : /**
83 : * Applies `control_after_generate` to a subgraph host node's promoted widgets.
84 : *
85 : * The interior control widget short-circuits for promoted inputs because the
86 : * interior widget is link-fed by the subgraph boundary, so its value is dead.
87 : * The authoritative value lives on the host node (store-backed), and this
88 : * applies the next controlled value there.
89 : */
90 1360 : export function applyPromotedWidgetControl(
91 : node: LGraphNode,
92 : phase: 'beforeQueued' | 'afterQueued'
93 : ): void {
94 1360 : const runBefore = controlValueRunBefore()
95 1360 : if (phase === 'beforeQueued' && !runBefore) return
96 1360 : if (phase === 'afterQueued' && runBefore) return
97 :
98 1200 : for (const target of collectPromotedControlTargets(node)) {
99 4 : if (phase === 'afterQueued') {
100 4 : applyTarget(target, node.id)
101 4 : continue
102 : }
103 :
104 0 : if (executedWidgets.has(target.hostWidget)) {
105 0 : applyTarget(target, node.id)
106 : }
107 0 : executedWidgets.add(target.hostWidget)
108 : }
109 : }
|