mirror of
https://github.com/lifegpc/eh-downloader.git
synced 2026-07-08 01:31:00 +08:00
Update
This commit is contained in:
30
server/state.ts
Normal file
30
server/state.ts
Normal 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);
|
||||
};
|
||||
Reference in New Issue
Block a user