WIP: major refactoring

This commit is contained in:
Jörn-Michael Miehe 2024-08-23 18:59:11 +00:00
parent 15b957791f
commit e3e19a0572

View file

@ -1,6 +1,6 @@
import { acceptHMRUpdate, defineStore } from "pinia"; import { acceptHMRUpdate, defineStore } from "pinia";
import { API } from "./api"; import { API } from "./api";
import { Loading, loading_success } from "./helpers"; import { Loading } from "./helpers";
import { Credentials, DoorSaved, ImageData, SiteConfigModel } from "./model"; import { Credentials, DoorSaved, ImageData, SiteConfigModel } from "./model";
import { Door } from "./rects/door"; import { Door } from "./rects/door";
@ -11,10 +11,10 @@ declare global {
} }
type State = { type State = {
on_initialized: (() => void)[]; on_initialized: (() => void)[] | null;
is_touch_device: boolean; is_touch_device: boolean;
is_admin: boolean; is_admin: boolean;
site_config: Loading<SiteConfigModel>; site_config: SiteConfigModel;
background_image: Loading<ImageData>; background_image: Loading<ImageData>;
user_doors: Door[]; user_doors: Door[];
next_door_target: number | null; next_door_target: number | null;
@ -31,15 +31,25 @@ export const advent22Store = defineStore({
navigator.maxTouchPoints > 0 || navigator.maxTouchPoints > 0 ||
navigator.msMaxTouchPoints > 0, navigator.msMaxTouchPoints > 0,
is_admin: false, is_admin: false,
site_config: "loading", site_config: {
title: document.title,
subtitle: "",
content: "",
footer: "",
},
background_image: "loading", background_image: "loading",
user_doors: [], user_doors: [],
next_door_target: null, next_door_target: null,
}), }),
actions: { actions: {
init(): void { async init(): Promise<void> {
this.update().then(() => this.on_initialized.forEach((fn) => fn())); await this.update();
if (this.on_initialized !== null) {
for (const callback of this.on_initialized) callback();
}
this.on_initialized = null;
}, },
async update(): Promise<void> { async update(): Promise<void> {
@ -84,7 +94,7 @@ export const advent22Store = defineStore({
}, },
when_initialized(callback: () => void): void { when_initialized(callback: () => void): void {
if (loading_success(this.site_config)) { if (this.on_initialized === null) {
callback(); callback();
} else { } else {
this.on_initialized.push(callback); this.on_initialized.push(callback);