Line data Source code
1 : import { whenever } from '@vueuse/core'
2 : import { computed, onUnmounted, ref } from 'vue'
3 :
4 : import type { components } from '@/types/comfyRegistryTypes'
5 : import { useNodePacks } from '@/platform/customNodes/manager/composables/nodePack/useNodePacks'
6 : import { useComfyManagerStore } from '@/platform/customNodes/manager/stores/comfyManagerStore'
7 : import type { UseNodePacksOptions } from '@/platform/customNodes/manager/types/comfyManagerTypes'
8 :
9 2356 : export const useInstalledPacks = (options: UseNodePacksOptions = {}) => {
10 9401 : const comfyManagerStore = useComfyManagerStore()
11 :
12 : // Flag to prevent duplicate fetches during initialization
13 9401 : const isInitializing = ref(false)
14 9401 : const lastFetchedIds = ref<string>('')
15 :
16 9401 : const installedPackIds = computed(() =>
17 : Array.from(comfyManagerStore.installedPacksIds)
18 : )
19 :
20 9401 : const { startFetch, cleanup, error, isLoading, nodePacks, isReady } =
21 : useNodePacks(installedPackIds, options)
22 :
23 9401 : const filterInstalledPack = (packs: components['schemas']['Node'][]) =>
24 : packs.filter((pack) => comfyManagerStore.isPackInstalled(pack.id))
25 :
26 9401 : const startFetchInstalled = async () => {
27 : // Prevent duplicate calls during initialization
28 31 : if (isInitializing.value) {
29 0 : return
30 : }
31 :
32 31 : isInitializing.value = true
33 31 : try {
34 31 : if (comfyManagerStore.installedPacksIds.size === 0) {
35 15 : await comfyManagerStore.refreshInstalledList()
36 : }
37 31 : await startFetch()
38 : } finally {
39 31 : isInitializing.value = false
40 : }
41 : }
42 :
43 : // When installedPackIds changes, we need to update the nodePacks
44 : // But only if the IDs actually changed (not just array reference)
45 9401 : whenever(installedPackIds, async (newIds) => {
46 28 : const newIdsStr = newIds.sort().join(',')
47 28 : if (newIdsStr !== lastFetchedIds.value && !isInitializing.value) {
48 13 : lastFetchedIds.value = newIdsStr
49 13 : await startFetch()
50 : }
51 : })
52 :
53 9401 : onUnmounted(() => {
54 519 : cleanup()
55 : })
56 :
57 : // Create a computed property that provides installed pack info with versions
58 : const installedPacksWithVersions = computed(() => {
59 15 : const result: Array<{ id: string; version: string }> = []
60 :
61 15 : for (const pack of Object.values(comfyManagerStore.installedPacks)) {
62 30 : const id = pack.cnr_id || pack.aux_id
63 30 : if (id) {
64 30 : result.push({
65 : id,
66 : version: pack.ver
67 : })
68 : }
69 : }
70 :
71 15 : return result
72 : })
73 :
74 9401 : return {
75 : error,
76 : isLoading,
77 : isReady,
78 : installedPacks: nodePacks,
79 : installedPacksWithVersions,
80 : startFetchInstalled,
81 : filterInstalledPack
82 : }
83 : }
|