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/AnimeList.svelte | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

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

diff --git a/src/lib/List/Due/AnimeList.svelte b/src/lib/List/Due/AnimeList.svelte
index 17897d8d..9f38090b 100644
--- a/src/lib/List/Due/AnimeList.svelte
+++ b/src/lib/List/Due/AnimeList.svelte
@@ -3,11 +3,12 @@
 
 	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 anime from '../../../stores/anime';
 	import animeLastPrune from '../../../stores/mangaLastPrune';
 	import sortByDifference from '../../../stores/sortByDifference';
 	import displayNotStarted from '../../../stores/displayNotStarted';
+	import cacheMinutes from '../../../stores/cacheMinutes';
 
 	export let user: AniListAuthorisation;
 	export let identity: UserIdentity;
@@ -17,11 +18,21 @@
 	let startTime: number;
 	let endTime: number;
 
+	const keyCacher = setInterval(() => {
+		startTime = performance.now();
+		endTime = -1;
+		animeLists = mediaListCollection(user, identity, Type.Anime, $anime, $animeLastPrune, true);
+	}, Number($cacheMinutes || 10) * 1000 * 60);
+
 	onMount(async () => {
 		startTime = performance.now();
 		animeLists = mediaListCollection(user, identity, Type.Anime, $anime, $animeLastPrune);
 	});
 
+	onDestroy(() => {
+		clearInterval(keyCacher);
+	});
+
 	const cleanMedia = (media: { entries: { media: Media }[] }[], displayUnresolved: boolean) => {
 		if (media === undefined) {
 			return [];
@@ -80,7 +91,7 @@
 			);
 		});
 
-		if (!endTime) {
+		if (!endTime || endTime === -1) {
 			endTime = performance.now() - startTime;
 		}
 
-- 
cgit v1.2.3

