/* * This file is a part of the SteamWar software. * * Copyright (C) 2023 SteamWar.de-Serverteam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ import { readonly, writable } 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 = false; store.subscribe((value) => { if (f) { resolve(value); } else { f = true; } }); }); let first = true; const reload = () => { init().then((data) => { store.set(data); }); }; return { ...readonly(store), subscribe: (run: Subscriber, invalidate?: (value?: T) => void): Unsubscriber => { if (first) { first = false; reload(); } return store.subscribe(run, invalidate); }, reload, future, }; } export function cachedFamily(normal: K, init: (arg0: T) => Promise): (arg: T) => Cached { const stores: Map> = new Map(); return (arg: T) => { if (stores.has(arg)) { return stores.get(arg)!; } else { const store = writable(normal); let first = true; const reload = () => { init(arg).then((data) => { store.set(data); }); }; const cachedStore = { ...readonly(store), subscribe: (run: Subscriber, invalidate?: (value?: K) => void): Unsubscriber => { if (first) { first = false; reload(); } return store.subscribe(run, invalidate); }, reload, } as Cached; stores.set(arg, cachedStore); return cachedStore; } }; }