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
This commit is contained in:
bymyself
2026-05-03 01:21:44 -07:00
parent 6f9ece1426
commit 16220e7d5e
4 changed files with 29 additions and 22 deletions

View File

@@ -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<DownloadEntry> {
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,

View File

@@ -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<string, DownloadEntry>()
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,

View File

@@ -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<DownloadService> {
export async function createElectronDownloadService(): Promise<PausableDownloadService> {
const api = electronAPI()
const downloadManager = api?.DownloadManager
if (!downloadManager) {

View File

@@ -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<DownloadEntry>
pause(id: string): Promise<void>
resume(id: string): Promise<void>
cancel(id: string): Promise<void>
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<void>
resume(id: string): Promise<void>
}
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