A
This commit is contained in:
53
webui/src/lib/runner.ts
Normal file
53
webui/src/lib/runner.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
"use client";
|
||||
|
||||
let worker: Worker | null = null;
|
||||
|
||||
export async function runScript(
|
||||
script: string,
|
||||
appendOutput: (line: string) => void,
|
||||
appendTerminal?: (line: string) => void
|
||||
): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (worker) {
|
||||
worker.terminate();
|
||||
}
|
||||
|
||||
worker = new Worker(new URL('./script-runner.worker.ts', import.meta.url));
|
||||
|
||||
worker.onmessage = (event) => {
|
||||
const { type, line, error } = event.data;
|
||||
|
||||
if (type === 'output') {
|
||||
appendOutput(line);
|
||||
} else if (type === 'terminal') {
|
||||
appendTerminal?.(line);
|
||||
} else if (type === 'complete') {
|
||||
worker?.terminate();
|
||||
worker = null;
|
||||
resolve();
|
||||
} else if (type === 'error') {
|
||||
worker?.terminate();
|
||||
worker = null;
|
||||
reject(new Error(error));
|
||||
} else if (type === 'stopped') {
|
||||
worker?.terminate();
|
||||
worker = null;
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
|
||||
worker.onerror = (error) => {
|
||||
worker?.terminate();
|
||||
worker = null;
|
||||
reject(error);
|
||||
};
|
||||
|
||||
worker.postMessage({ type: 'run', script });
|
||||
});
|
||||
}
|
||||
|
||||
export function stopScript(): void {
|
||||
if (worker) {
|
||||
worker.postMessage({ type: 'stop' });
|
||||
}
|
||||
}
|
||||
73
webui/src/lib/script-runner.worker.ts
Normal file
73
webui/src/lib/script-runner.worker.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import init, { GameState } from "./wasm/script_runner";
|
||||
|
||||
let wasmReady = false;
|
||||
let wasmInitPromise: Promise<void> | null = null;
|
||||
let currentGame: GameState | null = null;
|
||||
|
||||
async function initWasm(): Promise<void> {
|
||||
if (wasmReady) return;
|
||||
|
||||
if (wasmInitPromise) {
|
||||
return wasmInitPromise;
|
||||
}
|
||||
|
||||
wasmInitPromise = (async () => {
|
||||
await init();
|
||||
wasmReady = true;
|
||||
})();
|
||||
|
||||
return wasmInitPromise;
|
||||
}
|
||||
|
||||
self.onmessage = async (event) => {
|
||||
const { type, script } = event.data;
|
||||
|
||||
if (type === "run") {
|
||||
try {
|
||||
await initWasm();
|
||||
|
||||
const appendOutput = (line: string) => {
|
||||
self.postMessage({ type: "output", line });
|
||||
};
|
||||
|
||||
const appendDebug = (line: string) => {
|
||||
self.postMessage({ type: "output", line }); // debug also goes to output
|
||||
};
|
||||
|
||||
const appendTerminal = (line: string) => {
|
||||
self.postMessage({ type: "terminal", line });
|
||||
};
|
||||
|
||||
currentGame = new GameState(
|
||||
script,
|
||||
"max",
|
||||
appendOutput,
|
||||
appendDebug,
|
||||
|
||||
script,
|
||||
"min",
|
||||
appendOutput,
|
||||
appendDebug,
|
||||
|
||||
appendTerminal
|
||||
);
|
||||
|
||||
while (currentGame && !currentGame.is_done()) {
|
||||
const res = currentGame.step();
|
||||
if (res === undefined) {
|
||||
self.postMessage({ type: "complete" });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentGame) {
|
||||
self.postMessage({ type: "complete" });
|
||||
}
|
||||
} catch (error) {
|
||||
self.postMessage({ type: "error", error: String(error) });
|
||||
}
|
||||
} else if (type === "stop") {
|
||||
currentGame = null;
|
||||
self.postMessage({ type: "stopped" });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user