mirror of
https://github.com/Xevion/Pac-Man.git
synced 2026-01-31 12:25:04 -06:00
Only stop the game when navigating away from the game page (/) to avoid WASM runtime errors when navigating between /leaderboard and /download. Add error handling to gracefully handle crashes during page transitions.
30 lines
1.1 KiB
TypeScript
30 lines
1.1 KiB
TypeScript
import type { OnPageTransitionStartAsync } from "vike/types";
|
|
import { getPacmanWindow } from "@/lib/pacman";
|
|
import { setPendingNavigation } from "@/lib/navigation";
|
|
|
|
// Must match --transition-duration in layouts/tailwind.css
|
|
const TRANSITION_DURATION = 200;
|
|
|
|
export const onPageTransitionStart: OnPageTransitionStartAsync = async (pageContext) => {
|
|
console.log("Page transition start");
|
|
setPendingNavigation(pageContext.urlPathname);
|
|
document.querySelector("body")?.classList.add("page-is-transitioning");
|
|
|
|
// Only stop the game when navigating AWAY FROM the game page
|
|
// Don't stop when navigating between other pages (e.g., /leaderboard <-> /download)
|
|
if (window.location.pathname === "/") {
|
|
const win = getPacmanWindow();
|
|
if (win.Module?._stop_game) {
|
|
try {
|
|
console.log("Stopping game loop for page transition");
|
|
win.Module._stop_game();
|
|
} catch (error) {
|
|
console.warn("Failed to stop game (game may have already crashed):", error);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Wait for fade-out animation to complete before page content changes
|
|
await new Promise((resolve) => setTimeout(resolve, TRANSITION_DURATION));
|
|
};
|