Line data Source code
1 : import type { PreviewExposure } from '@/core/schemas/previewExposureSchema'
2 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
3 : import type { UUID } from '@/utils/uuid'
4 :
5 : interface ResolvedPreviewChainStep {
6 : rootGraphId: UUID
7 : hostNodeLocator: string
8 : exposure: PreviewExposure
9 : }
10 :
11 : export interface ResolvedPreviewChain {
12 : steps: readonly ResolvedPreviewChainStep[]
13 : leaf: {
14 : rootGraphId: UUID
15 : sourceNodeId: NodeId
16 : sourcePreviewName: string
17 : }
18 : }
19 :
20 : export interface PreviewExposureChainContext {
21 : getExposures(
22 : rootGraphId: UUID,
23 : hostNodeLocator: string
24 : ): readonly PreviewExposure[]
25 : resolveNestedHost(
26 : rootGraphId: UUID,
27 : hostNodeLocator: string,
28 : sourceNodeId: NodeId
29 : ): { rootGraphId: UUID; hostNodeLocator: string } | undefined
30 : }
31 :
32 2356 : const MAX_CHAIN_DEPTH = 32
33 :
34 370 : function exposureSourceNodeId(exposure: PreviewExposure): NodeId {
35 370 : return exposure.sourceNodeId
36 : }
37 :
38 185 : export function resolvePreviewExposureChain(
39 : rootGraphId: UUID,
40 : hostNodeLocator: string,
41 : name: string,
42 : ctx: PreviewExposureChainContext
43 : ): ResolvedPreviewChain | undefined {
44 185 : const steps: ResolvedPreviewChainStep[] = []
45 185 : const visited = new Set<string>()
46 :
47 185 : let currentRootGraphId: UUID = rootGraphId
48 185 : let currentHost = hostNodeLocator
49 185 : let currentName = name
50 :
51 185 : const chainFromLastStep = (): ResolvedPreviewChain | undefined => {
52 0 : if (steps.length === 0) return undefined
53 0 : const lastStep = steps[steps.length - 1]
54 0 : return {
55 : steps,
56 : leaf: {
57 : rootGraphId: lastStep.rootGraphId,
58 : sourceNodeId: exposureSourceNodeId(lastStep.exposure),
59 : sourcePreviewName: lastStep.exposure.sourcePreviewName
60 : }
61 : }
62 : }
63 :
64 185 : for (let depth = 0; depth < MAX_CHAIN_DEPTH; depth++) {
65 185 : const key = `${currentRootGraphId}|${currentHost}|${currentName}`
66 185 : if (visited.has(key)) {
67 0 : console.warn(
68 : `[previewExposureChain] cycle detected at ${key}; terminating walk`
69 : )
70 0 : return chainFromLastStep()
71 : }
72 185 : visited.add(key)
73 :
74 185 : const exposure = ctx
75 : .getExposures(currentRootGraphId, currentHost)
76 : .find((e) => e.name === currentName)
77 185 : if (!exposure) return chainFromLastStep()
78 :
79 185 : steps.push({
80 : rootGraphId: currentRootGraphId,
81 : hostNodeLocator: currentHost,
82 : exposure
83 : })
84 :
85 185 : const nested = ctx.resolveNestedHost(
86 : currentRootGraphId,
87 : currentHost,
88 : exposureSourceNodeId(exposure)
89 : )
90 185 : if (!nested) {
91 185 : return {
92 : steps,
93 : leaf: {
94 : rootGraphId: currentRootGraphId,
95 : sourceNodeId: exposureSourceNodeId(exposure),
96 : sourcePreviewName: exposure.sourcePreviewName
97 : }
98 : }
99 : }
100 :
101 0 : currentRootGraphId = nested.rootGraphId
102 0 : currentHost = nested.hostNodeLocator
103 0 : currentName = exposure.sourcePreviewName
104 : }
105 :
106 0 : console.warn(
107 : `[previewExposureChain] max chain depth (${MAX_CHAIN_DEPTH}) reached; terminating walk`
108 : )
109 0 : return chainFromLastStep()
110 : }
|