LCOV - code coverage report
Current view: top level - src/components/ui/toast - toastStore.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 84.2 % 38 32
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { defineStore } from 'pinia'
       2              : import { ref } from 'vue'
       3              : 
       4              : import { createToastId } from '@/types/toastId'
       5              : import type { ToastId } from '@/types/toastId'
       6              : import type { ToastAction, ToastOptions } from '@/types/toastOptions'
       7              : 
       8              : type ToastKind = 'error' | 'info' | 'loading' | 'success' | 'warning'
       9              : 
      10              : export interface Toast {
      11              :   closable: boolean
      12              :   duration: number
      13              :   id: ToastId
      14              :   instance: symbol
      15              :   kind: ToastKind
      16              :   title: string
      17              :   actions: ToastAction[]
      18              :   description?: string
      19              : }
      20              : 
      21         2356 : const isAction = (candidate: unknown): candidate is ToastAction =>
      22              :   typeof candidate === 'object' &&
      23              :   candidate !== null &&
      24              :   'label' in candidate &&
      25              :   typeof candidate.label === 'string' &&
      26              :   'onClick' in candidate &&
      27              :   typeof candidate.onClick === 'function'
      28              : 
      29          168 : function toActions(options: ToastOptions): ToastAction[] {
      30          168 :   const supplied: unknown[] = [options.action]
      31          168 :   if (Array.isArray(options.actions)) supplied.push(...options.actions)
      32          168 :   return supplied.filter(isAction)
      33              : }
      34              : 
      35         2356 : export const useToast = defineStore('toast', () => {
      36         2355 :   const toasts = ref<Toast[]>([])
      37         2355 :   const held = ref(false)
      38         2355 :   const closedByUser = new Set<ToastId>()
      39              : 
      40          168 :   function add(kind: ToastKind, title: string, options: ToastOptions = {}) {
      41          168 :     const id = options.id ?? createToastId()
      42          168 :     toasts.value = [
      43              :       ...toasts.value.filter((toast) => toast.id !== id),
      44              :       {
      45              :         actions: toActions(options),
      46              :         closable: options.closable ?? true,
      47              :         description: options.description,
      48              :         duration: options.duration ?? Number.POSITIVE_INFINITY,
      49              :         id,
      50              :         instance: Symbol(),
      51              :         kind,
      52              :         title
      53              :       }
      54              :     ]
      55          168 :     return id
      56              :   }
      57              : 
      58           53 :   function success(title: string, options?: ToastOptions) {
      59           53 :     return add('success', title, options)
      60              :   }
      61              : 
      62           52 :   function error(title: string, options?: ToastOptions) {
      63           52 :     return add('error', title, options)
      64              :   }
      65              : 
      66           46 :   function info(title: string, options?: ToastOptions) {
      67           46 :     return add('info', title, options)
      68              :   }
      69              : 
      70           14 :   function warning(title: string, options?: ToastOptions) {
      71           14 :     return add('warning', title, options)
      72              :   }
      73              : 
      74            3 :   function loading(title: string, options?: ToastOptions) {
      75            3 :     return add('loading', title, options)
      76              :   }
      77              : 
      78              :   /** Changes a shown toast's text in place, without showing it again. */
      79            0 :   function update(
      80              :     id: ToastId,
      81              :     changes: Partial<Pick<Toast, 'title' | 'description'>>
      82              :   ) {
      83            0 :     toasts.value = toasts.value.map((toast) =>
      84              :       toast.id === id ? { ...toast, ...changes } : toast
      85              :     )
      86              :   }
      87              : 
      88           70 :   function dismiss(id: ToastId): boolean {
      89           70 :     const remaining = toasts.value.filter((toast) => toast.id !== id)
      90           70 :     const removed = remaining.length < toasts.value.length
      91           70 :     toasts.value = remaining
      92           70 :     return removed
      93              :   }
      94              : 
      95           14 :   function close(id: ToastId): boolean {
      96           14 :     closedByUser.add(id)
      97           14 :     return dismiss(id)
      98              :   }
      99              : 
     100            0 :   function takeClosedByUser(id: ToastId): boolean {
     101            0 :     return closedByUser.delete(id)
     102              :   }
     103              : 
     104            0 :   function dismissAll() {
     105            0 :     toasts.value = []
     106              :   }
     107              : 
     108         2355 :   return {
     109              :     held,
     110              :     toasts,
     111              :     success,
     112              :     error,
     113              :     info,
     114              :     warning,
     115              :     loading,
     116              :     update,
     117              :     dismiss,
     118              :     close,
     119              :     takeClosedByUser,
     120              :     dismissAll
     121              :   }
     122              : })
        

Generated by: LCOV version 2.0-1