mirror of
https://github.com/immich-app/immich
synced 2025-11-07 17:27:20 +00:00
refactor: download manager (#17935)
This commit is contained in:
parent
f64e6f5dc3
commit
895b2bf5cd
5 changed files with 17 additions and 23 deletions
45
web/src/lib/managers/download-manager.svelte.ts
Normal file
45
web/src/lib/managers/download-manager.svelte.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
export interface DownloadProgress {
|
||||
progress: number;
|
||||
total: number;
|
||||
percentage: number;
|
||||
abort: AbortController | null;
|
||||
}
|
||||
|
||||
class DownloadManager {
|
||||
assets = $state<Record<string, DownloadProgress>>({});
|
||||
|
||||
isDownloading = $derived(Object.keys(this.assets).length > 0);
|
||||
|
||||
#update(key: string, value: Partial<DownloadProgress> | null) {
|
||||
if (value === null) {
|
||||
delete this.assets[key];
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.assets[key]) {
|
||||
this.assets[key] = { progress: 0, total: 0, percentage: 0, abort: null };
|
||||
}
|
||||
|
||||
const item = this.assets[key];
|
||||
Object.assign(item, value);
|
||||
item.percentage = Math.min(Math.floor((item.progress / item.total) * 100), 100);
|
||||
}
|
||||
|
||||
add(key: string, total: number, abort?: AbortController) {
|
||||
this.#update(key, { total, abort });
|
||||
}
|
||||
|
||||
clear(key: string) {
|
||||
this.#update(key, null);
|
||||
}
|
||||
|
||||
update(key: string, progress: number, total?: number) {
|
||||
const download: Partial<DownloadProgress> = { progress };
|
||||
if (total !== undefined) {
|
||||
download.total = total;
|
||||
}
|
||||
this.#update(key, download);
|
||||
}
|
||||
}
|
||||
|
||||
export const downloadManager = new DownloadManager();
|
||||
Loading…
Add table
Add a link
Reference in a new issue