Line data Source code
1 : import type { SubgraphNode } from '@comfyorg/graph-model/entityRegistry'
2 : import { defineStore } from 'pinia'
3 : import { ref } from 'vue'
4 :
5 : import type {
6 : PreviewExposureChainContext,
7 : ResolvedPreviewChain
8 : } from '@/core/graph/subgraph/preview/previewExposureChain'
9 : import { resolvePreviewExposureChain } from '@/core/graph/subgraph/preview/previewExposureChain'
10 : import type { PromotedWidgetSource } from '@/core/graph/subgraph/promotedWidgetTypes'
11 : import type { PreviewExposure } from '@/core/schemas/previewExposureSchema'
12 :
13 : import { nextUniqueName } from '@/lib/litegraph/src/strings'
14 : import { createNodeLocatorId } from '@comfyorg/graph-model/nodeIdentification'
15 : import type { NodeLocatorId } from '@comfyorg/graph-model/nodeIdentification'
16 : import { toNodeId } from '@comfyorg/graph-model/nodeId'
17 : import type { SerializedNodeId } from '@comfyorg/graph-model/nodeId'
18 : import type { UUID } from '@/utils/uuid'
19 :
20 2356 : const EMPTY_EXPOSURES: readonly PreviewExposure[] = Object.freeze([])
21 :
22 : type ResolveNestedHostFn = NonNullable<
23 : PreviewExposureChainContext['resolveNestedHost']
24 : >
25 :
26 : type PreviewExposureInput = Omit<PreviewExposure, 'sourceNodeId'> & {
27 : sourceNodeId: SerializedNodeId
28 : }
29 :
30 50 : function normalizePreviewExposure(
31 : exposure: PreviewExposureInput
32 : ): PreviewExposure {
33 50 : return {
34 : ...exposure,
35 : sourceNodeId: toNodeId(exposure.sourceNodeId)
36 : }
37 : }
38 :
39 1724 : export function getPreviewExposureHostLocator(
40 : host: SubgraphNode
41 : ): NodeLocatorId | null {
42 1724 : const locator = tryGetPreviewExposureHostLocator(host)
43 1724 : if (locator) return locator
44 0 : console.error(
45 : `Cannot create preview exposure host locator for node ${String(host.id)}`
46 : )
47 0 : return null
48 : }
49 :
50 4502 : export function tryGetPreviewExposureHostLocator(
51 : host: SubgraphNode
52 : ): NodeLocatorId | null {
53 4502 : const graphId = host.graph?.isRootGraph ? null : (host.graph?.id ?? null)
54 4502 : return createNodeLocatorId(graphId, host.id)
55 : }
56 :
57 2356 : export const usePreviewExposureStore = defineStore('previewExposure', () => {
58 2355 : const exposures = ref(new Map<UUID, Map<string, PreviewExposure[]>>())
59 :
60 298 : function _getHostsForGraph(
61 : rootGraphId: UUID
62 : ): Map<string, PreviewExposure[]> {
63 298 : const hosts = exposures.value.get(rootGraphId)
64 298 : if (hosts) return hosts
65 :
66 262 : const nextHosts = new Map<string, PreviewExposure[]>()
67 262 : exposures.value.set(rootGraphId, nextHosts)
68 262 : return nextHosts
69 : }
70 :
71 16422 : function _getExposuresRef(
72 : rootGraphId: UUID,
73 : hostNodeLocator: string
74 : ): PreviewExposure[] | undefined {
75 16422 : return exposures.value.get(rootGraphId)?.get(hostNodeLocator)
76 : }
77 :
78 16421 : function getExposures(
79 : rootGraphId: UUID,
80 : hostNodeLocator: string
81 : ): readonly PreviewExposure[] {
82 16421 : return _getExposuresRef(rootGraphId, hostNodeLocator) ?? EMPTY_EXPOSURES
83 : }
84 :
85 116 : function setExposures(
86 : rootGraphId: UUID,
87 : hostNodeLocator: string,
88 : next: readonly PreviewExposureInput[]
89 : ): void {
90 116 : const hosts = _getHostsForGraph(rootGraphId)
91 116 : if (next.length === 0) {
92 66 : hosts.delete(hostNodeLocator)
93 66 : if (hosts.size === 0) exposures.value.delete(rootGraphId)
94 66 : return
95 : }
96 50 : hosts.set(hostNodeLocator, next.map(normalizePreviewExposure))
97 : }
98 :
99 182 : function addExposure(
100 : rootGraphId: UUID,
101 : hostNodeLocator: string,
102 : source: { sourceNodeId: SerializedNodeId; sourcePreviewName: string }
103 : ): PreviewExposure {
104 182 : const hosts = _getHostsForGraph(rootGraphId)
105 182 : const current = hosts.get(hostNodeLocator) ?? []
106 182 : const existingNames = current.map((e) => e.name)
107 : const name = nextUniqueName(source.sourcePreviewName, existingNames)
108 182 : const entry: PreviewExposure = {
109 : name,
110 : sourceNodeId: toNodeId(source.sourceNodeId),
111 : sourcePreviewName: source.sourcePreviewName
112 : }
113 182 : hosts.set(hostNodeLocator, [...current, entry])
114 182 : return entry
115 : }
116 :
117 1 : function removeExposure(
118 : rootGraphId: UUID,
119 : hostNodeLocator: string,
120 : name: string
121 : ): void {
122 1 : const current = _getExposuresRef(rootGraphId, hostNodeLocator)
123 1 : if (!current?.length) return
124 1 : const next = current.filter((e) => e.name !== name)
125 1 : if (next.length === current.length) return
126 1 : setExposures(rootGraphId, hostNodeLocator, next)
127 : }
128 :
129 10998 : function clearHost(rootGraphId: UUID, hostNodeLocator: string): void {
130 10998 : const hosts = exposures.value.get(rootGraphId)
131 10998 : if (!hosts) return
132 159 : hosts.delete(hostNodeLocator)
133 474 : if (hosts.size === 0) exposures.value.delete(rootGraphId)
134 : }
135 :
136 11709 : function clearGraph(rootGraphId: UUID): void {
137 11709 : exposures.value.delete(rootGraphId)
138 : }
139 :
140 837 : function getExposuresAsPromotionShape(
141 : rootGraphId: UUID,
142 : hostNodeLocator: string
143 : ): PromotedWidgetSource[] {
144 837 : return getExposures(rootGraphId, hostNodeLocator).map((exposure) => ({
145 : sourceNodeId: exposure.sourceNodeId,
146 : sourceWidgetName: exposure.sourcePreviewName
147 : }))
148 : }
149 :
150 : /**
151 : * @param resolveNestedHost If provided, recurses through nested SubgraphNode
152 : * boundaries; otherwise the chain is a single-step walk on the starting host.
153 : */
154 185 : function resolveChain(
155 : rootGraphId: UUID,
156 : hostNodeLocator: string,
157 : name: string,
158 : resolveNestedHost?: ResolveNestedHostFn
159 : ): ResolvedPreviewChain | undefined {
160 : const ctx: PreviewExposureChainContext = {
161 : getExposures,
162 : resolveNestedHost: resolveNestedHost ?? (() => undefined)
163 : }
164 185 : return resolvePreviewExposureChain(rootGraphId, hostNodeLocator, name, ctx)
165 : }
166 :
167 2355 : return {
168 : getExposures,
169 : getExposuresAsPromotionShape,
170 : setExposures,
171 : addExposure,
172 : removeExposure,
173 : clearHost,
174 : clearGraph,
175 : resolveChain
176 : }
177 : })
|