refactor(web): simplify download manager, use SvelteMap

This commit is contained in:
Diogo Correia 2026-07-19 13:50:07 +01:00
parent b4302ee99c
commit 0dd39fbb0e
No known key found for this signature in database
GPG key ID: 12B4F3AC9C065D08
2 changed files with 11 additions and 21 deletions

View file

@ -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 });
}
}
}

View file

@ -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">