Something went wrong. Try again.
browser-based tetris-ish geometric packing puzzle game on icosahedron/octahedron/hexadecadeltahedron surfaces oscillatory.net/rad-cohesion
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162import type { DatabaseSync } from "node:sqlite";import type { IngestionWorker } from "./ingestion.ts";import type { LeaderboardApi } from "./leaderboards-api.ts";import type { OperationalApi } from "./operational-api.ts";import type { PlayerApi } from "./players-api.ts";import type { ScenarioApi } from "./scenarios-api.ts";
export interface ApplicationRuntime { readonly host: string; readonly port: number; readonly database: DatabaseSync; readonly worker: IngestionWorker; readonly leaderboards: LeaderboardApi; readonly players: PlayerApi; readonly scenarios: ScenarioApi; readonly operations: OperationalApi;}
interface RuntimeSlot { current: ApplicationRuntime | null; starting: Promise<ApplicationRuntime> | null; stopping: Promise<void> | null;}
const RUNTIME_KEY = Symbol.for("rad-cohesion.application-runtime");
function runtimeSlot(): RuntimeSlot { // Vite's config runner, its SSR bundle, and the production bootstrap can each // evaluate this module separately. A global symbol keeps one runtime across them. // SAFETY: this private symbol is only assigned a RuntimeSlot by this accessor. const symbolTable = globalThis as typeof globalThis & { [RUNTIME_KEY]?: RuntimeSlot }; const existing = symbolTable[RUNTIME_KEY];
if (existing !== undefined) return existing;
const created: RuntimeSlot = { current: null, starting: null, stopping: null }; symbolTable[RUNTIME_KEY] = created;
return created;}
export function currentApplicationRuntime(): ApplicationRuntime { const runtime = runtimeSlot().current;
if (!runtime) throw new Error("The application server runtime has not started.");
return runtime;}
export async function startApplicationRuntime(): Promise<ApplicationRuntime> { const slot = runtimeSlot();
if (slot.stopping) throw new Error("The application server runtime is shutting down.");
if (slot.current) return slot.current;
if (slot.starting) return slot.starting;
const starting = createApplicationRuntime().then((runtime) => { slot.current = runtime;
return runtime; }).finally(() => { slot.starting = null; });
slot.starting = starting;
return starting;}
export async function stopApplicationRuntime(): Promise<void> { const slot = runtimeSlot();
if (slot.stopping) return slot.stopping;
if (!slot.current && !slot.starting) return;
const stopping = (async () => { try { let runtime = slot.current;
if (!runtime && slot.starting) { try { runtime = await slot.starting; } catch { slot.starting = null; } }
if (runtime) { try { await runtime.worker.stop(); } finally { runtime.database.close();
if (slot.current === runtime) slot.current = null; slot.starting = null; } } } finally { slot.starting = null; slot.stopping = null; } })();
slot.stopping = stopping;
return stopping;}
async function createApplicationRuntime(): Promise<ApplicationRuntime> { const [{ loadApplicationConfig }, { openDatabase }, { migrate }, { startIngestion }, { createLeaderboardApi }, { createPlayerApi }, { createScenarioApi }, { createOperationalApi }] = await Promise.all([ import("./config.ts"), import("./database.ts"), import("./migrations.ts"), import("./ingestion.ts"), import("./leaderboards-api.ts"), import("./players-api.ts"), import("./scenarios-api.ts"), import("./operational-api.ts"), ]);
const config = loadApplicationConfig(); const database = openDatabase(config.databasePath);
try { await migrate(database);
const leaderboards = createLeaderboardApi(database, { publicOrigin: config.publicOrigin, trustedProxyIps: config.trustedProxyIps, });
const scenarios = createScenarioApi(database); const players = createPlayerApi(database);
const worker = startIngestion(database, config.ingestion);
try { return { host: config.host, port: config.port, database, worker, leaderboards, players, scenarios, operations: createOperationalApi(database, worker), }; } catch (error) { await worker.stop();
throw error; } } catch (error) { database.close();
throw error; }}