Updates and more
This commit is contained in:
63
src/components/stores/cached.ts
Normal file
63
src/components/stores/cached.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import {readonly, writable} from "svelte/store";
|
||||
|
||||
import type {Readable, Subscriber, Unsubscriber} from "svelte/store";
|
||||
|
||||
export interface Cached<T> extends Readable<T>{
|
||||
reload: () => void;
|
||||
}
|
||||
|
||||
export function cached<T>(normal: T, init: () => Promise<T>): Cached<T> {
|
||||
const store = writable<T>(normal);
|
||||
let first = true;
|
||||
|
||||
const reload = () => {
|
||||
init().then(data => {
|
||||
store.set(data);
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
...readonly(store),
|
||||
subscribe: (run: Subscriber<T>, invalidate?: (value?: T) => void): Unsubscriber => {
|
||||
if(first) {
|
||||
first = false;
|
||||
reload();
|
||||
}
|
||||
return store.subscribe(run, invalidate);
|
||||
},
|
||||
reload
|
||||
};
|
||||
}
|
||||
|
||||
export function cachedFamily<T, K>(normal: K, init: (arg0: T) => Promise<K>): (arg: T) => Cached<K> {
|
||||
const stores: Map<T, Cached<K>> = new Map();
|
||||
return (arg: T) => {
|
||||
if(stores.has(arg)) {
|
||||
return stores.get(arg)!!;
|
||||
} else {
|
||||
const store = writable<K>(normal);
|
||||
let first = true;
|
||||
|
||||
const reload = () => {
|
||||
init(arg).then(data => {
|
||||
store.set(data);
|
||||
});
|
||||
}
|
||||
|
||||
const cachedStore = {
|
||||
...readonly(store),
|
||||
subscribe: (run: Subscriber<K>, invalidate?: (value?: K) => void): Unsubscriber => {
|
||||
if(first) {
|
||||
first = false;
|
||||
reload();
|
||||
}
|
||||
return store.subscribe(run, invalidate);
|
||||
},
|
||||
reload
|
||||
} as Cached<K>;
|
||||
|
||||
stores.set(arg, cachedStore);
|
||||
return cachedStore;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
import {readable} from "svelte/store";
|
||||
import type {Readable} from "svelte/store";
|
||||
|
||||
export const server = readable(fetch(import.meta.env.PUBLIC_API_SERVER + "/data/server").then(res => res.json()))
|
||||
53
src/components/stores/stores.ts
Normal file
53
src/components/stores/stores.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import type {Player, SchematicType, Server} from "../types/data.js";
|
||||
import {PlayerSchema, ServerSchema} from "../types/data.js";
|
||||
import {cached, cachedFamily} from "./cached.js";
|
||||
import type {Team} from "../types/team.js";
|
||||
import {TeamSchema} from "../types/team.js";
|
||||
import {derived, get, readable, writable} from "svelte/store";
|
||||
import {dataRepo, fetchWithToken, pageRepo, tokenStore} from "../repo/repo.js";
|
||||
import {z} from "zod";
|
||||
|
||||
export const schemTypes = cached<SchematicType[]>([], () =>
|
||||
fetchWithToken(get(tokenStore), `/data/schematicTypes`)
|
||||
.then(res => res.json()))
|
||||
|
||||
export const players = cached<Player[]>([], async () => {
|
||||
const res = await fetchWithToken(get(tokenStore), `/data/users`);
|
||||
return z.array(PlayerSchema).parse(await res.json());
|
||||
})
|
||||
|
||||
export const gamemodes = cached<string[]>([], async () => {
|
||||
const res = await fetchWithToken(get(tokenStore), `/data/gamemodes`);
|
||||
return z.array(z.string()).parse(await res.json());
|
||||
})
|
||||
|
||||
export const maps = cachedFamily<string, string[]>([], async (gamemode) => {
|
||||
if (get(gamemodes).every(value => value !== gamemode)) return [];
|
||||
|
||||
const res = await fetchWithToken(get(tokenStore), `/data/gamemodes/${gamemode}/maps`);
|
||||
if (!res.ok) {
|
||||
return [];
|
||||
} else {
|
||||
return res.json();
|
||||
}
|
||||
})
|
||||
|
||||
export const groups = cached<string[]>([], async () => {
|
||||
const res = await fetchWithToken(get(tokenStore), `/data/groups`);
|
||||
return z.array(z.string()).parse(await res.json());
|
||||
})
|
||||
|
||||
export const teams = cached<Team[]>([], async () => {
|
||||
const res = await fetchWithToken(get(tokenStore), `/team`);
|
||||
return z.array(TeamSchema).parse(await res.json());
|
||||
})
|
||||
|
||||
export const branches = cached<string[]>([], async () => {
|
||||
const res = await get(pageRepo).getBranches();
|
||||
return z.array(z.string()).parse(res);
|
||||
})
|
||||
|
||||
export const server = readable(new Promise((resolve) => {}), (set) => set(get(dataRepo).getServer()));
|
||||
|
||||
export const isWide = writable(window.innerWidth >= 640);
|
||||
window.addEventListener("resize", () => isWide.set(window.innerWidth >= 640));
|
||||
Reference in New Issue
Block a user