mirror of
https://github.com/immich-app/immich
synced 2026-08-22 13:13:05 +00:00
refactor(web): simplify download manager, use SvelteMap
This commit is contained in:
parent
b4302ee99c
commit
0dd39fbb0e
2 changed files with 11 additions and 21 deletions
|
|
@ -1,3 +1,5 @@
|
|||
import { SvelteMap } from 'svelte/reactivity';
|
||||
|
||||
export interface DownloadState {
|
||||
url: string;
|
||||
payload: unknown;
|
||||
|
|
@ -6,34 +8,23 @@ export interface DownloadState {
|
|||
}
|
||||
|
||||
class DownloadManager {
|
||||
assets = $state<Record<string, DownloadState>>({});
|
||||
assets = new SvelteMap<string, DownloadState>();
|
||||
|
||||
isDownloading = $derived(Object.keys(this.assets).length > 0);
|
||||
|
||||
#update(key: string, value: Partial<DownloadState> | null) {
|
||||
if (value === null) {
|
||||
delete this.assets[key];
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.assets[key]) {
|
||||
this.assets[key] = { url: '', payload: undefined, total: 0, downloaded: false };
|
||||
}
|
||||
|
||||
const item = this.assets[key];
|
||||
Object.assign(item, value);
|
||||
}
|
||||
isDownloading = $derived(this.assets.size > 0);
|
||||
|
||||
add(key: string, url: string, payload: unknown, total: number) {
|
||||
this.#update(key, { url, payload, total });
|
||||
this.assets.set(key, { url, payload, total, downloaded: false });
|
||||
}
|
||||
|
||||
clearAll() {
|
||||
this.assets = {};
|
||||
this.assets.clear();
|
||||
}
|
||||
|
||||
markDownloaded(key: string) {
|
||||
this.#update(key, { downloaded: true });
|
||||
const state = this.assets.get(key);
|
||||
if (state) {
|
||||
this.assets.set(key, { ...state, downloaded: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,8 +28,7 @@
|
|||
<CloseButton class="w-8" size="small" onclick={closePanel} />
|
||||
</div>
|
||||
<div class="my-2 mb-2 flex max-h-50 flex-col overflow-y-auto text-sm">
|
||||
{#each Object.keys(downloadManager.assets) as downloadKey (downloadKey)}
|
||||
{@const download = downloadManager.assets[downloadKey]}
|
||||
{#each downloadManager.assets as [downloadKey, download] (downloadKey)}
|
||||
<div class="mb-2 flex place-items-center gap-2" transition:slide>
|
||||
<div class="min-w-0 grow">
|
||||
<div class="flex place-items-center justify-between gap-2 text-xs font-medium">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue