From 887235dc86305af434b5fa5e468e61f3a2314afb Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 29 Jun 2025 13:54:03 +0200 Subject: [PATCH] Enhance caching mechanism by adding future promise to cached function and updating maps retrieval logic to use it --- src/components/stores/cached.ts | 19 +++++++++++++++---- src/components/stores/stores.ts | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/components/stores/cached.ts b/src/components/stores/cached.ts index 63a885b..8400ba5 100644 --- a/src/components/stores/cached.ts +++ b/src/components/stores/cached.ts @@ -17,20 +17,30 @@ * along with this program. If not, see . */ -import {readonly, writable} from "svelte/store"; +import { readonly, writable } from "svelte/store"; -import type {Readable, Subscriber, Unsubscriber} from "svelte/store"; +import type { Readable, Subscriber, Unsubscriber } from "svelte/store"; export interface Cached extends Readable { reload: () => void; + future: Promise; } export function cached(normal: T, init: () => Promise): Cached { const store = writable(normal); + const future = new Promise((resolve) => { + let f = true; + store.subscribe((value) => { + if (f) { + f = false; + resolve(value); + } + }); + }); let first = true; const reload = () => { - init().then(data => { + init().then((data) => { store.set(data); }); }; @@ -45,6 +55,7 @@ export function cached(normal: T, init: () => Promise): Cached { return store.subscribe(run, invalidate); }, reload, + future, }; } @@ -58,7 +69,7 @@ export function cachedFamily(normal: K, init: (arg0: T) => Promise): (a let first = true; const reload = () => { - init(arg).then(data => { + init(arg).then((data) => { store.set(data); }); }; diff --git a/src/components/stores/stores.ts b/src/components/stores/stores.ts index a44b7c7..0afaf52 100644 --- a/src/components/stores/stores.ts +++ b/src/components/stores/stores.ts @@ -55,7 +55,7 @@ export const gamemodes = cached([], async () => { }); export const maps = cachedFamily([], async (gamemode) => { - if (get(gamemodes).every((value) => value !== gamemode)) return []; + if ((await gamemodes.future).every((value) => value !== gamemode)) return []; const res = await fetchWithToken(get(tokenStore), `/data/admin/gamemodes/${gamemode}/maps`); if (!res.ok) {