blob: ac3b4d72d8442fbcb2c2e20ca0c28fa91d4d163e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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";
const cacheStorageKey = (kind: MediaCacheKind) => `${kind}:v2`;
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>(cacheStorageKey(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;
};
|