refactor: align navigation-event with main branch pattern

This commit is contained in:
eikomaniac 2025-12-06 17:41:58 +00:00
parent 77b4c6db88
commit ca815a6079
3 changed files with 13 additions and 16 deletions

View file

@ -314,6 +314,6 @@ document.addEventListener("DOMContentLoaded", () => {
// Subscribe to navigation events from modules that can't directly import navigate
// due to circular dependency constraints
NavigationEvent.subscribe((url, options) => {
void navigate(url, options);
NavigationEvent.subscribe((event) => {
void navigate(event.url, event);
});

View file

@ -1,9 +1,11 @@
type NavigateOptions = {
type NavigationEvent = {
url: string;
data?: unknown;
force?: boolean;
tribeOverride?: boolean;
};
type SubscribeFunction = (url: string, options?: NavigateOptions) => void;
type SubscribeFunction = (event: NavigationEvent) => void;
const subscribers: SubscribeFunction[] = [];
@ -11,12 +13,12 @@ export function subscribe(fn: SubscribeFunction): void {
subscribers.push(fn);
}
export function dispatch(url: string, options?: NavigateOptions): void {
export function dispatch(event: NavigationEvent): void {
subscribers.forEach((fn) => {
try {
fn(url, options);
fn(event);
} catch (e) {
console.error("Navigate event subscriber threw an error");
console.error("Navigation event subscriber threw an error");
console.error(e);
}
});

View file

@ -392,7 +392,7 @@ TribeSocket.in.room.left(() => {
updateState(1);
TribePageMenu.enableButtons();
if (!$(".pageTribe").hasClass("active")) {
NavigationEvent.dispatch("/tribe");
NavigationEvent.dispatch({ url: "/tribe" });
}
TribeCarets.destroyAll();
TribeSound.play("leave");
@ -521,9 +521,7 @@ TribeSocket.in.room.initRace((data) => {
} else {
//TODO update lobby bars
if (ActivePage.get() !== "tribe") {
NavigationEvent.dispatch("/tribe", {
tribeOverride: true,
});
NavigationEvent.dispatch({ url: "/tribe", tribeOverride: true });
}
TribeBars.init("tribe");
TribeBars.show("tribe");
@ -531,10 +529,7 @@ TribeSocket.in.room.initRace((data) => {
}
if (room) room.seed = data.seed;
Random.setSeed(TribeState.getRoom()?.seed.toString() ?? "");
NavigationEvent.dispatch("/", {
tribeOverride: true,
force: true,
});
NavigationEvent.dispatch({ url: "/", tribeOverride: true, force: true });
TribeDelta.reset();
TribeDelta.showBar();
TribeCountdown.show2();
@ -766,7 +761,7 @@ TribeSocket.in.room.readyTimerOver(() => {
});
TribeSocket.in.room.backToLobby(() => {
NavigationEvent.dispatch("/tribe");
NavigationEvent.dispatch({ url: "/tribe" });
});
TribeSocket.in.room.finalPositions((data) => {