From 7d59db9b9fdb9e84596d5f7b2b2f68d3ca3230e6 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sat, 23 Dec 2023 16:45:09 -0800 Subject: refactor(media): use options object --- src/lib/AniList/media.ts | 66 ++++++++++++++++++++++------- src/lib/List/Anime/DueAnimeList.svelte | 11 ++--- src/lib/List/Manga/MangaListTemplate.svelte | 22 +++------- src/lib/Media/Anime/cache.ts | 4 +- src/lib/Tools/Wrapped.svelte | 12 ++++-- 5 files changed, 70 insertions(+), 45 deletions(-) (limited to 'src/lib') diff --git a/src/lib/AniList/media.ts b/src/lib/AniList/media.ts index 6fc83296..b1ea2f11 100644 --- a/src/lib/AniList/media.ts +++ b/src/lib/AniList/media.ts @@ -62,9 +62,16 @@ export interface Media { coverImage: { extraLarge: string; }; + tags: { + name: string; + }[]; + genres: string[]; } -export const flattenLists = (lists: { name: string; entries: { media: Media }[] }[]) => { +export const flattenLists = ( + lists: { name: string; entries: { media: Media }[] }[], + all = false +) => { if (lists === undefined) return []; const flattenedList: Media[] = []; @@ -80,19 +87,21 @@ export const flattenLists = (lists: { name: string; entries: { media: Media }[] self.findIndex((itemToCompare) => itemToCompare.id === item.id) === index ); - for (const list of lists) { - if (list.name.toLowerCase().includes('#dueinclude')) { - dueInclude = true; + if (!all) { + for (const list of lists) { + if (list.name.toLowerCase().includes('#dueinclude')) { + dueInclude = true; - markedMediaIds.splice(0, markedMediaIds.length); - markedMediaIds.push(...list.entries.map((entry) => entry.media.id)); - } + markedMediaIds.splice(0, markedMediaIds.length); + markedMediaIds.push(...list.entries.map((entry) => entry.media.id)); + } - if (dueInclude) continue; + if (dueInclude) continue; - if (list.name.toLowerCase().includes('#dueignore')) - markedMediaIds.push(...list.entries.map((entry) => entry.media.id)); - else flattenedList.push(...list.entries.map((entry) => entry.media)); + if (list.name.toLowerCase().includes('#dueignore')) + markedMediaIds.push(...list.entries.map((entry) => entry.media.id)); + else flattenedList.push(...list.entries.map((entry) => entry.media)); + } } return processedList(flattenedList, dueInclude); @@ -107,7 +116,8 @@ const collectionQueryTemplate = (type: Type, userId: number, includeCompleted: b lists { name entries { media { - id idMal status type episodes chapters format duration synonyms + id idMal status type episodes chapters format duration synonyms genres + tags { name } title { romaji english native } nextAiringEpisode { episode airingAt } mediaListEntry { @@ -122,15 +132,39 @@ const collectionQueryTemplate = (type: Type, userId: number, includeCompleted: b } }`; +interface CollectionOptions { + includeCompleted?: boolean; + forcePrune?: boolean; +} + +interface NonNullCollectionOptions { + includeCompleted: boolean; + forcePrune: boolean; +} + +const assignDefaultOptions = (options: CollectionOptions) => { + const nonNullOptions: NonNullCollectionOptions = { + includeCompleted: false, + forcePrune: false + }; + + if (options.includeCompleted !== undefined) + nonNullOptions.includeCompleted = options.includeCompleted; + if (options.forcePrune !== undefined) nonNullOptions.forcePrune = options.forcePrune; + + return nonNullOptions; +}; + export const mediaListCollection = async ( anilistAuthorisation: AniListAuthorisation, userIdentity: UserIdentity, type: Type, mediaCache: string | undefined, currentLastPruneAt: string | number, - forcePrune = false, - includeCompleted = false + inputOptions: CollectionOptions = {} ): Promise => { + const options = assignDefaultOptions(inputOptions); + let currentCacheMinutes; settings.subscribe((value) => (currentCacheMinutes = value.cacheMinutes)); @@ -142,7 +176,7 @@ export const mediaListCollection = async ( if ( (new Date().getTime() - Number(currentLastPruneAt)) / 1000 / 60 > Number(currentCacheMinutes) || - forcePrune + options.forcePrune ) { if (type === Type.Anime) { lastPruneTimes.setKey('anime', new Date().getTime()); @@ -167,7 +201,7 @@ export const mediaListCollection = async ( Accept: 'application/json' }, body: JSON.stringify({ - query: collectionQueryTemplate(type, userIdentity.id, includeCompleted) + query: collectionQueryTemplate(type, userIdentity.id, options.includeCompleted) }) }) ).json(); diff --git a/src/lib/List/Anime/DueAnimeList.svelte b/src/lib/List/Anime/DueAnimeList.svelte index e431ac89..efd4b31a 100644 --- a/src/lib/List/Anime/DueAnimeList.svelte +++ b/src/lib/List/Anime/DueAnimeList.svelte @@ -19,14 +19,9 @@ const keyCacher = setInterval(() => { startTime = performance.now(); endTime = -1; - animeLists = mediaListCollection( - user, - identity, - Type.Anime, - $anime, - $lastPruneTimes.anime, - true - ); + animeLists = mediaListCollection(user, identity, Type.Anime, $anime, $lastPruneTimes.anime, { + forcePrune: true + }); }, $settings.cacheMinutes * 1000 * 60); onMount(async () => { diff --git a/src/lib/List/Manga/MangaListTemplate.svelte b/src/lib/List/Manga/MangaListTemplate.svelte index 5409a3d9..d580343c 100644 --- a/src/lib/List/Manga/MangaListTemplate.svelte +++ b/src/lib/List/Manga/MangaListTemplate.svelte @@ -148,14 +148,9 @@ if (foundEntry && foundEntry.mediaListEntry) foundEntry.mediaListEntry.progress = (progress || 0) + 1; - mangaLists = mediaListCollection( - user, - identity, - Type.Manga, - $manga, - $lastPruneTimes.manga, - true - ); + mangaLists = mediaListCollection(user, identity, Type.Manga, $manga, $lastPruneTimes.manga, { + forcePrune: true + }); pendingUpdate = null; }); }; @@ -165,14 +160,9 @@ endTime = -1; pruneAllManga().then(() => { - mangaLists = mediaListCollection( - user, - identity, - Type.Manga, - $manga, - $lastPruneTimes.manga, - true - ); + mangaLists = mediaListCollection(user, identity, Type.Manga, $manga, $lastPruneTimes.manga, { + forcePrune: true + }); }); }; diff --git a/src/lib/Media/Anime/cache.ts b/src/lib/Media/Anime/cache.ts index c423a419..60164f85 100644 --- a/src/lib/Media/Anime/cache.ts +++ b/src/lib/Media/Anime/cache.ts @@ -5,7 +5,9 @@ import lastPruneTimes from '../../../stores/lastPruneTimes'; import type { AniListAuthorisation, UserIdentity } from '../../AniList/identity'; export const cleanCache = (user: AniListAuthorisation, identity: UserIdentity) => - mediaListCollection(user, identity, Type.Anime, get(anime), get(lastPruneTimes).anime, true); + mediaListCollection(user, identity, Type.Anime, get(anime), get(lastPruneTimes).anime, { + forcePrune: true + }); export const updateMedia = (id: number, progress: number | undefined, callback: () => void) => { fetch(`/api/anilist/increment?id=${id}&progress=${(progress || 0) + 1}`).then(callback); diff --git a/src/lib/Tools/Wrapped.svelte b/src/lib/Tools/Wrapped.svelte index 56c17942..6c7e16d1 100644 --- a/src/lib/Tools/Wrapped.svelte +++ b/src/lib/Tools/Wrapped.svelte @@ -189,8 +189,10 @@ Type.Anime, $anime, $lastPruneTimes.anime, - true, - true + { + forcePrune: true, + includeCompleted: true + } ) ) .filter( @@ -213,8 +215,10 @@ Type.Manga, $manga, $lastPruneTimes.manga, - true, - true + { + forcePrune: true, + includeCompleted: true + } ) ) .filter( -- cgit v1.2.3