Web: Change to a webworker architecture to stop freezing main thread

This commit is contained in:
2025-12-20 01:16:03 +01:00
parent 66feaab027
commit 92ce48997d
7 changed files with 277 additions and 178 deletions

26
chess-worker.js Normal file
View File

@@ -0,0 +1,26 @@
let exports;
async function init() {
const resp = await fetch("./chess.wasm");
if (!resp.ok) {
throw new Error("fetch wasm failed ${resp.status} ${resp.statusText}");
}
const { instance } =
await WebAssembly.instantiateStreaming(resp, {});
exports = instance.exports;
}
await init();
self.postMessage({ type: "ready" });
self.onmessage = (e) => {
const { id, method, args = [] } = e.data;
try {
const value = exports[method](...args);
self.postMessage({ id, ok: true, value });
} catch (err) {
self.postMessage({ id, ok: false, error: String(err?.message ?? err) });
}
};