From 9a72bbdb41a3ef663aeba563b5061fb5c48c4b65 Mon Sep 17 00:00:00 2001
From: Fuwn <contact@fuwn.me>
Date: Sat, 2 Sep 2023 10:08:34 -0700
Subject: feat(due): background cache

---
 src/lib/List/Due/MangaList.svelte | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

(limited to 'src/lib/List/Due/MangaList.svelte')

diff --git a/src/lib/List/Due/MangaList.svelte b/src/lib/List/Due/MangaList.svelte
index 6056b9eb..53ab5823 100644
--- a/src/lib/List/Due/MangaList.svelte
+++ b/src/lib/List/Due/MangaList.svelte
@@ -1,7 +1,7 @@
 <script lang="ts">
 	import { mediaListCollection, Type, flattenLists, type Media } from '$lib/AniList/media';
 	import type { UserIdentity, AniListAuthorisation } from '$lib/AniList/identity';
-	import { onMount } from 'svelte';
+	import { onDestroy, onMount } from 'svelte';
 	import { chapterCount } from '$lib/mangadex';
 	import chaptersLastPrune from '../../../stores/chaptersLastPrune';
 	import manga from '../../../stores/manga';
@@ -10,6 +10,7 @@
 	import roundDownChapters from '../../../stores/roundDownChapters';
 	import mangaLastPrune from '../../../stores/mangaLastPrune';
 	import displayNotStarted from '../../../stores/displayNotStarted';
+	import cacheMinutes from '../../../stores/cacheMinutes';
 
 	export let user: AniListAuthorisation;
 	export let identity: UserIdentity;
@@ -19,11 +20,21 @@
 	let startTime: number;
 	let endTime: number;
 
+	const keyCacher = setInterval(() => {
+		startTime = performance.now();
+		endTime = -1;
+		mangaLists = mediaListCollection(user, identity, Type.Anime, $manga, $mangaLastPrune, true);
+	}, Number($cacheMinutes || 30) * 1000 * 60);
+
 	onMount(async () => {
 		startTime = performance.now();
 		mangaLists = mediaListCollection(user, identity, Type.Manga, $manga, $mangaLastPrune);
 	});
 
+	onDestroy(() => {
+		clearInterval(keyCacher);
+	});
+
 	const cleanMedia = async (
 		media: { entries: { media: Media }[] }[],
 		displayUnresolved: boolean
@@ -88,7 +99,7 @@
 			);
 		});
 
-		if (!endTime) {
+		if (!endTime || endTime === -1) {
 			endTime = performance.now() - startTime;
 		}
 
-- 
cgit v1.2.3

