From 669115227593f51a49415da7587481ccc63c48b0 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sat, 28 Mar 2026 05:49:07 +0000 Subject: fix(cache): respect AniList media list recache windows --- src/lib/Data/AniList/cacheHydration.ts | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/lib/Data/AniList/cacheHydration.ts (limited to 'src/lib/Data/AniList/cacheHydration.ts') 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>(); + +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(kind), + localforage.getItem("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; +}; -- cgit v1.2.3