LCOV - code coverage report
Current view: top level - src/platform/customNodes/manager/composables - useManagerQueue.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 84.6 % 39 33
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { useEventListener } from '@vueuse/core'
       2              : import { pickBy } from 'es-toolkit/compat'
       3              : import type { Ref } from 'vue'
       4              : import { computed, ref } from 'vue'
       5              : 
       6              : import { t } from '@/i18n'
       7              : import { useToast } from '@/components/ui/toast/toastStore'
       8              : import { app } from '@/scripts/app'
       9              : import { normalizePackKeys } from '@/platform/customNodes/manager/utils/packUtils'
      10              : import type { components } from '@comfyorg/api-client/generatedManagerTypes'
      11              : 
      12              : type ManagerTaskHistory = Record<
      13              :   string,
      14              :   components['schemas']['TaskHistoryItem']
      15              : >
      16              : type ManagerTaskQueue = components['schemas']['TaskStateMessage']
      17              : type ManagerWsTaskDoneMsg = components['schemas']['MessageTaskDone']
      18              : type ManagerWsTaskStartedMsg = components['schemas']['MessageTaskStarted']
      19              : type QueueTaskItem = components['schemas']['QueueTaskItem']
      20              : type HistoryTaskItem = components['schemas']['TaskHistoryItem']
      21              : type Task = QueueTaskItem | HistoryTaskItem
      22              : 
      23         2356 : const MANAGER_WS_TASK_DONE_NAME = 'cm-task-completed'
      24         2356 : const MANAGER_WS_TASK_STARTED_NAME = 'cm-task-started'
      25              : 
      26         2356 : export const useManagerQueue = (
      27              :   taskHistory: Ref<ManagerTaskHistory>,
      28              :   taskQueue: Ref<ManagerTaskQueue>,
      29              :   installedPacks: Ref<Record<string, unknown>>
      30              : ) => {
      31         2355 :   const toast = useToast()
      32              : 
      33              :   // Task queue state (read-only from server)
      34         2355 :   const maxHistoryItems = ref(64)
      35         2355 :   const isLoading = ref(false)
      36         2355 :   const isProcessing = ref(false)
      37              : 
      38              :   // Computed values
      39         2355 :   const currentQueueLength = computed(
      40              :     () =>
      41              :       taskQueue.value.running_queue.length +
      42              :       taskQueue.value.pending_queue.length
      43              :   )
      44              : 
      45              :   /**
      46              :    * Update the processing state based on the current queue length.
      47              :    * If the queue is empty, or all tasks in the queue are associated
      48              :    * with different clients, then this client is not processing any tasks.
      49              :    */
      50         2355 :   const updateProcessingState = (): void => {
      51            1 :     isProcessing.value = currentQueueLength.value > 0
      52              :   }
      53              : 
      54         2355 :   const allTasksDone = computed(() => currentQueueLength.value === 0)
      55         2355 :   const historyCount = computed(() => Object.keys(taskHistory.value).length)
      56              : 
      57              :   /**
      58              :    * Check if a task is associated with this client.
      59              :    * Task can be from running queue, pending queue, or history.
      60              :    * @param task - The task to check
      61              :    * @returns True if the task belongs to this client
      62              :    */
      63         2355 :   const isTaskFromThisClient = (task: Task): boolean =>
      64              :     task.client_id === app.api.clientId
      65              : 
      66              :   /**
      67              :    * Check if a history task is associated with this client.
      68              :    * @param task - The history task to check
      69              :    * @returns True if the task belongs to this client
      70              :    */
      71         2355 :   const isHistoryTaskFromThisClient = (task: HistoryTaskItem): boolean =>
      72              :     task.client_id === app.api.clientId
      73              : 
      74              :   /**
      75              :    * Filter queue tasks by client id.
      76              :    * Ensures that only tasks associated with this client are processed and
      77              :    * added to client state.
      78              :    * @param tasks - Array of queue tasks to filter
      79              :    * @returns Filtered array containing only tasks from this client
      80              :    */
      81         2355 :   const filterQueueByClientId = (tasks: QueueTaskItem[]): QueueTaskItem[] =>
      82              :     tasks.filter(isTaskFromThisClient)
      83              : 
      84              :   /**
      85              :    * Filter history tasks by client id using pickBy for optimal performance.
      86              :    * Returns a new object containing only tasks associated with this client.
      87              :    * @param history - The history object to filter
      88              :    * @returns Filtered history object containing only tasks from this client
      89              :    */
      90         2355 :   const filterHistoryByClientId = (history: ManagerTaskHistory) =>
      91              :     pickBy(history, isHistoryTaskFromThisClient)
      92              : 
      93              :   /**
      94              :    * Update task queue and history state with filtered data from server.
      95              :    * Ensures only tasks from this client are stored in local state.
      96              :    * @param state - The task state message from the server
      97              :    */
      98         2355 :   const updateTaskState = (state: ManagerTaskQueue) => {
      99            1 :     taskQueue.value.running_queue = filterQueueByClientId(state.running_queue)
     100            1 :     taskQueue.value.pending_queue = filterQueueByClientId(state.pending_queue)
     101            1 :     taskHistory.value = filterHistoryByClientId(state.history)
     102              : 
     103            1 :     installedPacks.value = normalizePackKeys(state.installed_packs)
     104            1 :     updateProcessingState()
     105              :   }
     106              : 
     107              :   // WebSocket event listener for task done
     108         2355 :   const cleanupTaskDoneListener = useEventListener(
     109              :     app.api,
     110              :     MANAGER_WS_TASK_DONE_NAME,
     111              :     (event: CustomEvent<ManagerWsTaskDoneMsg>) => {
     112            1 :       if (event.type === MANAGER_WS_TASK_DONE_NAME) {
     113            1 :         const { ui_id: taskId, state, status, result } = event.detail
     114            1 :         const wasCompleted = Object.hasOwn(taskHistory.value, taskId)
     115            1 :         const completedTask = state.history[taskId]
     116            1 :         updateTaskState(state)
     117            1 :         if (
     118              :           Object.hasOwn(state.history, taskId) &&
     119              :           (status ?? completedTask.status)?.status_str === 'error' &&
     120              :           isHistoryTaskFromThisClient(completedTask) &&
     121              :           !wasCompleted
     122              :         ) {
     123            1 :           toast.error(t('g.error'), { description: result })
     124              :         }
     125              :       }
     126              :     }
     127              :   )
     128              : 
     129              :   // WebSocket event listener for task started
     130         2355 :   const cleanupTaskStartedListener = useEventListener(
     131              :     app.api,
     132              :     MANAGER_WS_TASK_STARTED_NAME,
     133              :     (event: CustomEvent<ManagerWsTaskStartedMsg>) => {
     134            0 :       if (event.type === MANAGER_WS_TASK_STARTED_NAME) {
     135            0 :         updateTaskState(event.detail.state)
     136              :       }
     137              :     }
     138              :   )
     139              : 
     140              :   /**
     141              :    * Cleanup function to remove event listeners and reset state
     142              :    */
     143         2355 :   const cleanup = () => {
     144            0 :     cleanupTaskDoneListener()
     145            0 :     cleanupTaskStartedListener()
     146              : 
     147              :     // Reset state
     148            0 :     isProcessing.value = false
     149            0 :     isLoading.value = false
     150              :   }
     151              : 
     152         2355 :   return {
     153              :     // State
     154              :     isLoading,
     155              :     isProcessing,
     156              :     maxHistoryItems,
     157              : 
     158              :     // Computed
     159              :     allTasksDone,
     160              :     historyCount,
     161              :     currentQueueLength,
     162              : 
     163              :     // Actions
     164              :     updateTaskState,
     165              :     cleanup
     166              :   }
     167              : }
        

Generated by: LCOV version 2.0-1