aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Data/AniList/cacheHydration.ts
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-03-28 05:49:07 +0000
committerFuwn <[email protected]>2026-03-28 05:49:07 +0000
commit669115227593f51a49415da7587481ccc63c48b0 (patch)
tree5fa7b93d7b7fabe9a3adc9c3bad8603e6bdb9070 /src/lib/Data/AniList/cacheHydration.ts
parentfeat(manga): allow forcing automatic refresh (diff)
downloaddue.moe-669115227593f51a49415da7587481ccc63c48b0.tar.xz
due.moe-669115227593f51a49415da7587481ccc63c48b0.zip
fix(cache): respect AniList media list recache windows
Diffstat (limited to 'src/lib/Data/AniList/cacheHydration.ts')
-rw-r--r--src/lib/Data/AniList/cacheHydration.ts48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/lib/Data/AniList/cacheHydration.ts b/src/lib/Data/AniList/cacheHydration.ts
new file mode 100644
index 00000000..434d2a82
--- /dev/null
+++ b/src/lib/Data/AniList/cacheHydration.ts
@@ -0,0 +1,48 @@
+import { browser } from "$app/environment";
+import anime from "$stores/anime";
+import lastPruneTimes from "$stores/lastPruneTimes";
+import manga from "$stores/manga";
+import localforage from "localforage";
+
+type MediaCacheKind = "anime" | "manga";
+
+interface StoredLastPruneTimes {
+ anime: number;
+ chapters: number;
+ manga: number;
+}
+
+const hydration = new Map<MediaCacheKind, Promise<void>>();
+
+const isStoredLastPruneTimes = (
+ value: unknown,
+): value is StoredLastPruneTimes =>
+ typeof value === "object" &&
+ value !== null &&
+ typeof (value as StoredLastPruneTimes).anime === "number" &&
+ typeof (value as StoredLastPruneTimes).chapters === "number" &&
+ typeof (value as StoredLastPruneTimes).manga === "number";
+
+export const hydrateMediaListCache = (kind: MediaCacheKind) => {
+ if (!browser) return Promise.resolve();
+
+ const existing = hydration.get(kind);
+
+ if (existing) return existing;
+
+ const promise = (async () => {
+ const [cache, pruneTimes] = await Promise.all([
+ localforage.getItem<string>(kind),
+ localforage.getItem<StoredLastPruneTimes>("lastPruneTimes"),
+ ]);
+
+ if (typeof cache === "string" && cache.length)
+ (kind === "anime" ? anime : manga).set(cache);
+
+ if (isStoredLastPruneTimes(pruneTimes)) lastPruneTimes.set(pruneTimes);
+ })();
+
+ hydration.set(kind, promise);
+
+ return promise;
+};