diff options
| author | Fuwn <[email protected]> | 2023-08-27 17:22:40 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-08-27 17:22:40 -0700 |
| commit | 7fc05993446392f5342815734a7a2e99cf63bddb (patch) | |
| tree | fb14a5ab35b6e4fdbcfb83ec18ad3ca594269134 /src/lib/AniList/media.ts | |
| parent | feat(layout): cache user identity (diff) | |
| download | due.moe-7fc05993446392f5342815734a7a2e99cf63bddb.tar.xz due.moe-7fc05993446392f5342815734a7a2e99cf63bddb.zip | |
feat: cache media
Diffstat (limited to 'src/lib/AniList/media.ts')
| -rw-r--r-- | src/lib/AniList/media.ts | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/src/lib/AniList/media.ts b/src/lib/AniList/media.ts index bd481d9a..36f042e9 100644 --- a/src/lib/AniList/media.ts +++ b/src/lib/AniList/media.ts @@ -1,5 +1,10 @@ import type { AniListAuthorisation } from '$lib/AniList/identity'; import type { UserIdentity } from './identity'; +import anime from '../../stores/anime'; +import manga from '../../stores/manga'; +import mangaLastPrune from '../../stores/mangaLastPrune'; +import animeLastPrune from '../../stores/animeLastPrune'; +import cacheMinutes from '../../stores/cacheMinutes'; export enum Type { Anime, @@ -51,8 +56,43 @@ export const flattenLists = (lists: object[][]) => { export const mediaListCollection = async ( anilistAuthorisation: AniListAuthorisation, userIdentity: UserIdentity, - type: Type + type: Type, + mediaCache: string | undefined, + currentLastPruneAt: string | number = 0 ) => { + let currentCacheMinutes; + + cacheMinutes.subscribe((value) => { + currentCacheMinutes = value; + }); + + if (String(currentLastPruneAt) == '') { + if (type === Type.Anime) { + animeLastPrune.set(new Date().getTime().toString()); + } else { + mangaLastPrune.set(new Date().getTime().toString()); + } + } else { + if ( + (new Date().getTime() - Number(currentLastPruneAt)) / 1000 / 60 > + Number(currentCacheMinutes) + ) { + if (type === Type.Anime) { + animeLastPrune.set(new Date().getTime().toString()); + anime.set(''); + } else { + mangaLastPrune.set(new Date().getTime().toString()); + manga.set(''); + } + + mediaCache = ''; + } + } + + if (mediaCache !== undefined && mediaCache !== '') { + return JSON.parse(mediaCache); + } + const userIdResponse = await ( await fetch('https://graphql.anilist.co', { method: 'POST', @@ -82,5 +122,13 @@ export const mediaListCollection = async ( }) ).json(); + if (mediaCache === '') { + if (type === Type.Anime) { + anime.set(JSON.stringify(userIdResponse['data']['MediaListCollection']['lists'])); + } else { + manga.set(JSON.stringify(userIdResponse['data']['MediaListCollection']['lists'])); + } + } + return userIdResponse['data']['MediaListCollection']['lists']; }; |