37 lines
1.5 KiB
TypeScript
37 lines
1.5 KiB
TypeScript
// file: crates/apps/game-reflex-poc-tauri/frontend/ts/main.ts
|
|
// version: 2
|
|
|
|
import { closeMainWindow, getRuntimeLabel, notifyFrontendReady } from "./bridge";
|
|
import { startGame } from "./game";
|
|
import { initializeTracing, tracingError, tracingInfo } from "./logging";
|
|
|
|
async function main(): Promise<void> {
|
|
await initializeTracing();
|
|
const canvas = document.querySelector<HTMLCanvasElement>("#game");
|
|
const score = document.querySelector<HTMLOutputElement>("#score");
|
|
const runtime = document.querySelector<HTMLOutputElement>("#runtime");
|
|
if (canvas === null || score === null || runtime === null) {
|
|
throw new Error("Reflex Tauri frontend DOM is incomplete");
|
|
}
|
|
runtime.value = await getRuntimeLabel();
|
|
const game = await startGame(canvas, score);
|
|
window.addEventListener("keydown", event => {
|
|
if (event.key !== "Escape" || event.repeat) {
|
|
return;
|
|
}
|
|
event.preventDefault();
|
|
const shouldExit = game.quit_requested_escape();
|
|
tracingInfo(`quit requested source=escape decision=${shouldExit ? "exit" : "continue"}`);
|
|
if (shouldExit) {
|
|
void closeMainWindow().catch(caughtError => tracingError("Tauri close command failed", caughtError));
|
|
}
|
|
});
|
|
await notifyFrontendReady();
|
|
tracingInfo("Reflex Tauri frontend started");
|
|
}
|
|
|
|
void main().catch(caughtError => {
|
|
const message = caughtError instanceof Error ? caughtError.stack ?? caughtError.message : String(caughtError);
|
|
tracingError(`Reflex Tauri frontend startup failed: ${message}`);
|
|
});
|