aboutsummaryrefslogtreecommitdiff
path: root/src/stores
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-09-03 18:36:53 -0700
committerFuwn <[email protected]>2023-09-03 18:36:53 -0700
commit92293c66156e49f801fed6e8cc0996fd88f5f33b (patch)
tree155c66f6103987a88ddcde2af46ee7c79cc47177 /src/stores
parentfix(media): cache minutes from settings (diff)
downloaddue.moe-92293c66156e49f801fed6e8cc0996fd88f5f33b.tar.xz
due.moe-92293c66156e49f801fed6e8cc0996fd88f5f33b.zip
feat(settings): move last prune times to store
Diffstat (limited to 'src/stores')
-rw-r--r--src/stores/animeLastPrune.ts14
-rw-r--r--src/stores/chaptersLastPrune.ts14
-rw-r--r--src/stores/lastPruneTimes.ts62
-rw-r--r--src/stores/mangaLastPrune.ts14
4 files changed, 62 insertions, 42 deletions
diff --git a/src/stores/animeLastPrune.ts b/src/stores/animeLastPrune.ts
deleted file mode 100644
index f2e747d1..00000000
--- a/src/stores/animeLastPrune.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import { browser } from '$app/environment';
-import { writable } from 'svelte/store';
-
-const animeLastPrune = writable<string>(
- browser ? localStorage.getItem('animeLastPrune') ?? '' : ''
-);
-
-animeLastPrune.subscribe((value) => {
- if (browser) {
- localStorage.setItem('animeLastPrune', value);
- }
-});
-
-export default animeLastPrune;
diff --git a/src/stores/chaptersLastPrune.ts b/src/stores/chaptersLastPrune.ts
deleted file mode 100644
index bccee23e..00000000
--- a/src/stores/chaptersLastPrune.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import { browser } from '$app/environment';
-import { writable } from 'svelte/store';
-
-const chaptersLastPrune = writable<string>(
- browser ? localStorage.getItem('chaptersLastPrune') ?? '' : ''
-);
-
-chaptersLastPrune.subscribe((value) => {
- if (browser) {
- localStorage.setItem('chaptersLastPrune', value);
- }
-});
-
-export default chaptersLastPrune;
diff --git a/src/stores/lastPruneTimes.ts b/src/stores/lastPruneTimes.ts
new file mode 100644
index 00000000..ef71053b
--- /dev/null
+++ b/src/stores/lastPruneTimes.ts
@@ -0,0 +1,62 @@
+import { browser } from '$app/environment';
+import { writable } from 'svelte/store';
+
+interface LastPruneTimes {
+ anime: number;
+ chapters: number;
+ manga: number;
+}
+
+const defaultTimes: LastPruneTimes = {
+ anime: 1,
+ chapters: 1,
+ manga: 1
+};
+
+const createStore = () => {
+ const { subscribe, set, update } = writable<LastPruneTimes>(
+ JSON.parse(
+ browser
+ ? localStorage.getItem('lastPruneTimes') ?? JSON.stringify(defaultTimes)
+ : JSON.stringify(defaultTimes)
+ )
+ );
+ let state: LastPruneTimes;
+
+ subscribe((value) => (state = value));
+
+ return {
+ subscribe,
+ set,
+ update,
+ reset: () => set(defaultTimes),
+ get: () => {
+ const keys = Object.keys(defaultTimes);
+ const lastPruneTimesKeys = Object.keys(state);
+
+ if (keys.length !== lastPruneTimesKeys.length) {
+ return defaultTimes;
+ }
+
+ for (const key of keys) {
+ if (!lastPruneTimesKeys.includes(key)) {
+ return defaultTimes;
+ }
+ }
+
+ return state;
+ },
+ setKey: (key: keyof LastPruneTimes, value: unknown) =>
+ update((lastPruneTimes) => ({ ...lastPruneTimes, [key]: value }))
+ };
+};
+
+const lastPruneTimes = createStore();
+
+lastPruneTimes.subscribe((value) => {
+ if (browser) {
+ localStorage.setItem('lastPruneTimes', JSON.stringify(value));
+ }
+});
+
+export default lastPruneTimes;
diff --git a/src/stores/mangaLastPrune.ts b/src/stores/mangaLastPrune.ts
deleted file mode 100644
index f6b40b02..00000000
--- a/src/stores/mangaLastPrune.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import { browser } from '$app/environment';
-import { writable } from 'svelte/store';
-
-const mangaLastPrune = writable<string>(
- browser ? localStorage.getItem('mangaLastPrune') ?? '' : ''
-);
-
-mangaLastPrune.subscribe((value) => {
- if (browser) {
- localStorage.setItem('mangaLastPrune', value);
- }
-});
-
-export default mangaLastPrune;