Line data Source code
1 : import type { LGraphNode } from '@comfyorg/graph-model/entityRegistry'
2 : import type { INodeInputSlot } from '@comfyorg/graph-model/slots'
3 :
4 : type SubgraphInputLinkContext = {
5 : inputNode: LGraphNode
6 : targetInput: INodeInputSlot
7 : getTargetWidget: () => ReturnType<LGraphNode['getWidgetFromSlot']>
8 : }
9 :
10 2520 : export function resolveSubgraphInputLink<TResult>(
11 : node: LGraphNode,
12 : inputName: string,
13 : resolve: (context: SubgraphInputLinkContext) => TResult | undefined
14 : ): TResult | undefined {
15 2520 : if (!node.isSubgraphNode()) return undefined
16 :
17 2520 : const inputSlot = node.subgraph.inputNode.slots.find(
18 : (slot) => slot.name === inputName
19 : )
20 2520 : if (!inputSlot) return undefined
21 :
22 : // Iterate forward so the first connected source is the promoted representative,
23 : // matching SubgraphNode._resolveLinkedPromotionBySubgraphInput.
24 2517 : for (const linkId of inputSlot.linkIds) {
25 2520 : const link = node.subgraph.getLink(linkId)
26 2520 : if (!link) continue
27 :
28 2520 : const { inputNode, input: targetInput } = link.resolve(node.subgraph)
29 2520 : if (!inputNode || !targetInput) continue
30 :
31 2520 : let cachedTargetWidget:
32 : | ReturnType<LGraphNode['getWidgetFromSlot']>
33 : | undefined
34 2520 : let hasCachedTargetWidget = false
35 :
36 2520 : const resolved = resolve({
37 : inputNode,
38 : targetInput,
39 : getTargetWidget: () => {
40 2193 : if (!hasCachedTargetWidget) {
41 2193 : cachedTargetWidget = inputNode.getWidgetFromSlot(targetInput)
42 2193 : hasCachedTargetWidget = true
43 : }
44 2193 : return cachedTargetWidget
45 : }
46 : })
47 2520 : if (resolved !== undefined) return resolved
48 : }
49 :
50 : return undefined
51 : }
|