diff options
| author | Fuwn <[email protected]> | 2026-03-28 06:45:59 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-03-28 06:45:59 +0000 |
| commit | 8eb0d9eb20976135c8241fb0104a33467d730f42 (patch) | |
| tree | da7ef8568f7ef6ed5ba983e7d889b6b89582e79f /src/lib | |
| parent | fix(anilist): avoid leaking settings subscriptions (diff) | |
| download | due.moe-8eb0d9eb20976135c8241fb0104a33467d730f42.tar.xz due.moe-8eb0d9eb20976135c8241fb0104a33467d730f42.zip | |
fix(anilist): fall back to persisted media list cache
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/Data/AniList/media.ts | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/lib/Data/AniList/media.ts b/src/lib/Data/AniList/media.ts index 16267df3..55263207 100644 --- a/src/lib/Data/AniList/media.ts +++ b/src/lib/Data/AniList/media.ts @@ -1,3 +1,4 @@ +import { browser } from "$app/environment"; import type { AniListAuthorisation } from "$lib/Data/AniList/identity"; import { parseJsonStringOrDefault } from "$lib/Effect/json"; import type { UserIdentity } from "./identity"; @@ -7,6 +8,7 @@ import settings from "$stores/settings"; import lastPruneTimes from "$stores/lastPruneTimes"; import { options as getOptions, type Options } from "$lib/Notification/options"; import type { PrequelRelation, PrequelRelations } from "./prequels"; +import localforage from "localforage"; export enum Type { Anime, @@ -248,6 +250,52 @@ const collectionKey = ( includeRelations: options.includeRelations, }); +const cacheStorageKey = (type: Type) => + type === Type.Anime ? "anime" : "manga"; + +const hydrateCollectionInputs = async ( + type: Type, + mediaCache: string | undefined, + currentLastPruneAt: string | number, +) => { + if (!browser) return { mediaCache, currentLastPruneAt }; + + const shouldReadStoredCache = mediaCache === undefined || mediaCache === ""; + const shouldReadStoredPruneTime = + String(currentLastPruneAt) === "" || Number(currentLastPruneAt) <= 1; + + if (!shouldReadStoredCache && !shouldReadStoredPruneTime) + return { mediaCache, currentLastPruneAt }; + + const [storedCache, storedPruneTimes] = await Promise.all([ + shouldReadStoredCache + ? localforage.getItem<string>(cacheStorageKey(type)) + : Promise.resolve(null), + shouldReadStoredPruneTime + ? localforage.getItem<{ + anime?: number; + manga?: number; + chapters?: number; + }>("lastPruneTimes") + : Promise.resolve(null), + ]); + + if ( + shouldReadStoredCache && + typeof storedCache === "string" && + storedCache.length + ) + mediaCache = storedCache; + + const storedPruneAt = + type === Type.Anime ? storedPruneTimes?.anime : storedPruneTimes?.manga; + + if (shouldReadStoredPruneTime && typeof storedPruneAt === "number") + currentLastPruneAt = storedPruneAt; + + return { mediaCache, currentLastPruneAt }; +}; + export const mediaListCollection = async ( anilistAuthorisation: AniListAuthorisation, userIdentity: UserIdentity, @@ -260,6 +308,14 @@ export const mediaListCollection = async ( const options = assignDefaultOptions(inputOptions); const currentCacheMinutes = settings.get().cacheMinutes; + const hydratedInputs = await hydrateCollectionInputs( + type, + mediaCache, + currentLastPruneAt, + ); + + mediaCache = hydratedInputs.mediaCache; + currentLastPruneAt = hydratedInputs.currentLastPruneAt; if (String(currentLastPruneAt) === "") { if (type === Type.Anime) |