diff options
Diffstat (limited to 'src')
| -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 | ||||
| -rw-r--r-- | src/routes/settings/+page.svelte | 7 | ||||
| -rw-r--r-- | src/stores/anime.ts | 12 | ||||
| -rw-r--r-- | src/stores/animeLastPrune.ts | 14 | ||||
| -rw-r--r-- | src/stores/cacheMinutes.ts | 14 | ||||
| -rw-r--r-- | src/stores/lastPruneAt.ts | 12 | ||||
| -rw-r--r-- | src/stores/manga.ts | 12 | ||||
| -rw-r--r-- | src/stores/mangaLastPrune.ts | 14 |
10 files changed, 136 insertions, 27 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}`); }; diff --git a/src/routes/settings/+page.svelte b/src/routes/settings/+page.svelte index 941f0715..f7b64296 100644 --- a/src/routes/settings/+page.svelte +++ b/src/routes/settings/+page.svelte @@ -4,6 +4,7 @@ import closeMangaByDefault from '../../stores/closeMangaByDefault'; import { chapterDatabase } from '$lib/chapterDatabase'; import cacheMangaMinutes from '../../stores/cacheMangaMinutes'; + import cacheMinutes from '../../stores/cacheMinutes'; export let data; @@ -67,7 +68,11 @@ > </li> <li> - Re-cache <b>ALL</b> manga every <input type="number" bind:value={$cacheMangaMinutes} /> minutes + Re-cache <b>ALL</b> media keys every <input type="number" bind:value={$cacheMinutes} /> minutes + </li> + <li> + Re-cache <b>ALL</b> manga chapter counts every + <input type="number" bind:value={$cacheMangaMinutes} /> minutes </li> </ul> {/if} diff --git a/src/stores/anime.ts b/src/stores/anime.ts new file mode 100644 index 00000000..f269d8d0 --- /dev/null +++ b/src/stores/anime.ts @@ -0,0 +1,12 @@ +import { browser } from '$app/environment'; +import { writable } from 'svelte/store'; + +const anime = writable<string>(browser ? localStorage.getItem('anime') ?? '' : ''); + +anime.subscribe((value) => { + if (browser) { + localStorage.setItem('anime', value); + } +}); + +export default anime; diff --git a/src/stores/animeLastPrune.ts b/src/stores/animeLastPrune.ts new file mode 100644 index 00000000..f2e747d1 --- /dev/null +++ b/src/stores/animeLastPrune.ts @@ -0,0 +1,14 @@ +import { browser } from '$app/environment'; +import { writable } from 'svelte/store'; + +const animeLastPrune = writable<string>( + browser ? localStorage.getItem('animeLastPrune') ?? '' : '' +); + +animeLastPrune.subscribe((value) => { + if (browser) { + localStorage.setItem('animeLastPrune', value); + } +}); + +export default animeLastPrune; diff --git a/src/stores/cacheMinutes.ts b/src/stores/cacheMinutes.ts new file mode 100644 index 00000000..ccea5db1 --- /dev/null +++ b/src/stores/cacheMinutes.ts @@ -0,0 +1,14 @@ +import { browser } from '$app/environment'; +import { writable } from 'svelte/store'; + +const cacheMinutes = writable<string>( + browser ? localStorage.getItem('cacheMinutes') ?? '30' : '30' +); + +cacheMinutes.subscribe((value) => { + if (browser) { + localStorage.setItem('cacheMinutes', value); + } +}); + +export default cacheMinutes; diff --git a/src/stores/lastPruneAt.ts b/src/stores/lastPruneAt.ts deleted file mode 100644 index 0720bc60..00000000 --- a/src/stores/lastPruneAt.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { browser } from '$app/environment'; -import { writable } from 'svelte/store'; - -const lastPruneAt = writable<string>(browser ? localStorage.getItem('lastPruneAt') ?? '' : ''); - -lastPruneAt.subscribe((value) => { - if (browser) { - localStorage.setItem('lastPruneAt', value); - } -}); - -export default lastPruneAt; diff --git a/src/stores/manga.ts b/src/stores/manga.ts new file mode 100644 index 00000000..74e3f95a --- /dev/null +++ b/src/stores/manga.ts @@ -0,0 +1,12 @@ +import { browser } from '$app/environment'; +import { writable } from 'svelte/store'; + +const manga = writable<string>(browser ? localStorage.getItem('manga') ?? '' : ''); + +manga.subscribe((value) => { + if (browser) { + localStorage.setItem('manga', value); + } +}); + +export default manga; diff --git a/src/stores/mangaLastPrune.ts b/src/stores/mangaLastPrune.ts new file mode 100644 index 00000000..f6b40b02 --- /dev/null +++ b/src/stores/mangaLastPrune.ts @@ -0,0 +1,14 @@ +import { browser } from '$app/environment'; +import { writable } from 'svelte/store'; + +const mangaLastPrune = writable<string>( + browser ? localStorage.getItem('mangaLastPrune') ?? '' : '' +); + +mangaLastPrune.subscribe((value) => { + if (browser) { + localStorage.setItem('mangaLastPrune', value); + } +}); + +export default mangaLastPrune; |