Some Code Cleanup

This commit is contained in:
2023-12-25 21:54:40 +01:00
parent a2687083e0
commit 3108d9bf20
61 changed files with 305 additions and 247 deletions

View File

@@ -33,7 +33,7 @@ export function cached<T>(normal: T, init: () => Promise<T>): Cached<T> {
init().then(data => {
store.set(data);
});
}
};
return {
...readonly(store),
@@ -52,7 +52,7 @@ export function cachedFamily<T, K>(normal: K, init: (arg0: T) => Promise<K>): (a
const stores: Map<T, Cached<K>> = new Map();
return (arg: T) => {
if(stores.has(arg)) {
return stores.get(arg)!!;
return stores.get(arg)!;
} else {
const store = writable<K>(normal);
let first = true;
@@ -61,7 +61,7 @@ export function cachedFamily<T, K>(normal: K, init: (arg0: T) => Promise<K>): (a
init(arg).then(data => {
store.set(data);
});
}
};
const cachedStore = {
...readonly(store),
@@ -78,5 +78,5 @@ export function cachedFamily<T, K>(normal: K, init: (arg0: T) => Promise<K>): (a
stores.set(arg, cachedStore);
return cachedStore;
}
}
};
}

View File

@@ -17,28 +17,28 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import type {Player, SchematicType} from "../types/data.js";
import {PlayerSchema} from "../types/data.js";
import {cached, cachedFamily} from "./cached.js";
import type {Team} from "../types/team.js";
import {TeamSchema} from "../types/team.js";
import type {Player, SchematicType} from "@type/data";
import {PlayerSchema} from "@type/data.ts";
import {cached, cachedFamily} from "./cached";
import type {Team} from "@type/team.ts";
import {TeamSchema} from "@type/team";
import {derived, get, writable} from "svelte/store";
import {dataRepo, fetchWithToken, pageRepo, tokenStore} from "../repo/repo";
import {dataRepo, fetchWithToken, pageRepo, tokenStore} from "@repo/repo.ts";
import {z} from "zod";
export const schemTypes = cached<SchematicType[]>([], () =>
fetchWithToken(get(tokenStore), `/data/schematicTypes`)
.then(res => res.json()))
fetchWithToken(get(tokenStore), "/data/schematicTypes")
.then(res => res.json()));
export const players = cached<Player[]>([], async () => {
const res = await fetchWithToken(get(tokenStore), `/data/users`);
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`);
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 [];
@@ -49,22 +49,22 @@ export const maps = cachedFamily<string, string[]>([], async (gamemode) => {
} else {
return res.json();
}
})
});
export const groups = cached<string[]>([], async () => {
const res = await fetchWithToken(get(tokenStore), `/data/groups`);
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`);
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 = derived(dataRepo, $dataRepo => $dataRepo.getServer());