LCOV - code coverage report
Current view: top level - src/composables/queue - useQueueNotificationBanners.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 95.2 % 145 138
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { computed, nextTick, onUnmounted, ref, watch } from 'vue'
       2              : 
       3              : import { api } from '@/scripts/api'
       4              : import type {
       5              :   PromptQueuedEventPayload,
       6              :   PromptQueueingEventPayload
       7              : } from '@comfyorg/api-client/ComfyApi'
       8              : import { useExecutionStore } from '@/stores/executionStore'
       9              : import { useQueueStore } from '@/stores/queueStore'
      10              : import { resultItemUrlWithTimestamp } from '@/utils/resultItemUrl'
      11              : import { isImageResult } from '@/utils/resultItem'
      12              : import { jobStateFromTask } from '@/utils/queueUtil'
      13              : 
      14         2355 : const BANNER_DISMISS_DELAY_MS = 4000
      15         2355 : const MAX_COMPLETION_THUMBNAILS = 2
      16              : 
      17              : type QueueQueuedNotificationType = 'queuedPending' | 'queued'
      18              : 
      19              : type QueueQueuedNotification = {
      20              :   type: QueueQueuedNotificationType
      21              :   count: number
      22              :   requestId?: number
      23              : }
      24              : 
      25              : type QueueCompletedNotification = {
      26              :   type: 'completed'
      27              :   count: number
      28              :   thumbnailUrls?: string[]
      29              : }
      30              : 
      31              : type QueueFailedNotification = {
      32              :   type: 'failed'
      33              :   count: number
      34              : }
      35              : 
      36              : export type QueueNotificationBanner =
      37              :   | QueueQueuedNotification
      38              :   | QueueCompletedNotification
      39              :   | QueueFailedNotification
      40              : 
      41         2355 : const isRunAcknowledgement = (notification: QueueNotificationBanner) =>
      42              :   notification.type === 'queuedPending' || notification.type === 'queued'
      43              : 
      44         2355 : const isOutcome = (notification: QueueNotificationBanner) =>
      45              :   notification.type === 'completed' || notification.type === 'failed'
      46              : 
      47         2355 : const sanitizeCount = (value: number | undefined) => {
      48          333 :   if (!(typeof value === 'number' && value > 0)) {
      49            0 :     return 1
      50              :   }
      51          333 :   return Math.floor(value)
      52              : }
      53              : 
      54         2355 : export const useQueueNotificationBanners = () => {
      55         2009 :   const queueStore = useQueueStore()
      56         2009 :   const executionStore = useExecutionStore()
      57              : 
      58         2009 :   const pendingNotifications = ref<QueueNotificationBanner[]>([])
      59         2009 :   const activeNotification = ref<QueueNotificationBanner | null>(null)
      60         2009 :   const dismissTimer = ref<number | null>(null)
      61         2009 :   const lastActiveStartTs = ref<number | null>(null)
      62         2009 :   let stopIdleHistoryWatch: (() => void) | null = null
      63         2009 :   let idleCompletionScheduleToken = 0
      64         2009 :   const isQueueActive = computed(
      65              :     () =>
      66              :       queueStore.pendingTasks.length > 0 ||
      67              :       queueStore.runningTasks.length > 0 ||
      68              :       !executionStore.isIdle
      69              :   )
      70              : 
      71         2009 :   const clearIdleCompletionHooks = () => {
      72          330 :     idleCompletionScheduleToken++
      73          330 :     if (!stopIdleHistoryWatch) {
      74          285 :       return
      75              :     }
      76           45 :     stopIdleHistoryWatch()
      77           45 :     stopIdleHistoryWatch = null
      78              :   }
      79              : 
      80         2009 :   const clearDismissTimer = () => {
      81          420 :     if (dismissTimer.value === null) {
      82          417 :       return
      83              :     }
      84            3 :     clearTimeout(dismissTimer.value)
      85            3 :     dismissTimer.value = null
      86              :   }
      87              : 
      88         2009 :   const dismissActiveNotification = () => {
      89            5 :     activeNotification.value = null
      90            5 :     dismissTimer.value = null
      91            5 :     showNextNotification()
      92              :   }
      93              : 
      94         2009 :   const showNextNotification = () => {
      95          206 :     if (
      96              :       activeNotification.value !== null ||
      97              :       pendingNotifications.value.length === 0
      98              :     ) {
      99           29 :       return
     100              :     }
     101          177 :     const [nextNotification, ...rest] = pendingNotifications.value
     102          177 :     pendingNotifications.value = rest
     103              : 
     104          177 :     activeNotification.value = nextNotification
     105          177 :     clearDismissTimer()
     106          177 :     dismissTimer.value = window.setTimeout(
     107              :       dismissActiveNotification,
     108              :       BANNER_DISMISS_DELAY_MS
     109              :     )
     110              :   }
     111              : 
     112         2009 :   const queuePositionFor = (notification: QueueNotificationBanner) => {
     113           30 :     if (!isRunAcknowledgement(notification)) {
     114           12 :       return pendingNotifications.value.length
     115              :     }
     116           18 :     const firstOutcome = pendingNotifications.value.findIndex(isOutcome)
     117           30 :     return firstOutcome === -1
     118              :       ? pendingNotifications.value.length
     119              :       : firstOutcome
     120              :   }
     121              : 
     122         2009 :   const queueNotification = (notification: QueueNotificationBanner) => {
     123          201 :     if (isRunAcknowledgement(notification)) {
     124          189 :       const active = activeNotification.value
     125          189 :       if (active === null || isOutcome(active)) {
     126          171 :         clearDismissTimer()
     127          171 :         activeNotification.value = null
     128          171 :         pendingNotifications.value = [
     129              :           notification,
     130              :           ...pendingNotifications.value
     131              :         ]
     132          171 :         showNextNotification()
     133          171 :         return
     134              :       }
     135              :     }
     136              : 
     137           30 :     const pending = [...pendingNotifications.value]
     138           30 :     pending.splice(queuePositionFor(notification), 0, notification)
     139           30 :     pendingNotifications.value = pending
     140           30 :     showNextNotification()
     141              :   }
     142              : 
     143         2009 :   const toQueueLifecycleNotification = (
     144              :     type: QueueQueuedNotificationType,
     145              :     count: number,
     146              :     requestId?: number
     147              :   ): QueueQueuedNotification => {
     148          333 :     if (requestId === undefined) {
     149            0 :       return {
     150              :         type,
     151              :         count
     152              :       }
     153              :     }
     154          333 :     return {
     155              :       type,
     156              :       count,
     157              :       requestId
     158              :     }
     159              :   }
     160              : 
     161         2009 :   const toCompletedNotification = (
     162              :     count: number,
     163              :     thumbnailUrls: string[]
     164              :   ): QueueCompletedNotification => ({
     165              :     type: 'completed',
     166              :     count,
     167              :     thumbnailUrls: thumbnailUrls.slice(0, MAX_COMPLETION_THUMBNAILS)
     168              :   })
     169              : 
     170         2009 :   const toFailedNotification = (count: number): QueueFailedNotification => ({
     171              :     type: 'failed',
     172              :     count
     173              :   })
     174              : 
     175         2009 :   const convertQueuedPendingToQueued = (
     176              :     requestId: number | undefined,
     177              :     count: number
     178              :   ) => {
     179          150 :     if (
     180              :       activeNotification.value?.type === 'queuedPending' &&
     181              :       (requestId === undefined ||
     182              :         activeNotification.value.requestId === requestId)
     183              :     ) {
     184          129 :       activeNotification.value = toQueueLifecycleNotification(
     185              :         'queued',
     186              :         count,
     187              :         requestId
     188              :       )
     189          129 :       return true
     190              :     }
     191              : 
     192           21 :     const pendingIndex = pendingNotifications.value.findIndex(
     193              :       (notification) =>
     194              :         notification.type === 'queuedPending' &&
     195              :         (requestId === undefined || notification.requestId === requestId)
     196              :     )
     197              : 
     198           87 :     if (pendingIndex === -1) {
     199            6 :       return false
     200              :     }
     201              : 
     202           15 :     const queuedPendingNotification = pendingNotifications.value[pendingIndex]
     203           25 :     if (queuedPendingNotification.type !== 'queuedPending') {
     204            0 :       return false
     205              :     }
     206              : 
     207           15 :     pendingNotifications.value = [
     208              :       ...pendingNotifications.value.slice(0, pendingIndex),
     209              :       toQueueLifecycleNotification(
     210              :         'queued',
     211              :         count,
     212              :         queuedPendingNotification.requestId
     213              :       ),
     214              :       ...pendingNotifications.value.slice(pendingIndex + 1)
     215              :     ]
     216              : 
     217           15 :     return true
     218              :   }
     219              : 
     220         2009 :   const handlePromptQueueing = (
     221              :     event: CustomEvent<PromptQueueingEventPayload>
     222              :   ) => {
     223          183 :     const payload = event.detail
     224          183 :     const count = sanitizeCount(payload.batchCount)
     225          183 :     queueNotification(
     226              :       toQueueLifecycleNotification('queuedPending', count, payload.requestId)
     227              :     )
     228              :   }
     229              : 
     230         2009 :   const handlePromptQueued = (event: CustomEvent<PromptQueuedEventPayload>) => {
     231          150 :     const payload = event.detail
     232          150 :     const count = sanitizeCount(payload.batchCount)
     233              :     const handled = convertQueuedPendingToQueued(payload.requestId, count)
     234          150 :     if (!handled) {
     235            6 :       queueNotification(
     236              :         toQueueLifecycleNotification('queued', count, payload.requestId)
     237              :       )
     238              :     }
     239              :   }
     240              : 
     241         2009 :   api.addEventListener('promptQueueing', handlePromptQueueing)
     242         2009 :   api.addEventListener('promptQueued', handlePromptQueued)
     243              : 
     244         2009 :   const queueCompletionBatchNotifications = () => {
     245          118 :     const startTs = lastActiveStartTs.value ?? 0
     246          118 :     const finishedTasks = queueStore.historyTasks.filter((task) => {
     247          239 :       const ts = task.executionEndTimestamp
     248          239 :       return typeof ts === 'number' && ts >= startTs
     249              :     })
     250              : 
     251          118 :     if (!finishedTasks.length) {
     252          106 :       return false
     253              :     }
     254              : 
     255           12 :     let completedCount = 0
     256           12 :     let failedCount = 0
     257           12 :     const imagePreviews: string[] = []
     258              : 
     259           12 :     for (const task of finishedTasks) {
     260           12 :       const state = jobStateFromTask(task, false)
     261           12 :       if (state === 'completed') {
     262           11 :         completedCount++
     263           11 :         const preview = task.previewOutput
     264           11 :         if (preview && isImageResult(preview)) {
     265            4 :           imagePreviews.push(resultItemUrlWithTimestamp(preview))
     266              :         }
     267            1 :       } else if (state === 'failed') {
     268            1 :         failedCount++
     269              :       }
     270              :     }
     271              : 
     272           13 :     if (completedCount > 0) {
     273           11 :       queueNotification(toCompletedNotification(completedCount, imagePreviews))
     274              :     }
     275              : 
     276           15 :     if (failedCount > 0) {
     277            1 :       queueNotification(toFailedNotification(failedCount))
     278              :     }
     279              : 
     280          118 :     return completedCount > 0 || failedCount > 0
     281              :   }
     282              : 
     283         2009 :   const scheduleIdleCompletionBatchNotifications = () => {
     284           87 :     clearIdleCompletionHooks()
     285           87 :     const scheduleToken = idleCompletionScheduleToken
     286           87 :     const startTsSnapshot = lastActiveStartTs.value
     287              : 
     288           87 :     const isStillSameIdleWindow = () =>
     289              :       scheduleToken === idleCompletionScheduleToken &&
     290              :       !isQueueActive.value &&
     291              :       lastActiveStartTs.value === startTsSnapshot
     292              : 
     293           87 :     stopIdleHistoryWatch = watch(
     294              :       () => queueStore.historyTasks,
     295              :       () => {
     296           31 :         if (!isStillSameIdleWindow()) {
     297            0 :           clearIdleCompletionHooks()
     298            0 :           return
     299              :         }
     300           31 :         queueCompletionBatchNotifications()
     301           31 :         clearIdleCompletionHooks()
     302              :       }
     303              :     )
     304              : 
     305           87 :     void nextTick(() => {
     306           87 :       if (!isStillSameIdleWindow()) {
     307            0 :         clearIdleCompletionHooks()
     308            0 :         return
     309              :       }
     310              : 
     311              :       const hasShownNotifications = queueCompletionBatchNotifications()
     312           87 :       if (hasShownNotifications) {
     313            8 :         clearIdleCompletionHooks()
     314              :       }
     315              :     })
     316              :   }
     317              : 
     318         2009 :   watch(
     319              :     isQueueActive,
     320              :     (active, prev) => {
     321         2227 :       if (!prev && active) {
     322          132 :         clearIdleCompletionHooks()
     323          132 :         lastActiveStartTs.value = Date.now()
     324          132 :         return
     325              :       }
     326         2227 :       if (prev && !active) {
     327           87 :         scheduleIdleCompletionBatchNotifications()
     328              :       }
     329              :     },
     330              :     { immediate: true }
     331              :   )
     332              : 
     333         2009 :   onUnmounted(() => {
     334           72 :     api.removeEventListener('promptQueueing', handlePromptQueueing)
     335           72 :     api.removeEventListener('promptQueued', handlePromptQueued)
     336           72 :     clearIdleCompletionHooks()
     337           72 :     clearDismissTimer()
     338           72 :     pendingNotifications.value = []
     339           72 :     activeNotification.value = null
     340           72 :     lastActiveStartTs.value = null
     341              :   })
     342              : 
     343              :   const currentNotification = computed(() => activeNotification.value)
     344              : 
     345         2009 :   return {
     346              :     currentNotification
     347              :   }
     348              : }
        

Generated by: LCOV version 2.0-1