fix games

This commit is contained in:
Chris Wanstrath 2025-10-02 13:23:07 -07:00
parent 56952afa5b
commit d95d2ec4b3
3 changed files with 7 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import { $$ } from "./dom"
import { randomId } from "../shared/utils" import { randomId } from "../shared/utils"
import { setStatus, addOutput, insert } from "./scrollback" import { setStatus, addOutput, insert } from "./scrollback"
import { browserCommands } from "./commands" import { browserCommands } from "./commands"
import { sessionId } from "./session"
const FPS = 30 const FPS = 30
const HEIGHT = 540 const HEIGHT = 540
@ -34,7 +35,7 @@ export async function handleGameStart(msg: Message) {
let game let game
try { try {
game = await import(`/source/${name}`) game = await import(`/source/${name}?session=${sessionId}`)
} catch (err: any) { } catch (err: any) {
setStatus(msgId, "error") setStatus(msgId, "error")
addOutput(msgId, `Error: ${err.message ? err.message : err}`) addOutput(msgId, `Error: ${err.message ? err.message : err}`)

View File

@ -17,6 +17,7 @@ import { initSneakers, disconnectSneakers } from "./sneaker"
import { dispatchMessage } from "./dispatch" import { dispatchMessage } from "./dispatch"
import { fatal } from "./fatal" import { fatal } from "./fatal"
import { getState } from "./state" import { getState } from "./state"
import { sessionRun } from "./session"
import { Layout } from "./html/layout" import { Layout } from "./html/layout"
import { Terminal } from "./html/terminal" import { Terminal } from "./html/terminal"
@ -96,7 +97,8 @@ app.use("*", async (c, next) => {
app.get("/source/:name", async c => { app.get("/source/:name", async c => {
const name = c.req.param("name") const name = c.req.param("name")
const path = commandPath(name) const sessionId = c.req.query("session") || "0"
const path = await sessionRun(sessionId, () => commandPath(name))
if (!path) return c.text("Command not found", 404) if (!path) return c.text("Command not found", 404)
return new Response(await transpile(path), { return new Response(await transpile(path), {
headers: { headers: {
@ -111,7 +113,7 @@ app.on(["GET", "POST"], ["/cmd/:name"], async c => {
const method = c.req.method const method = c.req.method
try { try {
const mod = await loadCommandModule(cmd) const mod = await sessionRun(sessionId, () => loadCommandModule(cmd))
if (!mod || !mod[method]) if (!mod || !mod[method])
return c.json({ status: "error", output: `No ${method} export in ${cmd}` }, 500) return c.json({ status: "error", output: `No ${method} export in ${cmd}` }, 500)

View File

@ -17,7 +17,7 @@ export const ALS = g.__thread ??= new AsyncLocalStorage<Session>()
const sessions: Map<string, Session> = new Map() const sessions: Map<string, Session> = new Map()
export async function sessionRun(sessionId: string, fn: () => void | Promise<void>) { export async function sessionRun(sessionId: string, fn: () => any) {
const state = sessionStore(sessionId) const state = sessionStore(sessionId)
return await ALS.run(state, async () => fn()) return await ALS.run(state, async () => fn())
} }