diff options
| author | Fuwn <[email protected]> | 2025-06-12 00:54:50 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-06-12 00:54:50 -0700 |
| commit | 3e9d826755d9d2b7793daa67314999a89ad0787e (patch) | |
| tree | 4757a943d3ba8d2b606333267944115c0eed5149 /src | |
| parent | style: Reformat using latest formatting (diff) | |
| download | due.moe-3e9d826755d9d2b7793daa67314999a89ad0787e.tar.xz due.moe-3e9d826755d9d2b7793daa67314999a89ad0787e.zip | |
refactor(stateBin): Simplify interface
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/List/Anime/CleanAnimeList.svelte | 6 | ||||
| -rw-r--r-- | src/lib/List/Manga/CleanMangaList.svelte | 4 | ||||
| -rw-r--r-- | src/stores/stateBin.ts | 73 |
3 files changed, 34 insertions, 49 deletions
diff --git a/src/lib/List/Anime/CleanAnimeList.svelte b/src/lib/List/Anime/CleanAnimeList.svelte index d31cad55..df5a5834 100644 --- a/src/lib/List/Anime/CleanAnimeList.svelte +++ b/src/lib/List/Anime/CleanAnimeList.svelte @@ -66,17 +66,17 @@ ? 'Completed' : 'Due'; const filterKey = `${filterKind}AnimeListFilter`; - let selectedList = stateBin.get()[filterKey] || 'All'; + let selectedList = $stateBin[filterKey] || 'All'; onMount(() => { if (browser) { - const storedValue = stateBin.get()[filterKey]; + const storedValue = $stateBin[filterKey]; if (typeof storedValue === 'string') selectedList = storedValue; } }); - $: stateBin.setKey(filterKey, selectedList); + $: $stateBin[filterKey] = selectedList; $: filteredMedia = selectedList === 'All' || !$settings.displayMediaListFilter diff --git a/src/lib/List/Manga/CleanMangaList.svelte b/src/lib/List/Manga/CleanMangaList.svelte index d5750e5a..f49d3266 100644 --- a/src/lib/List/Manga/CleanMangaList.svelte +++ b/src/lib/List/Manga/CleanMangaList.svelte @@ -50,11 +50,11 @@ ); const filterKind = due ? 'due' : 'completed'; const filterKey = `${filterKind}MangaListFilter`; - let selectedList = stateBin.get()[filterKey] || 'All'; + let selectedList = $stateBin[filterKey] || 'All'; onMount(() => { if (browser) { - const storedValue = stateBin.get()[filterKey]; + const storedValue = $stateBin[filterKey]; if (typeof storedValue === 'string') selectedList = storedValue; } diff --git a/src/stores/stateBin.ts b/src/stores/stateBin.ts index 6c664f71..74ea510f 100644 --- a/src/stores/stateBin.ts +++ b/src/stores/stateBin.ts @@ -4,63 +4,48 @@ import { writable, get, type Writable } from 'svelte/store'; type StateBin = Record<string, unknown>; const STORAGE_KEY = 'stateBin'; -const defaultState: StateBin = {}; +const initialState = browser ? JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '{}') : {}; +const baseStore = writable<StateBin>(initialState); -function createStateBin() { - const initialState: StateBin = browser - ? JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '{}') - : defaultState; - const store: Writable<StateBin> = writable(initialState); +if (browser) + baseStore.subscribe((val) => { + localStorage.setItem(STORAGE_KEY, JSON.stringify(val)); + }); - const writeToStorage = (val: StateBin) => { - if (browser) - try { - localStorage.setItem(STORAGE_KEY, JSON.stringify(val)); - } catch (error) { - console.error('Failed to write stateBin:', error); - } - }; +const createProxyStore = (store: Writable<StateBin>) => { + return new Proxy(store, { + get(target, prop: string) { + if (prop in target) return (target as any)[prop]; - return { - subscribe: store.subscribe, + const derivedKey = writable(get(store)[prop]); - set: (value) => { - writeToStorage(value); - store.set(value); - }, + derivedKey.subscribe((value) => { + const state = get(store); + const updatedState = { ...state }; - update: (updateFunction) => { - const updatedStore = updateFunction(get(store)); + if (value === null || value === undefined) delete updatedState[prop]; + else updatedState[prop] = value; - writeToStorage(updatedStore); - store.set(updatedStore); - }, + store.set(updatedState); + }); - reset: () => { - writeToStorage(defaultState); - store.set(defaultState); + return derivedKey; }, - get: () => get(store), + set(_, prop: string, value) { + const state = get(store); + const updatedState = { ...state }; - setKey: (key: string, value: unknown) => { - const currentStore = get(store); - const updatedStore = { ...currentStore, [key]: value }; - - writeToStorage(updatedStore); - store.set(updatedStore); - }, + if (value === null || value === undefined) delete updatedState[prop]; + else updatedState[prop] = value; - removeKey: (key: string) => { - const currentStore = get(store); - const { [key]: _, ...rest } = currentStore; + store.set(updatedState); - writeToStorage(rest); - store.set(rest); + return true; } - }; -} + }); +}; -const stateBin = createStateBin(); +const stateBin = createProxyStore(baseStore); export default stateBin; |