Line data Source code
1 : import { defineStore } from 'pinia'
2 : import { computed, readonly, ref, watch } from 'vue'
3 :
4 : import { useSettingStore } from '@/platform/settings/settingStore'
5 : import type { JobId } from '@comfyorg/api-client/executionTypes'
6 : import {
7 : releaseSharedObjectUrl,
8 : retainSharedObjectUrl
9 : } from '@/utils/objectUrlUtil'
10 :
11 : type PromptPreviewMap = Record<JobId, string>
12 : interface NodePromptPreview {
13 : url: string
14 : nodeId?: string
15 : }
16 :
17 2356 : export const useJobPreviewStore = defineStore('jobPreview', () => {
18 1977 : const settingStore = useSettingStore()
19 1977 : const nodePreviewsByPromptId = ref<Record<JobId, NodePromptPreview>>({})
20 :
21 1977 : const previewMethod = computed(() =>
22 : settingStore.get('Comfy.Execution.PreviewMethod')
23 : )
24 1977 : const isPreviewEnabled = computed(() => previewMethod.value !== 'none')
25 :
26 1977 : const previewsByPromptId = computed(() => {
27 7 : const result: PromptPreviewMap = {}
28 7 : for (const [k, v] of Object.entries(nodePreviewsByPromptId.value)) {
29 0 : result[k] = v.url
30 : }
31 7 : return result
32 : })
33 :
34 4 : function setPreviewUrl(
35 : promptId: JobId | undefined,
36 : url: string,
37 : nodeId?: string
38 : ) {
39 4 : if (!promptId || !isPreviewEnabled.value) return
40 4 : const current = nodePreviewsByPromptId.value[promptId]
41 4 : if (promptId in nodePreviewsByPromptId.value) {
42 0 : if (current.url === url) return
43 0 : releaseSharedObjectUrl(current.url)
44 : }
45 4 : retainSharedObjectUrl(url)
46 4 : nodePreviewsByPromptId.value = {
47 : ...nodePreviewsByPromptId.value,
48 : [promptId]: { url, nodeId }
49 : }
50 : }
51 :
52 98 : function clearPreview(promptId: JobId | undefined) {
53 98 : if (!promptId) return
54 98 : if (!(promptId in nodePreviewsByPromptId.value)) return
55 1 : const current = nodePreviewsByPromptId.value[promptId]
56 1 : releaseSharedObjectUrl(current.url)
57 1 : const next = { ...nodePreviewsByPromptId.value }
58 1 : delete next[promptId]
59 1 : nodePreviewsByPromptId.value = next
60 : }
61 :
62 0 : function clearAllPreviews() {
63 0 : for (const { url } of Object.values(nodePreviewsByPromptId.value)) {
64 0 : releaseSharedObjectUrl(url)
65 : }
66 0 : nodePreviewsByPromptId.value = {}
67 : }
68 :
69 1977 : watch(isPreviewEnabled, (enabled) => {
70 0 : if (!enabled) clearAllPreviews()
71 : })
72 :
73 1977 : return {
74 : nodePreviewsByPromptId: readonly(nodePreviewsByPromptId),
75 : previewsByPromptId,
76 : isPreviewEnabled,
77 : setPreviewUrl,
78 : clearPreview,
79 : clearAllPreviews
80 : }
81 : })
|