guard around potential crashes
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { browser } from '$app/environment';
|
||||
import { SvelteSet, SvelteMap } from 'svelte/reactivity';
|
||||
import { readStored, writeStored } from './utils.ts';
|
||||
import type { StreamSummary, PartialDownload } from './types.ts';
|
||||
|
||||
const STORAGE_KEY = 'cachedStreams';
|
||||
@@ -16,23 +17,21 @@ const CHUNK_SIZE = 8 * 1024 * 1024;
|
||||
// as the intent is to catch socket breakage during cellular network changes for example
|
||||
const CHUNK_TIMEOUT = 60_000;
|
||||
|
||||
export const cached = new SvelteSet<string>(
|
||||
browser ? JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]') : []
|
||||
);
|
||||
export const cached = new SvelteSet<string>(readStored<string[]>(STORAGE_KEY, []));
|
||||
export const downloading = new SvelteMap<string, number>();
|
||||
|
||||
// bytes already written to OPFS for each incomplete download
|
||||
export const partials = new SvelteMap<string, PartialDownload>(
|
||||
browser ? Object.entries(JSON.parse(localStorage.getItem(PARTIALS_KEY) || '{}')) : []
|
||||
Object.entries(readStored<Record<string, PartialDownload>>(PARTIALS_KEY, {}))
|
||||
);
|
||||
|
||||
if (browser) {
|
||||
$effect.root(() => {
|
||||
$effect(() => {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify([...cached]));
|
||||
writeStored(STORAGE_KEY, [...cached]);
|
||||
});
|
||||
$effect(() => {
|
||||
localStorage.setItem(PARTIALS_KEY, JSON.stringify(Object.fromEntries(partials)));
|
||||
writeStored(PARTIALS_KEY, Object.fromEntries(partials));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user