[Manager] Fetch lists of node packs in single request (#3250)

This commit is contained in:
Christian Byrne
2025-03-27 08:49:05 -07:00
committed by GitHub
parent 6e72207927
commit 4bfc8e9e33
5 changed files with 181 additions and 100 deletions

View File

@@ -1,3 +1,5 @@
import QuickLRU from '@alloc/quick-lru'
import { partition } from 'lodash'
import { defineStore } from 'pinia'
import { useCachedRequest } from '@/composables/useCachedRequest'
@@ -5,7 +7,7 @@ import { useComfyRegistryService } from '@/services/comfyRegistryService'
import type { components, operations } from '@/types/comfyRegistryTypes'
const PACK_LIST_CACHE_SIZE = 20
const PACK_BY_ID_CACHE_SIZE = 50
const PACK_BY_ID_CACHE_SIZE = 64
type NodePack = components['schemas']['Node']
type ListPacksParams = operations['listAllNodes']['parameters']['query']
@@ -14,12 +16,21 @@ type ListPacksResult =
type ComfyNode = components['schemas']['ComfyNode']
type GetPackByIdPath = operations['getNode']['parameters']['path']['nodeId']
const isNodePack = (pack: NodePack | undefined): pack is NodePack => {
return pack !== undefined && 'id' in pack
}
/**
* Store for managing remote custom nodes
*/
export const useComfyRegistryStore = defineStore('comfyRegistry', () => {
const registryService = useComfyRegistryService()
let getPacksByIdController: AbortController | null = null
const getPacksByIdCache = new QuickLRU<NodePack['id'], NodePack>({
maxSize: PACK_BY_ID_CACHE_SIZE
})
/**
* Get a list of all node packs from the registry
*/
@@ -39,6 +50,41 @@ export const useComfyRegistryStore = defineStore('comfyRegistry', () => {
{ maxSize: PACK_BY_ID_CACHE_SIZE }
)
/**
* Get a list of packs by their IDs from the registry
*/
const getPacksByIds = async (ids: NodePack['id'][]): Promise<NodePack[]> => {
const [cachedPacksIds, uncachedPacksIds] = partition(ids, (id) =>
getPacksByIdCache.has(id)
)
const resolvedPacks = cachedPacksIds
.map((id) => getPacksByIdCache.get(id))
.filter(isNodePack)
if (uncachedPacksIds.length) {
getPacksByIdController = new AbortController()
const uncachedPacks = await registryService.listAllPacks(
{
node_id: uncachedPacksIds.filter(
(id): id is string => id !== undefined
)
},
getPacksByIdController.signal
)
const { nodes = [] } = uncachedPacks ?? {}
nodes.forEach((pack) => {
if (pack?.id) {
getPacksByIdCache.set(pack.id, pack)
resolvedPacks.push(pack)
}
})
}
return resolvedPacks
}
/**
* Get the node definitions for a pack
*/
@@ -63,11 +109,16 @@ export const useComfyRegistryStore = defineStore('comfyRegistry', () => {
getNodeDefs.cancel()
listAllPacks.cancel()
getPackById.cancel()
getPacksByIdController?.abort()
}
return {
listAllPacks,
getPackById,
getPacksByIds: {
call: getPacksByIds,
cancel: () => getPacksByIdController?.abort()
},
getNodeDefs,
clearCache,