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 | |
| 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')
| -rw-r--r-- | src/lib/AniList/media.ts | 50 | ||||
| -rw-r--r-- | src/lib/List/Due/AnimeList.svelte | 6 | ||||
| -rw-r--r-- | src/lib/List/Due/MangaList.svelte | 22 |
3 files changed, 64 insertions, 14 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']; }; diff --git a/src/lib/List/Due/AnimeList.svelte b/src/lib/List/Due/AnimeList.svelte index 4580cdf1..8527ed43 100644 --- a/src/lib/List/Due/AnimeList.svelte +++ b/src/lib/List/Due/AnimeList.svelte @@ -2,6 +2,8 @@ import { mediaListCollection, Type, flattenLists, type Media } from '$lib/AniList/media'; import type { UserIdentity, AniListAuthorisation } from '$lib/AniList/identity'; import { onMount } from 'svelte'; + import anime from '../../../stores/anime'; + import animeLastPrune from '../../../stores/mangaLastPrune'; export let user: AniListAuthorisation; export let identity: UserIdentity; @@ -13,7 +15,7 @@ onMount(async () => { startTime = performance.now(); - animeLists = mediaListCollection(user, identity, Type.Anime); + animeLists = mediaListCollection(user, identity, Type.Anime, $anime, $animeLastPrune); }); const cleanMedia = (media: object[][], displayUnresolved: boolean) => { @@ -104,7 +106,7 @@ }; const updateMedia = async (id: number, progress: number | undefined) => { - animeLists = mediaListCollection(user, identity, Type.Anime); + animeLists = mediaListCollection(user, identity, Type.Anime, $anime, $animeLastPrune); await fetch(`/anilist/increment?id=${id}&progress=${progress! + 1}`); }; diff --git a/src/lib/List/Due/MangaList.svelte b/src/lib/List/Due/MangaList.svelte index 936d310a..c7f58819 100644 --- a/src/lib/List/Due/MangaList.svelte +++ b/src/lib/List/Due/MangaList.svelte @@ -3,8 +3,8 @@ import type { UserIdentity, AniListAuthorisation } from '$lib/AniList/identity'; import { onMount } from 'svelte'; import { chapterCount } from '$lib/mangadex'; - import lastPruneAt from '../../../stores/lastPruneAt'; - import cacheMangaMinutes from '../../../stores/cacheMangaMinutes'; + import mangaLastPrune from '../../../stores/mangaLastPrune'; + import manga from '../../../stores/manga'; export let user: AniListAuthorisation; export let identity: UserIdentity; @@ -16,7 +16,7 @@ onMount(async () => { startTime = performance.now(); - mangaLists = mediaListCollection(user, identity, Type.Manga); + mangaLists = mediaListCollection(user, identity, Type.Manga, $manga, $mangaLastPrune); }); const cleanMedia = async (media: object[][], displayUnresolved: boolean) => { @@ -24,13 +24,13 @@ return []; } - if ($lastPruneAt == '') { - lastPruneAt.set(new Date().getTime().toString()); - } else { - if ((new Date().getTime() - Number($lastPruneAt)) / 1000 / 60 > Number($cacheMangaMinutes)) { - lastPruneAt.set(new Date().getTime().toString()); - } - } + // if ($lastPruneAt == '') { + // lastPruneAt.set(new Date().getTime().toString()); + // } else { + // if ((new Date().getTime() - Number($lastPruneAt)) / 1000 / 60 > Number($cacheMangaMinutes)) { + // lastPruneAt.set(new Date().getTime().toString()); + // } + // } const flattenedLists = flattenLists(media); const releasingMedia = flattenedLists.filter( @@ -76,7 +76,7 @@ }; const updateMedia = async (id: number, progress: number | undefined) => { - mangaLists = mediaListCollection(user, identity, Type.Manga); + mangaLists = mediaListCollection(user, identity, Type.Manga, $manga, $mangaLastPrune); await fetch(`/anilist/increment?id=${id}&progress=${progress! + 1}`); }; |