Line data Source code
1 : import { useTimeoutFn } from '@vueuse/core'
2 : import { mapKeys } from 'es-toolkit'
3 : import { defineStore } from 'pinia'
4 : import { ref, toRaw } from 'vue'
5 :
6 : import type {
7 : LGraph,
8 : LGraphNode,
9 : SubgraphNode
10 : } from '@/lib/litegraph/src/litegraph'
11 : import { LiteGraph } from '@/lib/litegraph/src/litegraph'
12 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
13 : import type {
14 : ExecutedWsMessage,
15 : ResultItem
16 : } from '@comfyorg/api-client/executionTypes'
17 : import type { ResultItemType } from '@comfyorg/api-client/resultItemTypeSchema'
18 : import { parseNodeOutput } from '@/stores/resultItemParsing'
19 : import { resultItemPreviewUrl, resultItemUrl } from '@/utils/resultItemUrl'
20 : import { api } from '@/scripts/api'
21 : import { useApp } from '@/scripts/appInstance'
22 : import { clone } from '@/scripts/utils'
23 : import { reportError } from '@comfyorg/telemetry/reportError'
24 : import {
25 : createNodeLocatorId,
26 : parseNodeLocatorId
27 : } from '@comfyorg/graph-model/nodeIdentification'
28 : import type {
29 : NodeExecutionId,
30 : NodeLocatorId
31 : } from '@comfyorg/graph-model/nodeIdentification'
32 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
33 : import { parseAnnotatedPath } from '@/utils/createAnnotatedPath'
34 : import { parseFilePath } from '@/utils/formatUtil'
35 : import {
36 : executionIdToNodeLocatorId,
37 : getNodeByLocatorId
38 : } from '@/utils/graphTraversalUtil'
39 : import { parseImageWidgetValue } from '@/utils/imageUtil'
40 : import {
41 : isAnimatedOutput,
42 : isVideoNode,
43 : resolveNode
44 : } from '@/utils/litegraphUtil'
45 : import { isInputPreviewOutput } from '@/utils/nodeOutputUtil'
46 : import {
47 : releaseSharedObjectUrl,
48 : retainSharedObjectUrl
49 : } from '@/utils/objectUrlUtil'
50 :
51 2356 : const PREVIEW_REVOKE_DELAY_MS = 400
52 :
53 2356 : const createOutputs = (
54 : filenames: string[],
55 : type: ResultItemType,
56 : isAnimated: boolean
57 : ): ExecutedWsMessage['output'] => {
58 340 : return {
59 : images: filenames.map((image) => ({ type, ...parseFilePath(image) })),
60 : animated: filenames.map((image) => {
61 340 : const { filepath } = parseAnnotatedPath(image, type)
62 340 : return (
63 : isAnimated && (filepath.endsWith('.webp') || filepath.endsWith('.png'))
64 : )
65 : })
66 : }
67 : }
68 :
69 340 : function isResolvedPreviewOf(
70 : output: ExecutedWsMessage['output'] | undefined,
71 : widgetValue: string
72 : ): boolean {
73 340 : const images = output?.images ?? []
74 340 : if (images.length !== 1 || !images[0].preview_id) return false
75 1 : const [image] = images
76 1 : const { filename, subfolder, type } = parseImageWidgetValue(widgetValue)
77 340 : return (
78 : image.filename === filename &&
79 : (image.subfolder ?? '') === subfolder &&
80 : image.type === type
81 : )
82 : }
83 :
84 2356 : const isResultItem = (
85 : image: ResultItem | null | undefined
86 : ): image is ResultItem => image != null
87 :
88 : interface SetOutputOptions {
89 : merge?: boolean
90 : }
91 :
92 : type NodePreviewMap = Partial<Record<string, string[]>>
93 : type RuntimeOutput = Pick<ExecutedWsMessage['output'], 'animated'> & {
94 : images?: (ResultItem | null)[]
95 : }
96 :
97 0 : function locatesInGraph(
98 : rootGraph: LGraph | undefined,
99 : nodeLocatorId: string
100 : ): boolean {
101 0 : const subgraphUuid = parseNodeLocatorId(nodeLocatorId)?.subgraphUuid
102 0 : return !subgraphUuid || !!rootGraph?.subgraphs.has(subgraphUuid)
103 : }
104 :
105 2356 : export const useNodeOutputStore = defineStore('nodeOutput', () => {
106 2355 : const workflowStore = useWorkflowStore()
107 2355 : const { nodeIdToNodeLocatorId, nodeToNodeLocatorId } = workflowStore
108 2355 : const scheduledRevoke: Partial<Record<NodeLocatorId, { stop: () => void }>> =
109 : {}
110 2355 : const latestPreview = ref<string[]>([])
111 : /**
112 : * Previews belonging to open-but-inactive workflows, keyed by workflow path.
113 : * The stash owns the object URL retain taken when the previews were set.
114 : */
115 2355 : const stashedPreviews = new Map<string, NodePreviewMap>()
116 :
117 58 : function scheduleRevoke(locator: NodeLocatorId, cb: () => void) {
118 58 : scheduledRevoke[locator]?.stop()
119 :
120 58 : const { stop } = useTimeoutFn(() => {
121 27 : delete scheduledRevoke[locator]
122 27 : cb()
123 : }, PREVIEW_REVOKE_DELAY_MS)
124 :
125 58 : scheduledRevoke[locator] = { stop }
126 : }
127 :
128 2355 : const nodeOutputs = ref<Partial<Record<string, ExecutedWsMessage['output']>>>(
129 : {
130 : ...useApp().nodeOutputs
131 : }
132 : )
133 2355 : const nodePreviewImages = ref<NodePreviewMap>(
134 : Object.fromEntries(
135 : Object.entries(useApp().nodePreviewImages).flatMap(([id, urls]) =>
136 : urls ? [[id, [...urls]]] : []
137 : )
138 : )
139 : )
140 :
141 80900 : function getNodeOutputs(
142 : node: LGraphNode
143 : ): ExecutedWsMessage['output'] | undefined {
144 80900 : return nodeOutputs.value[nodeToNodeLocatorId(node)]
145 : }
146 :
147 67163 : function getNodePreviews(node: LGraphNode): string[] | undefined {
148 67163 : return nodePreviewImages.value[nodeToNodeLocatorId(node)]
149 : }
150 :
151 2355 : const isImageOutputs = (
152 : node: LGraphNode,
153 : outputs: RuntimeOutput
154 : ): boolean => {
155 625 : if (isAnimatedOutput(outputs) || isVideoNode(node)) return false
156 :
157 625 : const images = outputs.images?.filter((image) => image !== null)
158 625 : if (!images?.length) return false
159 :
160 577 : if (images.some((image) => image.filename?.toLowerCase().endsWith('.svg')))
161 0 : return false
162 :
163 557 : return true
164 : }
165 :
166 625 : function getPreviewParam(
167 : node: LGraphNode,
168 : outputs: ExecutedWsMessage['output']
169 : ): string {
170 625 : return isImageOutputs(node, outputs) ? useApp().getPreviewFormatParam() : ''
171 : }
172 :
173 2355 : const randParamByOutputs = new WeakMap<object, string>()
174 :
175 520 : function randParamFor(outputs: ExecutedWsMessage['output']): string {
176 520 : const key = toRaw(outputs)
177 520 : let rand = randParamByOutputs.get(key)
178 520 : if (rand === undefined) {
179 370 : rand = useApp().getRandParam()
180 370 : randParamByOutputs.set(key, rand)
181 : }
182 520 : return rand
183 : }
184 :
185 280 : function getNodeOutputItems(
186 : node: LGraphNode,
187 : outputs = getNodeOutputs(node)
188 : ) {
189 280 : if (!outputs) return []
190 280 : const rand = randParamFor(outputs)
191 280 : return parseNodeOutput(node.id, outputs).map((item) => {
192 385 : const { filepath, rootFolder } = parseAnnotatedPath(
193 : item.filename,
194 : item.type
195 : )
196 385 : const result = {
197 : ...item,
198 : filename:
199 : node.comfyClass === 'LoadImageOutput' ? item.filename : filepath,
200 : type: rootFolder
201 : }
202 385 : return {
203 : ...result,
204 : url: result.url ?? resultItemUrl(result) + rand,
205 : previewUrl: resultItemPreviewUrl(result, {
206 : previewFormat: getPreviewParam(node, outputs),
207 : rand
208 : })
209 : }
210 : })
211 : }
212 :
213 247 : function buildImageUrls(
214 : node: LGraphNode,
215 : outputs: ExecutedWsMessage['output'] | undefined
216 : ): string[] | undefined {
217 247 : if (!outputs?.images?.length) return
218 :
219 240 : const rand = randParamFor(outputs)
220 240 : const previewParam = getPreviewParam(node, outputs)
221 :
222 240 : return outputs.images.filter(isResultItem).map((image) => {
223 244 : const filename = image.filename ?? ''
224 244 : const { filepath, rootFolder } = parseAnnotatedPath(filename, image.type)
225 244 : const params = new URLSearchParams({
226 : ...image,
227 : filename: node.comfyClass === 'LoadImageOutput' ? filename : filepath,
228 : type: rootFolder
229 : })
230 244 : return api.apiURL(`/view?${params}${previewParam}${rand}`)
231 : })
232 : }
233 :
234 247 : function getNodeImageUrls(node: LGraphNode): string[] | undefined {
235 247 : const previews = getNodePreviews(node)
236 247 : if (previews?.length) return previews
237 :
238 247 : return buildImageUrls(node, getNodeOutputs(node))
239 : }
240 :
241 180 : function getNodeOutputByExecutionId(
242 : executionId: NodeExecutionId
243 : ): ExecutedWsMessage['output'] | undefined {
244 180 : const locatorId = executionIdToNodeLocatorId(
245 : useApp().rootGraph,
246 : executionId
247 : )
248 180 : if (!locatorId) return undefined
249 179 : return nodeOutputs.value[locatorId]
250 : }
251 :
252 180 : function getNodePreviewImagesByExecutionId(
253 : executionId: NodeExecutionId
254 : ): string[] | undefined {
255 180 : const locatorId = executionIdToNodeLocatorId(
256 : useApp().rootGraph,
257 : executionId
258 : )
259 180 : if (!locatorId) return undefined
260 179 : return nodePreviewImages.value[locatorId]
261 : }
262 :
263 0 : function getNodeImageUrlsByExecutionId(
264 : executionId: NodeExecutionId,
265 : node: LGraphNode
266 : ): string[] | undefined {
267 0 : const previews = getNodePreviewImagesByExecutionId(executionId)
268 0 : if (previews?.length) return previews
269 :
270 0 : return buildImageUrls(node, getNodeOutputByExecutionId(executionId))
271 : }
272 :
273 497 : function setOutputsByLocatorId(
274 : nodeLocatorId: NodeLocatorId,
275 : outputs: ExecutedWsMessage['output'] | ResultItem | null | undefined,
276 : options: SetOutputOptions = {}
277 : ) {
278 497 : if (outputs == null) return
279 :
280 497 : const incomingImages = 'images' in outputs ? outputs.images : undefined
281 497 : const hasIncomingImages =
282 : Array.isArray(incomingImages) && incomingImages.length > 0
283 497 : const existingOutput = nodeOutputs.value[nodeLocatorId]
284 497 : if (!hasIncomingImages && isInputPreviewOutput(existingOutput)) {
285 0 : outputs = {
286 : ...outputs,
287 : images: existingOutput.images
288 : }
289 : }
290 :
291 497 : if (options.merge && existingOutput) {
292 0 : const mergedOutput = { ...existingOutput }
293 0 : for (const [key, newValue] of Object.entries(outputs)) {
294 0 : const existingValue = existingOutput[key]
295 :
296 0 : if (Array.isArray(existingValue) && Array.isArray(newValue)) {
297 0 : mergedOutput[key] = existingValue.concat(newValue)
298 : } else {
299 0 : mergedOutput[key] = newValue
300 : }
301 : }
302 0 : nodeOutputs.value[nodeLocatorId] = mergedOutput
303 0 : useApp().nodeOutputs[nodeLocatorId] = clone(mergedOutput)
304 0 : return
305 : }
306 :
307 497 : nodeOutputs.value[nodeLocatorId] = outputs
308 497 : useApp().nodeOutputs[nodeLocatorId] = clone(outputs)
309 : }
310 :
311 341 : function setNodeOutputs(
312 : node: LGraphNode | null | undefined,
313 : filenames: string | string[] | ResultItem,
314 : {
315 : folder = 'input',
316 : isAnimated = false
317 : }: { folder?: ResultItemType; isAnimated?: boolean } = {}
318 : ) {
319 341 : if (!filenames || !node) return
320 :
321 340 : const locatorId = nodeToNodeLocatorId(node)
322 340 : if (!locatorId) return
323 340 : if (typeof filenames === 'string') {
324 340 : const existingOutput = nodeOutputs.value[locatorId]
325 340 : const outputs = createOutputs([filenames], folder, isAnimated)
326 340 : setOutputsByLocatorId(
327 : locatorId,
328 : isResolvedPreviewOf(existingOutput, filenames)
329 : ? { ...outputs, images: existingOutput?.images }
330 : : outputs
331 : )
332 0 : } else if (!Array.isArray(filenames)) {
333 0 : setOutputsByLocatorId(locatorId, filenames)
334 : } else {
335 0 : const resultItems = createOutputs(filenames, folder, isAnimated)
336 0 : if (!resultItems.images?.length) return
337 0 : setOutputsByLocatorId(locatorId, resultItems)
338 : }
339 : }
340 :
341 0 : function setNodeOutputImages(
342 : node: LGraphNode,
343 : images: NonNullable<ExecutedWsMessage['output']['images']>
344 : ) {
345 0 : const locatorId = nodeToNodeLocatorId(node)
346 0 : if (!locatorId) return
347 :
348 0 : setOutputsByLocatorId(locatorId, {
349 : ...nodeOutputs.value[locatorId],
350 : images
351 : })
352 0 : node.images = images
353 : }
354 :
355 : /**
356 : * Replaces a node's output with exactly these images, dropping any media
357 : * metadata the previous output carried. `animated`, `video` and friends are
358 : * parallel to the images they were produced with, so an editor that rewrites
359 : * the image list wholesale must not inherit them.
360 : */
361 6 : function replaceNodeOutputImages(
362 : node: LGraphNode,
363 : images: NonNullable<ExecutedWsMessage['output']['images']>
364 : ) {
365 6 : if (!images.length) return
366 :
367 6 : const locatorId = nodeToNodeLocatorId(node)
368 6 : if (!locatorId) return
369 :
370 6 : revokePreviewsByLocatorId(locatorId)
371 6 : setOutputsByLocatorId(locatorId, { images })
372 6 : node.images = images
373 : }
374 :
375 154 : function setNodeOutputsByExecutionId(
376 : executionId: NodeExecutionId,
377 : outputs: ExecutedWsMessage['output'] | ResultItem | null | undefined,
378 : options: SetOutputOptions = {}
379 : ) {
380 154 : const rootGraph = useApp().rootGraphOrUndefined
381 154 : const nodeLocatorId = executionIdToNodeLocatorId(rootGraph, executionId)
382 154 : const node =
383 : rootGraph && nodeLocatorId
384 : ? getNodeByLocatorId(rootGraph, nodeLocatorId)
385 : : undefined
386 154 : if (!nodeLocatorId || (rootGraph && !node)) {
387 3 : reportUnresolvedExecutionId(executionId, outputs)
388 3 : return
389 : }
390 154 : const emptyTextOutput =
391 : outputs == null &&
392 : (node?.comfyClass === 'PreviewAny' || node?.comfyClass === 'SaveText')
393 154 : setOutputsByLocatorId(
394 : nodeLocatorId,
395 : emptyTextOutput ? {} : outputs,
396 : emptyTextOutput ? {} : options
397 : )
398 : }
399 :
400 3 : function reportUnresolvedExecutionId(
401 : executionId: NodeExecutionId,
402 : outputs: ExecutedWsMessage['output'] | ResultItem | null | undefined
403 : ) {
404 3 : const incomingImages =
405 : typeof outputs === 'object' && outputs !== null && 'images' in outputs
406 : ? outputs.images
407 : : undefined
408 3 : const filenames = Array.isArray(incomingImages)
409 : ? incomingImages
410 : .filter(isResultItem)
411 : .map((image) => image.filename)
412 : .filter((filename): filename is string => !!filename)
413 : : undefined
414 3 : reportError(new Error('No live node resolves the executed output'), {
415 : surface: 'graph',
416 : errorType: 'failure_resolving_node_output_execution_id',
417 : level: 'warning',
418 : tags: { executionId },
419 : context: {
420 : filenames: filenames?.length ? filenames : undefined
421 : }
422 : })
423 : }
424 :
425 5 : function setNodePreviewsByExecutionId(
426 : executionId: NodeExecutionId,
427 : previewImages: string[]
428 : ) {
429 5 : const nodeLocatorId = executionIdToNodeLocatorId(
430 : useApp().rootGraph,
431 : executionId
432 : )
433 5 : if (!nodeLocatorId) return
434 5 : setNodePreviewsByLocatorId(nodeLocatorId, previewImages)
435 5 : latestPreview.value = [...previewImages]
436 : }
437 :
438 37 : function setNodePreviewsByLocatorId(
439 : nodeLocatorId: NodeLocatorId,
440 : previewImages: string[]
441 : ) {
442 37 : const existingPreviews = nodePreviewImages.value[nodeLocatorId]
443 37 : scheduledRevoke[nodeLocatorId]?.stop()
444 37 : delete scheduledRevoke[nodeLocatorId]
445 37 : if (existingPreviews) {
446 13 : for (const url of existingPreviews) {
447 13 : releaseSharedObjectUrl(url)
448 : }
449 : }
450 37 : for (const url of previewImages) {
451 37 : retainSharedObjectUrl(url)
452 : }
453 37 : nodePreviewImages.value[nodeLocatorId] = [...previewImages]
454 37 : useApp().nodePreviewImages[nodeLocatorId] = [...previewImages]
455 : }
456 :
457 28 : function setNodePreviewsByNodeId(nodeId: NodeId, previewImages: string[]) {
458 28 : setNodePreviewsByLocatorId(nodeIdToNodeLocatorId(nodeId), previewImages)
459 : }
460 :
461 58 : function revokePreviewsByExecutionId(executionId: NodeExecutionId) {
462 58 : const nodeLocatorId = executionIdToNodeLocatorId(
463 : useApp().rootGraph,
464 : executionId
465 : )
466 58 : if (!nodeLocatorId) return
467 58 : scheduleRevoke(nodeLocatorId, () =>
468 : revokePreviewsByLocatorId(nodeLocatorId)
469 : )
470 : }
471 :
472 69 : function revokePreviewsByLocatorId(nodeLocatorId: NodeLocatorId) {
473 69 : const previews = nodePreviewImages.value[nodeLocatorId]
474 69 : if (!previews) return
475 :
476 2 : for (const url of previews) {
477 2 : releaseSharedObjectUrl(url)
478 : }
479 :
480 2 : delete nodePreviewImages.value[nodeLocatorId]
481 2 : delete useApp().nodePreviewImages[nodeLocatorId]
482 : }
483 :
484 3915 : function releasePreviewUrls(previews: NodePreviewMap) {
485 3915 : for (const urls of Object.values(previews)) {
486 0 : if (!urls) continue
487 :
488 0 : for (const url of urls) {
489 0 : releaseSharedObjectUrl(url)
490 : }
491 : }
492 : }
493 :
494 3915 : function revokeAllPreviews() {
495 3915 : releasePreviewUrls(nodePreviewImages.value)
496 3915 : nodePreviewImages.value = {}
497 3915 : useApp().nodePreviewImages = {}
498 : }
499 :
500 : /** Release the previews held for a workflow that is going away. */
501 1580 : function discardPreviewsForWorkflow(workflowPath: string) {
502 1580 : const stashed = stashedPreviews.get(workflowPath)
503 1580 : if (!stashed) return
504 :
505 0 : stashedPreviews.delete(workflowPath)
506 0 : releasePreviewUrls(stashed)
507 : }
508 :
509 1548 : function discardClosedWorkflowPreviews() {
510 1548 : const openPaths = new Set(workflowStore.openWorkflows.map((wf) => wf.path))
511 1548 : for (const path of Array.from(stashedPreviews.keys())) {
512 0 : if (!openPaths.has(path)) discardPreviewsForWorkflow(path)
513 : }
514 : }
515 :
516 : /**
517 : * Move the live previews into the stash so `app.clean()` cannot revoke them
518 : * while `workflowPath` is loaded out of the editor. Preview frames arrive
519 : * over the websocket and cannot be re-fetched, so releasing their object
520 : * URLs on a tab switch loses them for good.
521 : */
522 1548 : function stashPreviewsForWorkflow(workflowPath: string) {
523 1548 : discardClosedWorkflowPreviews()
524 1548 : discardPreviewsForWorkflow(workflowPath)
525 :
526 1548 : const previews = nodePreviewImages.value
527 1548 : if (Object.keys(previews).length) {
528 0 : stashedPreviews.set(workflowPath, previews)
529 : }
530 1548 : nodePreviewImages.value = {}
531 1548 : useApp().nodePreviewImages = {}
532 : }
533 :
534 0 : function addLivePreviewsToStash(workflowPath: string) {
535 0 : const stashed = stashedPreviews.get(workflowPath)
536 0 : if (!stashed) {
537 0 : stashPreviewsForWorkflow(workflowPath)
538 0 : return
539 : }
540 :
541 0 : const superseded: Record<string, string[]> = {}
542 0 : for (const [nodeLocatorId, urls] of Object.entries(
543 : nodePreviewImages.value
544 : )) {
545 0 : if (!urls) continue
546 :
547 0 : const replaced = stashed[nodeLocatorId]
548 0 : if (replaced) superseded[nodeLocatorId] = replaced
549 0 : stashed[nodeLocatorId] = urls
550 : }
551 0 : releasePreviewUrls(superseded)
552 0 : nodePreviewImages.value = {}
553 0 : useApp().nodePreviewImages = {}
554 : }
555 :
556 3897 : function restorePreviewsForWorkflow(workflowPath: string | undefined) {
557 3897 : if (!workflowPath) return
558 :
559 3897 : const stashed = stashedPreviews.get(workflowPath)
560 3897 : if (!stashed) return
561 0 : stashedPreviews.delete(workflowPath)
562 :
563 0 : const live = nodePreviewImages.value
564 0 : const rootGraph = useApp().rootGraphOrUndefined
565 0 : const released: Record<string, string[]> = {}
566 0 : for (const [nodeLocatorId, urls] of Object.entries(stashed)) {
567 0 : if (!urls) continue
568 :
569 0 : if (live[nodeLocatorId] || !locatesInGraph(rootGraph, nodeLocatorId))
570 0 : released[nodeLocatorId] = urls
571 0 : else live[nodeLocatorId] = urls
572 : }
573 0 : releasePreviewUrls(released)
574 0 : nodePreviewImages.value = { ...live }
575 0 : useApp().nodePreviewImages = clone(nodePreviewImages.value)
576 : }
577 :
578 12 : function revokeSubgraphPreviews(subgraphNode: SubgraphNode, graph: LGraph) {
579 12 : revokePreviewsByLocatorId(
580 : createNodeLocatorId(graph.isRootGraph ? null : graph.id, subgraphNode.id)
581 : )
582 12 : for (const node of subgraphNode.subgraph.nodes) {
583 22 : revokePreviewsByLocatorId(
584 : createNodeLocatorId(subgraphNode.subgraph.id, node.id)
585 : )
586 : }
587 : }
588 :
589 0 : function removeOutputsByLocatorId(nodeLocatorId: NodeLocatorId) {
590 0 : const hadOutputs = !!nodeOutputs.value[nodeLocatorId]
591 0 : delete nodeOutputs.value[nodeLocatorId]
592 0 : delete useApp().nodeOutputs[nodeLocatorId]
593 :
594 0 : const previews = nodePreviewImages.value[nodeLocatorId]
595 0 : if (previews) {
596 0 : for (const url of previews) {
597 0 : releaseSharedObjectUrl(url)
598 : }
599 0 : delete nodePreviewImages.value[nodeLocatorId]
600 0 : delete useApp().nodePreviewImages[nodeLocatorId]
601 : }
602 :
603 0 : return hadOutputs
604 : }
605 :
606 0 : function removeNodeOutputs(nodeId: NodeId) {
607 0 : const nodeLocatorId = nodeIdToNodeLocatorId(nodeId)
608 0 : if (!nodeLocatorId) return false
609 0 : return removeOutputsByLocatorId(nodeLocatorId)
610 : }
611 :
612 0 : function removeNodeOutputsForNode(node: LGraphNode) {
613 0 : return removeOutputsByLocatorId(nodeToNodeLocatorId(node))
614 : }
615 :
616 1731 : function snapshotOutputs(): Record<string, ExecutedWsMessage['output']> {
617 1731 : return Object.fromEntries(
618 : Object.entries(nodeOutputs.value).flatMap(([id, output]) =>
619 : output ? [[id, clone(output)]] : []
620 : )
621 : )
622 : }
623 :
624 4281 : function replaceOutputsFromLegacy(
625 : outputs: Partial<Record<string, ExecutedWsMessage['output']>>
626 : ) {
627 4281 : const parsedOutputs = mapKeys(
628 : outputs,
629 : (_, id) => executionIdToNodeLocatorId(useApp().rootGraph, id) ?? id
630 : )
631 4281 : nodeOutputs.value = { ...parsedOutputs }
632 : }
633 :
634 520 : function setOutputFromLegacy(
635 : id: string,
636 : output: ExecutedWsMessage['output']
637 : ): void {
638 520 : const locatorId = executionIdToNodeLocatorId(useApp().rootGraph, id) ?? id
639 520 : nodeOutputs.value[locatorId] = { ...output }
640 : }
641 :
642 0 : function removeOutputFromLegacy(id: string): void {
643 0 : const locatorId = executionIdToNodeLocatorId(useApp().rootGraph, id) ?? id
644 0 : delete nodeOutputs.value[locatorId]
645 : }
646 :
647 183 : function restoreOutputs(
648 : outputs: Partial<Record<string, ExecutedWsMessage['output']>>
649 : ) {
650 183 : replaceOutputsFromLegacy(outputs)
651 183 : useApp().nodeOutputs = snapshotOutputs()
652 : }
653 :
654 1 : function refreshNodeOutputs(node: LGraphNode) {
655 1 : const locatorId = nodeToNodeLocatorId(node)
656 1 : if (!locatorId) return
657 :
658 1 : const outputs = nodeOutputs.value[locatorId]
659 1 : if (!outputs) return
660 :
661 1 : nodeOutputs.value[locatorId] = { ...outputs }
662 : }
663 :
664 3915 : function resetAllOutputsAndPreviews() {
665 3915 : nodeOutputs.value = {}
666 3915 : useApp().nodeOutputs = {}
667 3915 : revokeAllPreviews()
668 : }
669 :
670 196 : function syncLegacyNodeImgs(
671 : nodeId: NodeId,
672 : element: HTMLImageElement,
673 : activeIndex: number = 0
674 : ) {
675 196 : if (!LiteGraph.vueNodesMode) return
676 :
677 196 : const node = resolveNode(nodeId)
678 196 : if (!node) return
679 :
680 196 : node.imgs = [element]
681 196 : node.imageIndex = activeIndex
682 :
683 196 : const outputs = getNodeOutputs(node)
684 196 : if (outputs?.images) node.images = outputs.images
685 : }
686 :
687 2355 : return {
688 : getNodeOutputs,
689 : getNodeOutputItems,
690 : getNodeImageUrls,
691 : getNodeImageUrlsByExecutionId,
692 : getNodeOutputByExecutionId,
693 : getNodePreviewImagesByExecutionId,
694 : getNodePreviews,
695 : getPreviewParam,
696 :
697 : setNodeOutputs,
698 : setNodeOutputImages,
699 : replaceNodeOutputImages,
700 : setNodeOutputsByExecutionId,
701 : setNodePreviewsByExecutionId,
702 : setNodePreviewsByLocatorId,
703 : setNodePreviewsByNodeId,
704 : refreshNodeOutputs,
705 : syncLegacyNodeImgs,
706 :
707 : revokePreviewsByExecutionId,
708 : revokePreviewsByLocatorId,
709 : revokeAllPreviews,
710 : revokeSubgraphPreviews,
711 : stashPreviewsForWorkflow,
712 : addLivePreviewsToStash,
713 : restorePreviewsForWorkflow,
714 : discardPreviewsForWorkflow,
715 : removeNodeOutputs,
716 : removeNodeOutputsForNode,
717 : snapshotOutputs,
718 : replaceOutputsFromLegacy,
719 : setOutputFromLegacy,
720 : removeOutputFromLegacy,
721 : restoreOutputs,
722 : resetAllOutputsAndPreviews,
723 :
724 : nodeOutputs,
725 : nodePreviewImages,
726 : latestPreview
727 : }
728 : })
|