From 3f0754c899f84648f05b3a8fbf0a6e66d9ec3bdf Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 10 Sep 2023 02:09:38 -0700 Subject: refactor(anime): generalise clean cache --- src/lib/List/Due/AnimeList.svelte | 29 ++++++++--------------------- src/lib/List/UpcomingAnimeList.svelte | 20 +++++++------------- src/lib/List/WatchingAnimeList.svelte | 29 ++++++++--------------------- src/lib/anime.ts | 17 ++++++++++++++++- 4 files changed, 39 insertions(+), 56 deletions(-) diff --git a/src/lib/List/Due/AnimeList.svelte b/src/lib/List/Due/AnimeList.svelte index 0be1ba1e..66a26712 100644 --- a/src/lib/List/Due/AnimeList.svelte +++ b/src/lib/List/Due/AnimeList.svelte @@ -7,7 +7,7 @@ import anime from '../../../stores/anime'; import settings from '../../../stores/settings'; import lastPruneTimes from '../../../stores/lastPruneTimes'; - import { airingTime, totalEpisodes } from '$lib/anime'; + import { airingTime, cleanCache, totalEpisodes } from '$lib/anime'; export let user: AniListAuthorisation; export let identity: UserIdentity; @@ -107,27 +107,9 @@ const updateMedia = async (id: number, progress: number | undefined) => { fetch(`/anilist/increment?id=${id}&progress=${(progress || 0) + 1}`).then(() => { - animeLists = mediaListCollection( - user, - identity, - Type.Anime, - $anime, - $lastPruneTimes.anime, - true - ); + animeLists = cleanCache(user, identity); }); }; - - const cleanCache = () => { - animeLists = mediaListCollection( - user, - identity, - Type.Anime, - $anime, - $lastPruneTimes.anime, - true - ); - }; {#await animeLists} @@ -144,7 +126,12 @@ {#if cleanedMedia.length === 0} {/if} diff --git a/src/lib/List/UpcomingAnimeList.svelte b/src/lib/List/UpcomingAnimeList.svelte index 7cac01fc..bb52b573 100644 --- a/src/lib/List/UpcomingAnimeList.svelte +++ b/src/lib/List/UpcomingAnimeList.svelte @@ -7,7 +7,7 @@ import anime from '../../stores/anime'; import lastPruneTimes from '../../stores/lastPruneTimes'; import settings from '../../stores/settings'; - import { airingTime } from '$lib/anime'; + import { airingTime, cleanCache } from '$lib/anime'; export let user: AniListAuthorisation; export let identity: UserIdentity; @@ -72,17 +72,6 @@ return finalMedia; }; - - const cleanCache = () => { - animeLists = mediaListCollection( - user, - identity, - Type.Anime, - $anime, - $lastPruneTimes.anime, - true - ); - }; {#await animeLists} @@ -99,7 +88,12 @@ {#if cleanedMedia.length === 0} {/if} diff --git a/src/lib/List/WatchingAnimeList.svelte b/src/lib/List/WatchingAnimeList.svelte index f22d80df..e76b1ef0 100644 --- a/src/lib/List/WatchingAnimeList.svelte +++ b/src/lib/List/WatchingAnimeList.svelte @@ -7,7 +7,7 @@ import anime from '../../stores/anime'; import lastPruneTimes from '../../stores/lastPruneTimes'; import settings from '../../stores/settings'; - import { totalEpisodes } from '$lib/anime'; + import { cleanCache, totalEpisodes } from '$lib/anime'; export let user: AniListAuthorisation; export let identity: UserIdentity; @@ -62,27 +62,9 @@ const updateMedia = async (id: number, progress: number | undefined) => { fetch(`/anilist/increment?id=${id}&progress=${(progress || 0) + 1}`).then(() => { - animeLists = mediaListCollection( - user, - identity, - Type.Anime, - $anime, - $lastPruneTimes.anime, - true - ); + animeLists = cleanCache(user, identity); }); }; - - const cleanCache = () => { - animeLists = mediaListCollection( - user, - identity, - Type.Anime, - $anime, - $lastPruneTimes.anime, - true - ); - }; {#await animeLists} @@ -99,7 +81,12 @@ {#if cleanedMedia.length === 0} {/if} diff --git a/src/lib/anime.ts b/src/lib/anime.ts index 8547882f..2ebdf25e 100644 --- a/src/lib/anime.ts +++ b/src/lib/anime.ts @@ -1,4 +1,19 @@ -import type { Media } from './AniList/media'; +import { get } from 'svelte/store'; +import anime from '../stores/anime'; +import { mediaListCollection, type Media, Type } from './AniList/media'; +import lastPruneTimes from '../stores/lastPruneTimes'; +import type { AniListAuthorisation, UserIdentity } from './AniList/identity'; + +export const cleanCache = (user: AniListAuthorisation, identity: UserIdentity) => { + return mediaListCollection( + user, + identity, + Type.Anime, + get(anime), + get(lastPruneTimes).anime, + true + ); +}; export const totalEpisodes = (anime: Media) => { return anime.episodes === null ? '' : `/${anime.episodes}`; -- cgit v1.2.3