Line data Source code
1 : import type { CompactSize } from '@comfyorg/shared-frontend-utils/geometry'
2 : import { reactive } from 'vue'
3 :
4 : import type { CompositorBBox } from '@comfyorg/layer-editor/persistence/compositorLayerState'
5 : import type { ImageFileRef } from '@/renderer/extensions/layerEditor/compositor/compositorPaths'
6 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
7 : import { createNodeLocatorId } from '@comfyorg/graph-model/nodeIdentification'
8 : import type { NodeLocatorId } from '@comfyorg/graph-model/nodeIdentification'
9 : import { isSubgraph } from '@/utils/typeGuardUtil'
10 :
11 : type CompositorNodeRef = Pick<LGraphNode, 'id' | 'graph'>
12 :
13 : interface CompositorNodeCache {
14 : layers: ImageFileRef[]
15 : inputsFingerprint?: string[]
16 : bboxes?: (CompositorBBox | null)[]
17 : canvas?: CompactSize
18 : }
19 :
20 2355 : const cacheByNode = reactive(new Map<NodeLocatorId, CompositorNodeCache>())
21 2355 : const previewOverrideByNode = reactive(new Map<NodeLocatorId, string>())
22 :
23 0 : function cacheKey(node: CompositorNodeRef): NodeLocatorId {
24 0 : return createNodeLocatorId(
25 : isSubgraph(node.graph) ? node.graph.id : null,
26 : node.id
27 : )
28 : }
29 :
30 0 : export function setCompositorLayers(
31 : node: CompositorNodeRef,
32 : refs: ImageFileRef[],
33 : inputsFingerprint?: string[],
34 : bboxes?: (CompositorBBox | null)[],
35 : canvas?: CompactSize
36 : ): void {
37 0 : cacheByNode.set(cacheKey(node), {
38 : layers: refs,
39 : ...(inputsFingerprint ? { inputsFingerprint } : {}),
40 : ...(bboxes ? { bboxes } : {}),
41 : ...(canvas ? { canvas } : {})
42 : })
43 : }
44 :
45 0 : export function getCompositorLayers(
46 : node: CompositorNodeRef
47 : ): ImageFileRef[] | undefined {
48 0 : return cacheByNode.get(cacheKey(node))?.layers
49 : }
50 :
51 0 : export function getCompositorInputsFingerprint(
52 : node: CompositorNodeRef
53 : ): string[] | undefined {
54 0 : return cacheByNode.get(cacheKey(node))?.inputsFingerprint
55 : }
56 :
57 0 : export function getCompositorBBoxes(
58 : node: CompositorNodeRef
59 : ): (CompositorBBox | null)[] | undefined {
60 0 : return cacheByNode.get(cacheKey(node))?.bboxes
61 : }
62 :
63 0 : export function getCompositorCanvas(
64 : node: CompositorNodeRef
65 : ): CompactSize | undefined {
66 0 : return cacheByNode.get(cacheKey(node))?.canvas
67 : }
68 :
69 0 : export function clearCompositorLayers(node: CompositorNodeRef): void {
70 0 : cacheByNode.delete(cacheKey(node))
71 0 : clearCompositorPreviewOverride(node)
72 : }
73 :
74 0 : function revokeIfObjectUrl(url: string | undefined): void {
75 0 : if (url?.startsWith('blob:')) URL.revokeObjectURL(url)
76 : }
77 :
78 0 : export function setCompositorPreviewOverride(
79 : node: CompositorNodeRef,
80 : url: string
81 : ): void {
82 0 : const key = cacheKey(node)
83 0 : revokeIfObjectUrl(previewOverrideByNode.get(key))
84 0 : previewOverrideByNode.set(key, url)
85 : }
86 :
87 0 : export function getCompositorPreviewOverride(
88 : node: CompositorNodeRef
89 : ): string | undefined {
90 0 : return previewOverrideByNode.get(cacheKey(node))
91 : }
92 :
93 0 : export function clearCompositorPreviewOverride(node: CompositorNodeRef): void {
94 0 : const key = cacheKey(node)
95 0 : revokeIfObjectUrl(previewOverrideByNode.get(key))
96 0 : previewOverrideByNode.delete(key)
97 : }
98 :
99 0 : export function hasCompositorLayers(node: CompositorNodeRef): boolean {
100 0 : return (cacheByNode.get(cacheKey(node))?.layers.length ?? 0) > 0
101 : }
|