Enhance caching mechanism by adding future promise to cached function and updating maps retrieval logic to use it
SteamWarCI Build successful
SteamWarCI Build successful
This commit is contained in:
@@ -17,20 +17,30 @@
|
|||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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<T> extends Readable<T> {
|
export interface Cached<T> extends Readable<T> {
|
||||||
reload: () => void;
|
reload: () => void;
|
||||||
|
future: Promise<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function cached<T>(normal: T, init: () => Promise<T>): Cached<T> {
|
export function cached<T>(normal: T, init: () => Promise<T>): Cached<T> {
|
||||||
const store = writable<T>(normal);
|
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;
|
let first = true;
|
||||||
|
|
||||||
const reload = () => {
|
const reload = () => {
|
||||||
init().then(data => {
|
init().then((data) => {
|
||||||
store.set(data);
|
store.set(data);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -45,6 +55,7 @@ export function cached<T>(normal: T, init: () => Promise<T>): Cached<T> {
|
|||||||
return store.subscribe(run, invalidate);
|
return store.subscribe(run, invalidate);
|
||||||
},
|
},
|
||||||
reload,
|
reload,
|
||||||
|
future,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +69,7 @@ export function cachedFamily<T, K>(normal: K, init: (arg0: T) => Promise<K>): (a
|
|||||||
let first = true;
|
let first = true;
|
||||||
|
|
||||||
const reload = () => {
|
const reload = () => {
|
||||||
init(arg).then(data => {
|
init(arg).then((data) => {
|
||||||
store.set(data);
|
store.set(data);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ export const gamemodes = cached<string[]>([], async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const maps = cachedFamily<string, string[]>([], async (gamemode) => {
|
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`);
|
const res = await fetchWithToken(get(tokenStore), `/data/admin/gamemodes/${gamemode}/maps`);
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
|
|||||||
Reference in New Issue
Block a user