fix(web): prevent stopGame from being called on non-game page transitions

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.
This commit is contained in:
2025-12-29 14:53:42 -06:00
parent 21078b7ada
commit e017a87e12
2 changed files with 31 additions and 21 deletions
+19 -16
View File
@@ -23,23 +23,26 @@ function restartGame() {
const win = getPacmanWindow(); const win = getPacmanWindow();
const module = win.Module; const module = win.Module;
if (module?._restart_game) { if (!module?._restart_game) {
const canvas = document.getElementById("canvas") as HTMLCanvasElement | null; console.warn("Game restart function not available (WASM may not be initialized)");
if (!canvas) { return;
console.error("Canvas element not found during game restart"); }
return;
}
// Update canvas reference BEFORE restart - App::new() will read from Module.canvas const canvas = document.getElementById("canvas") as HTMLCanvasElement | null;
module.canvas = canvas; if (!canvas) {
// SDL2's Emscripten backend reads this for canvas lookup console.error("Canvas element not found during game restart");
win.SDL_CANVAS_ID = "#canvas"; return;
}
try { // Update canvas reference BEFORE restart - App::new() will read from Module.canvas
console.log("Restarting game with fresh App instance"); module.canvas = canvas;
module._restart_game(); // SDL2's Emscripten backend reads this for canvas lookup
} catch (error) { win.SDL_CANVAS_ID = "#canvas";
console.error("Failed to restart game:", error);
} try {
console.log("Restarting game with fresh App instance");
module._restart_game();
} catch (error) {
console.error("Failed to restart game:", error);
} }
} }
+12 -5
View File
@@ -10,11 +10,18 @@ export const onPageTransitionStart: OnPageTransitionStartAsync = async (pageCont
setPendingNavigation(pageContext.urlPathname); setPendingNavigation(pageContext.urlPathname);
document.querySelector("body")?.classList.add("page-is-transitioning"); document.querySelector("body")?.classList.add("page-is-transitioning");
// Stop the game loop when navigating away from the game page // Only stop the game when navigating AWAY FROM the game page
const win = getPacmanWindow(); // Don't stop when navigating between other pages (e.g., /leaderboard <-> /download)
if (win.Module?._stop_game) { if (window.location.pathname === "/") {
console.log("Stopping game loop for page transition"); const win = getPacmanWindow();
win.Module._stop_game(); 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 // Wait for fade-out animation to complete before page content changes