From 16220e7d5ea5d3ea81ded03b76fd2b7b3976a990 Mon Sep 17 00:00:00 2001 From: bymyself Date: Sun, 3 May 2026 01:21:44 -0700 Subject: [PATCH] refactor: discriminate DownloadService union by supportsPauseResume Split DownloadService into PausableDownloadService and NonPausableDownloadService discriminated by supportsPauseResume. Removes pause/resume stubs from cloud and browser providers; the compiler now enforces narrowing before pause/resume calls. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/11437#discussion_r3113480116 --- .../providers/createBrowserDownloadService.ts | 10 +++----- .../providers/createCloudDownloadService.ts | 10 +++----- .../createElectronDownloadService.ts | 6 ++--- src/platform/downloads/types.ts | 25 +++++++++++++++---- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/platform/downloads/providers/createBrowserDownloadService.ts b/src/platform/downloads/providers/createBrowserDownloadService.ts index 1b1e01e1cb..5d2d1d1203 100644 --- a/src/platform/downloads/providers/createBrowserDownloadService.ts +++ b/src/platform/downloads/providers/createBrowserDownloadService.ts @@ -1,10 +1,10 @@ import type { DownloadEntry, - DownloadService, - DownloadStartParams + DownloadStartParams, + NonPausableDownloadService } from '../types' -export function createBrowserDownloadService(): DownloadService { +export function createBrowserDownloadService(): NonPausableDownloadService { async function start(params: DownloadStartParams): Promise { const anchorElement = document.createElement('a') anchorElement.href = params.url @@ -23,8 +23,6 @@ export function createBrowserDownloadService(): DownloadService { } } - async function pause() {} - async function resume() {} async function cancel() {} function getAll(): DownloadEntry[] { @@ -42,8 +40,6 @@ export function createBrowserDownloadService(): DownloadService { return { supportsPauseResume: false, start, - pause, - resume, cancel, getAll, getById, diff --git a/src/platform/downloads/providers/createCloudDownloadService.ts b/src/platform/downloads/providers/createCloudDownloadService.ts index a4e3636f5c..34ed7c4b30 100644 --- a/src/platform/downloads/providers/createCloudDownloadService.ts +++ b/src/platform/downloads/providers/createCloudDownloadService.ts @@ -1,10 +1,10 @@ import type { DownloadEntry, - DownloadService, - DownloadStartParams + DownloadStartParams, + NonPausableDownloadService } from '../types' -export function createCloudDownloadService(): DownloadService { +export function createCloudDownloadService(): NonPausableDownloadService { const entries = new Map() const progressListeners = new Map< string, @@ -33,8 +33,6 @@ export function createCloudDownloadService(): DownloadService { return entry } - async function pause() {} - async function resume() {} async function cancel() {} function getAll(): DownloadEntry[] { @@ -61,8 +59,6 @@ export function createCloudDownloadService(): DownloadService { return { supportsPauseResume: false, start, - pause, - resume, cancel, getAll, getById, diff --git a/src/platform/downloads/providers/createElectronDownloadService.ts b/src/platform/downloads/providers/createElectronDownloadService.ts index b6976e53b6..e67abb2434 100644 --- a/src/platform/downloads/providers/createElectronDownloadService.ts +++ b/src/platform/downloads/providers/createElectronDownloadService.ts @@ -2,9 +2,9 @@ import type { DownloadProgressUpdate } from '@comfyorg/comfyui-electron-types' import type { DownloadEntry, - DownloadService, DownloadStartParams, - DownloadStatus + DownloadStatus, + PausableDownloadService } from '../types' import { electronAPI } from '@/utils/envUtil' @@ -29,7 +29,7 @@ function toDownloadStatus( return isDownloadStatus(value) ? value : fallback } -export async function createElectronDownloadService(): Promise { +export async function createElectronDownloadService(): Promise { const api = electronAPI() const downloadManager = api?.DownloadManager if (!downloadManager) { diff --git a/src/platform/downloads/types.ts b/src/platform/downloads/types.ts index 56f42dd36d..a28c3a119a 100644 --- a/src/platform/downloads/types.ts +++ b/src/platform/downloads/types.ts @@ -34,12 +34,10 @@ export interface DownloadStartParams { tags?: string[] } -export interface DownloadService { +interface BaseDownloadService { /** Resolves once the download is accepted, not when it completes. */ start(params: DownloadStartParams): Promise - pause(id: string): Promise - resume(id: string): Promise cancel(id: string): Promise getAll(): DownloadEntry[] @@ -47,6 +45,23 @@ export interface DownloadService { /** Returns an unsubscribe function. */ onProgress(id: string, cb: (entry: DownloadEntry) => void): () => void - - readonly supportsPauseResume: boolean } + +export interface PausableDownloadService extends BaseDownloadService { + readonly supportsPauseResume: true + pause(id: string): Promise + resume(id: string): Promise +} + +export interface NonPausableDownloadService extends BaseDownloadService { + readonly supportsPauseResume: false +} + +/** + * Discriminated union by {@link supportsPauseResume}. Callers must narrow + * on the discriminant before calling {@link PausableDownloadService.pause} + * or {@link PausableDownloadService.resume}; the compiler enforces this. + */ +export type DownloadService = + | PausableDownloadService + | NonPausableDownloadService