This commit is contained in:
2023-06-25 16:06:32 +08:00
parent 494a0f66e3
commit 01d364a1ec
5 changed files with 108 additions and 21 deletions

30
server/state.ts Normal file
View File

@@ -0,0 +1,30 @@
import { signal } from "@preact/signals";
import { StateUpdater } from "preact/hooks";
export const state = signal("#/");
let listener: StateUpdater<string> | undefined = undefined;
export function initState(l: StateUpdater<string>) {
const hash = document.location.hash;
listener = l;
if (!hash || hash == "#") {
set_state("#/");
} else {
set_state(hash);
}
self.addEventListener("popstate", (e) => {
const s = e.state;
if (typeof s === "string") {
l(s);
} else {
l("#/");
}
});
}
export const set_state: StateUpdater<string> = (updater) => {
const v = typeof updater === "function" ? updater(state.value) : updater;
state.value = v;
history.pushState(v, "", v);
if (listener) listener(v);
};