Enhance caching mechanism by adding future promise to cached function and updating maps retrieval logic to use it
All checks were successful
SteamWarCI Build successful

This commit is contained in:
2025-06-29 13:54:03 +02:00
parent a99a066f0d
commit 887235dc86
2 changed files with 16 additions and 5 deletions

View File

@ -23,14 +23,24 @@ import type {Readable, Subscriber, Unsubscriber} from "svelte/store";
export interface Cached<T> extends Readable<T> {
reload: () => void;
future: Promise<T>;
}
export function cached<T>(normal: T, init: () => Promise<T>): Cached<T> {
const store = writable<T>(normal);
const future = new Promise<T>((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<T>(normal: T, init: () => Promise<T>): Cached<T> {
return store.subscribe(run, invalidate);
},
reload,
future,
};
}
@ -58,7 +69,7 @@ export function cachedFamily<T, K>(normal: K, init: (arg0: T) => Promise<K>): (a
let first = true;
const reload = () => {
init(arg).then(data => {
init(arg).then((data) => {
store.set(data);
});
};

View File

@ -55,7 +55,7 @@ export const gamemodes = cached<string[]>([], async () => {
});
export const maps = cachedFamily<string, string[]>([], 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) {