LCOV - code coverage report
Current view: top level - src/stores - electronDownloadStore.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 6.7 % 15 1
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { DownloadStatus } from '@comfyorg/comfyui-electron-types'
       2              : import type { DownloadState } from '@comfyorg/comfyui-electron-types'
       3              : import { defineStore } from 'pinia'
       4              : import { computed, ref } from 'vue'
       5              : 
       6              : import { isDesktop } from '@/platform/distribution/types'
       7              : import { reportError } from '@comfyorg/telemetry/reportError'
       8              : import { electronAPI } from '@/utils/envUtil'
       9              : 
      10              : export interface ElectronDownload extends Pick<
      11              :   DownloadState,
      12              :   'url' | 'filename'
      13              : > {
      14              :   progress?: number
      15              :   receivedBytes?: number
      16              :   savePath?: string
      17              :   status?: DownloadStatus
      18              :   totalBytes?: number
      19              : }
      20              : 
      21              : function normalizeElectronDownloadState({
      22              :   url,
      23              :   filename,
      24              :   state,
      25              :   receivedBytes,
      26              :   totalBytes
      27              : }: DownloadState): ElectronDownload {
      28              :   const download: ElectronDownload = {
      29              :     url,
      30              :     filename,
      31              :     status: state,
      32              :     receivedBytes,
      33              :     totalBytes
      34              :   }
      35              : 
      36              :   return Number.isFinite(receivedBytes) &&
      37              :     receivedBytes >= 0 &&
      38              :     Number.isFinite(totalBytes) &&
      39              :     totalBytes > 0 &&
      40              :     receivedBytes <= totalBytes
      41              :     ? { ...download, progress: receivedBytes / totalBytes }
      42              :     : download
      43              : }
      44              : 
      45              : /** Electron downloads store handler */
      46         2355 : export const useElectronDownloadStore = defineStore('downloads', () => {
      47            0 :   const downloads = ref<ElectronDownload[]>([])
      48            0 :   const DownloadManager = isDesktop ? electronAPI().DownloadManager : undefined
      49            0 :   const progressListeners = new Set<(download: ElectronDownload) => void>()
      50              :   let isProgressListenerInstalled = false
      51              : 
      52            0 :   const findByUrl = (url: string) =>
      53              :     downloads.value.find((download) => url === download.url)
      54              : 
      55              :   function notifyProgressListeners(download: ElectronDownload) {
      56              :     for (const listener of progressListeners) {
      57              :       // Isolated so one throwing subscriber cannot stop the others, or
      58              :       // abandon a replay partway through.
      59              :       try {
      60              :         listener(download)
      61              :       } catch (error) {
      62              :         reportError(error, {
      63              :           surface: 'platform',
      64              :           errorType: 'error_notifying_download_progress_listener'
      65              :         })
      66              :       }
      67              :     }
      68              :   }
      69              : 
      70              :   function applyProgress(data: ElectronDownload) {
      71              :     if (!findByUrl(data.url)) {
      72              :       downloads.value.push(data)
      73              :     }
      74              : 
      75              :     const download = findByUrl(data.url)
      76              : 
      77              :     if (download) {
      78              :       download.progress = data.progress
      79              :       download.receivedBytes = undefined
      80              :       download.totalBytes = undefined
      81              :       download.status = data.status
      82              :       download.filename = data.filename
      83              :       download.savePath = data.savePath
      84              :       notifyProgressListeners(download)
      85              :     }
      86              :   }
      87              : 
      88              :   /** `defer` may hold an event back; returning true means it took ownership. */
      89              :   function installProgressListener(
      90              :     defer?: (download: ElectronDownload) => boolean
      91              :   ) {
      92              :     if (!DownloadManager || isProgressListenerInstalled) return
      93              : 
      94              :     isProgressListenerInstalled = true
      95              :     DownloadManager.onDownloadProgress((data) => {
      96              :       if (defer?.(data)) return
      97              :       applyProgress(data)
      98              :     })
      99              :   }
     100              : 
     101            0 :   async function initialize() {
     102              :     if (!isDesktop || !DownloadManager) return
     103              : 
     104              :     // Listen immediately so no live event is missed, but hold them back until
     105              :     // the snapshot has been applied: the snapshot describes an older moment,
     106              :     // so replaying afterwards is what keeps a download from moving backwards.
     107              :     const live = new Map<string, ElectronDownload>()
     108              :     let restored = false
     109              :     installProgressListener((download) => {
     110              :       if (restored) return false
     111              :       live.set(download.url, download)
     112              :       return true
     113              :     })
     114              : 
     115              :     try {
     116              :       const allDownloads = await DownloadManager.getAllDownloads()
     117              : 
     118              :       for (const download of allDownloads) {
     119              :         const normalizedDownload = normalizeElectronDownloadState(download)
     120              :         const existing = findByUrl(normalizedDownload.url)
     121              :         if (existing) {
     122              :           Object.assign(existing, normalizedDownload)
     123              :         } else {
     124              :           downloads.value.push(normalizedDownload)
     125              :         }
     126              :       }
     127              :     } catch {
     128              :       // A missing snapshot must not keep live progress from being observed.
     129              :     }
     130              : 
     131              :     restored = true
     132              :     for (const download of live.values()) applyProgress(download)
     133              :     live.clear()
     134              :   }
     135              : 
     136            0 :   function subscribeToDownloadProgress(
     137              :     listener: (download: ElectronDownload) => void
     138              :   ) {
     139            0 :     progressListeners.add(listener)
     140              : 
     141            0 :     return () => progressListeners.delete(listener)
     142              :   }
     143              : 
     144            0 :   void initialize()
     145              : 
     146            0 :   const start = ({
     147              :     url,
     148              :     savePath,
     149              :     filename
     150              :   }: {
     151              :     url: string
     152              :     savePath: string
     153              :     filename: string
     154              :   }) => DownloadManager!.startDownload(url, savePath, filename)
     155            0 :   const pause = (url: string) => DownloadManager!.pauseDownload(url)
     156            0 :   const resume = (url: string) => DownloadManager!.resumeDownload(url)
     157            0 :   const cancel = (url: string) => DownloadManager!.cancelDownload(url)
     158              : 
     159            0 :   return {
     160              :     downloads,
     161              :     start,
     162              :     pause,
     163              :     resume,
     164              :     cancel,
     165              :     findByUrl,
     166              :     initialize,
     167              :     subscribeToDownloadProgress,
     168              :     inProgressDownloads: computed(() =>
     169              :       downloads.value.filter(
     170              :         ({ status }) => status !== DownloadStatus.COMPLETED
     171              :       )
     172              :     )
     173              :   }
     174              : })
        

Generated by: LCOV version 2.0-1