diff options
| author | Fuwn <[email protected]> | 2023-09-03 18:36:53 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-09-03 18:36:53 -0700 |
| commit | 92293c66156e49f801fed6e8cc0996fd88f5f33b (patch) | |
| tree | 155c66f6103987a88ddcde2af46ee7c79cc47177 | |
| parent | fix(media): cache minutes from settings (diff) | |
| download | due.moe-92293c66156e49f801fed6e8cc0996fd88f5f33b.tar.xz due.moe-92293c66156e49f801fed6e8cc0996fd88f5f33b.zip | |
feat(settings): move last prune times to store
| -rw-r--r-- | src/lib/AniList/media.ts | 11 | ||||
| -rw-r--r-- | src/lib/List/Due/AnimeList.svelte | 22 | ||||
| -rw-r--r-- | src/lib/List/Due/MangaList.svelte | 24 | ||||
| -rw-r--r-- | src/lib/List/UpcomingAnimeList.svelte | 4 | ||||
| -rw-r--r-- | src/stores/animeLastPrune.ts | 14 | ||||
| -rw-r--r-- | src/stores/chaptersLastPrune.ts | 14 | ||||
| -rw-r--r-- | src/stores/lastPruneTimes.ts | 62 | ||||
| -rw-r--r-- | src/stores/mangaLastPrune.ts | 14 |
8 files changed, 102 insertions, 63 deletions
diff --git a/src/lib/AniList/media.ts b/src/lib/AniList/media.ts index 034fccfd..ebf8c72f 100644 --- a/src/lib/AniList/media.ts +++ b/src/lib/AniList/media.ts @@ -2,9 +2,8 @@ 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 settings from '../../stores/settings'; +import lastPruneTimes from '../../stores/lastPruneTimes'; export enum Type { Anime, @@ -69,9 +68,9 @@ export const mediaListCollection = async ( if (String(currentLastPruneAt) == '') { if (type === Type.Anime) { - animeLastPrune.set(new Date().getTime().toString()); + lastPruneTimes.setKey('anime', new Date().getTime().toString()); } else { - mangaLastPrune.set(new Date().getTime().toString()); + lastPruneTimes.setKey('manga', new Date().getTime().toString()); } } else { if ( @@ -80,10 +79,10 @@ export const mediaListCollection = async ( forcePrune ) { if (type === Type.Anime) { - animeLastPrune.set(new Date().getTime().toString()); + lastPruneTimes.setKey('anime', new Date().getTime().toString()); anime.set(''); } else { - mangaLastPrune.set(new Date().getTime().toString()); + lastPruneTimes.setKey('manga', new Date().getTime().toString()); manga.set(''); } diff --git a/src/lib/List/Due/AnimeList.svelte b/src/lib/List/Due/AnimeList.svelte index 1409730f..f70363f3 100644 --- a/src/lib/List/Due/AnimeList.svelte +++ b/src/lib/List/Due/AnimeList.svelte @@ -5,8 +5,8 @@ import type { UserIdentity, AniListAuthorisation } from '$lib/AniList/identity'; import { onDestroy, onMount } from 'svelte'; import anime from '../../../stores/anime'; - import animeLastPrune from '../../../stores/mangaLastPrune'; import settings from '../../../stores/settings'; + import lastPruneTimes from '../../../stores/lastPruneTimes'; export let user: AniListAuthorisation; export let identity: UserIdentity; @@ -19,12 +19,19 @@ const keyCacher = setInterval(() => { startTime = performance.now(); endTime = -1; - animeLists = mediaListCollection(user, identity, Type.Anime, $anime, $animeLastPrune, true); + animeLists = mediaListCollection( + user, + identity, + Type.Anime, + $anime, + $lastPruneTimes.anime, + true + ); }, Number($settings.cacheMinutes || 10) * 1000 * 60); onMount(async () => { startTime = performance.now(); - animeLists = mediaListCollection(user, identity, Type.Anime, $anime, $animeLastPrune); + animeLists = mediaListCollection(user, identity, Type.Anime, $anime, $lastPruneTimes.anime); }); onDestroy(() => { @@ -133,7 +140,14 @@ 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, $animeLastPrune, true); + animeLists = mediaListCollection( + user, + identity, + Type.Anime, + $anime, + $lastPruneTimes.anime, + true + ); }); }; </script> diff --git a/src/lib/List/Due/MangaList.svelte b/src/lib/List/Due/MangaList.svelte index 374d06e1..353d1895 100644 --- a/src/lib/List/Due/MangaList.svelte +++ b/src/lib/List/Due/MangaList.svelte @@ -3,11 +3,10 @@ import type { UserIdentity, AniListAuthorisation } from '$lib/AniList/identity'; import { onDestroy, onMount } from 'svelte'; import { chapterCount } from '$lib/mangadex'; - import chaptersLastPrune from '../../../stores/chaptersLastPrune'; import manga from '../../../stores/manga'; import { chapterDatabase } from '$lib/chapterDatabase'; - import mangaLastPrune from '../../../stores/mangaLastPrune'; import settings from '../../../stores/settings'; + import lastPruneTimes from '../../../stores/lastPruneTimes'; export let user: AniListAuthorisation; export let identity: UserIdentity; @@ -20,12 +19,12 @@ const keyCacher = setInterval(() => { startTime = performance.now(); endTime = -1; - mangaLists = mediaListCollection(user, identity, Type.Manga, $manga, $mangaLastPrune); + mangaLists = mediaListCollection(user, identity, Type.Manga, $manga, $lastPruneTimes.manga); }, Number($settings.cacheMinutes || 30) * 1000 * 60); onMount(async () => { startTime = performance.now(); - mangaLists = mediaListCollection(user, identity, Type.Manga, $manga, $mangaLastPrune); + mangaLists = mediaListCollection(user, identity, Type.Manga, $manga, $lastPruneTimes.manga); }); onDestroy(() => { @@ -40,17 +39,17 @@ return []; } - if ($chaptersLastPrune == '') { - chaptersLastPrune.set(new Date().getTime().toString()); + if ($lastPruneTimes.chapters === 1) { + lastPruneTimes.setKey('chapters', new Date().getTime().toString()); } else { if ( - (new Date().getTime() - Number($chaptersLastPrune)) / 1000 / 60 > + (new Date().getTime() - $lastPruneTimes.chapters) / 1000 / 60 > Number($settings.cacheMangaMinutes) ) { const unresolved = await chapterDatabase.chapters.where('chapters').equals(-1).toArray(); const ids = unresolved.map((m) => m.id); - chaptersLastPrune.set(new Date().getTime().toString()); + lastPruneTimes.setKey('chapters', new Date().getTime().toString()); await chapterDatabase.chapters.bulkDelete(ids); } } @@ -106,7 +105,14 @@ const updateMedia = async (id: number, progress: number | undefined) => { await chapterDatabase.chapters.delete(id); await fetch(`/anilist/increment?id=${id}&progress=${(progress || 0) + 1}`).then(() => { - mangaLists = mediaListCollection(user, identity, Type.Manga, $manga, $mangaLastPrune, true); + mangaLists = mediaListCollection( + user, + identity, + Type.Manga, + $manga, + $lastPruneTimes.manga, + true + ); }); }; </script> diff --git a/src/lib/List/UpcomingAnimeList.svelte b/src/lib/List/UpcomingAnimeList.svelte index 88b2cca0..87343032 100644 --- a/src/lib/List/UpcomingAnimeList.svelte +++ b/src/lib/List/UpcomingAnimeList.svelte @@ -5,7 +5,7 @@ import type { UserIdentity, AniListAuthorisation } from '$lib/AniList/identity'; import { onMount } from 'svelte'; import anime from '../../stores/anime'; - import animeLastPrune from '../../stores/mangaLastPrune'; + import lastPruneTimes from '../../stores/lastPruneTimes'; export let user: AniListAuthorisation; export let identity: UserIdentity; @@ -17,7 +17,7 @@ onMount(async () => { startTime = performance.now(); - animeLists = mediaListCollection(user, identity, Type.Anime, $anime, $animeLastPrune); + animeLists = mediaListCollection(user, identity, Type.Anime, $anime, $lastPruneTimes.anime); }); const cleanMedia = (media: { entries: { media: Media }[] }[], displayUnresolved: boolean) => { diff --git a/src/stores/animeLastPrune.ts b/src/stores/animeLastPrune.ts deleted file mode 100644 index f2e747d1..00000000 --- a/src/stores/animeLastPrune.ts +++ /dev/null @@ -1,14 +0,0 @@ -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/chaptersLastPrune.ts b/src/stores/chaptersLastPrune.ts deleted file mode 100644 index bccee23e..00000000 --- a/src/stores/chaptersLastPrune.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { browser } from '$app/environment'; -import { writable } from 'svelte/store'; - -const chaptersLastPrune = writable<string>( - browser ? localStorage.getItem('chaptersLastPrune') ?? '' : '' -); - -chaptersLastPrune.subscribe((value) => { - if (browser) { - localStorage.setItem('chaptersLastPrune', value); - } -}); - -export default chaptersLastPrune; diff --git a/src/stores/lastPruneTimes.ts b/src/stores/lastPruneTimes.ts new file mode 100644 index 00000000..ef71053b --- /dev/null +++ b/src/stores/lastPruneTimes.ts @@ -0,0 +1,62 @@ +import { browser } from '$app/environment'; +import { writable } from 'svelte/store'; + +interface LastPruneTimes { + anime: number; + chapters: number; + manga: number; +} + +const defaultTimes: LastPruneTimes = { + anime: 1, + chapters: 1, + manga: 1 +}; + +const createStore = () => { + const { subscribe, set, update } = writable<LastPruneTimes>( + JSON.parse( + browser + ? localStorage.getItem('lastPruneTimes') ?? JSON.stringify(defaultTimes) + : JSON.stringify(defaultTimes) + ) + ); + let state: LastPruneTimes; + + subscribe((value) => (state = value)); + + return { + subscribe, + set, + update, + reset: () => set(defaultTimes), + get: () => { + const keys = Object.keys(defaultTimes); + const lastPruneTimesKeys = Object.keys(state); + + if (keys.length !== lastPruneTimesKeys.length) { + return defaultTimes; + } + + for (const key of keys) { + if (!lastPruneTimesKeys.includes(key)) { + return defaultTimes; + } + } + + return state; + }, + setKey: (key: keyof LastPruneTimes, value: unknown) => + update((lastPruneTimes) => ({ ...lastPruneTimes, [key]: value })) + }; +}; + +const lastPruneTimes = createStore(); + +lastPruneTimes.subscribe((value) => { + if (browser) { + localStorage.setItem('lastPruneTimes', JSON.stringify(value)); + } +}); + +export default lastPruneTimes; diff --git a/src/stores/mangaLastPrune.ts b/src/stores/mangaLastPrune.ts deleted file mode 100644 index f6b40b02..00000000 --- a/src/stores/mangaLastPrune.ts +++ /dev/null @@ -1,14 +0,0 @@ -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; |