aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-06-11 23:32:39 -0700
committerFuwn <[email protected]>2025-06-11 23:32:39 -0700
commitb288ea46b769f407b25e18586dbd2fb36626cfb3 (patch)
tree0747708aa27eafacdf93427f8eb2b7ad78ae428d
parentfix(stores): Move anime and manga from localStorage to IndexedDB (diff)
downloaddue.moe-b288ea46b769f407b25e18586dbd2fb36626cfb3.tar.xz
due.moe-b288ea46b769f407b25e18586dbd2fb36626cfb3.zip
refactor(stores): Generic persistent storage facility
-rw-r--r--src/lib/Utility/persistentStore.ts19
-rw-r--r--src/stores/anime.ts16
-rw-r--r--src/stores/manga.ts16
3 files changed, 23 insertions, 28 deletions
diff --git a/src/lib/Utility/persistentStore.ts b/src/lib/Utility/persistentStore.ts
new file mode 100644
index 00000000..b08c825c
--- /dev/null
+++ b/src/lib/Utility/persistentStore.ts
@@ -0,0 +1,19 @@
+import { writable, type Writable } from 'svelte/store';
+import { browser } from '$app/environment';
+
+export const persistentStore = <T>(key: string, initial: T): Writable<T> => {
+ const store = writable<T>(initial);
+
+ if (browser)
+ import('localforage').then((localforage) => {
+ localforage.default.getItem<T>(key).then((value) => {
+ if (value !== null) store.set(value);
+ });
+
+ store.subscribe((value) => {
+ localforage.default.setItem(key, value);
+ });
+ });
+
+ return store;
+};
diff --git a/src/stores/anime.ts b/src/stores/anime.ts
index dd85588d..116139a3 100644
--- a/src/stores/anime.ts
+++ b/src/stores/anime.ts
@@ -1,17 +1,5 @@
-import { browser } from '$app/environment';
-import { writable } from 'svelte/store';
-import localforage from 'localforage';
+import { persistentStore } from '$lib/Utility/persistentStore';
-const anime = writable<string>('');
-
-if (browser) {
- localforage.getItem<string>('anime').then((value) => {
- if (value) anime.set(value);
- });
-
- anime.subscribe((value) => {
- localforage.setItem('anime', value);
- });
-}
+const anime = persistentStore<string>('anime', '');
export default anime;
diff --git a/src/stores/manga.ts b/src/stores/manga.ts
index ff61d335..84b19665 100644
--- a/src/stores/manga.ts
+++ b/src/stores/manga.ts
@@ -1,17 +1,5 @@
-import { browser } from '$app/environment';
-import { writable } from 'svelte/store';
-import localforage from 'localforage';
+import { persistentStore } from '$lib/Utility/persistentStore';
-const manga = writable<string>('');
-
-if (browser) {
- localforage.getItem<string>('manga').then((value) => {
- if (value) manga.set(value);
- });
-
- manga.subscribe((value) => {
- localforage.setItem('manga', value);
- });
-}
+const manga = persistentStore<string>('manga', '');
export default manga;