// wasm/app/worker.mjs
// Tier-evaluation Web Worker. Keeps the main thread responsive so the
// live ms counter actually ticks AND so cancel works (main thread
// terminates this worker via worker.terminate()).

import { evalOnTier } from "./runner.js";

self.onmessage = async (e) => {
    const { kind, runId, tier, src } = e.data;
    if (kind !== "eval") return;
    try {
        const output = await evalOnTier(tier, src, (loadingTier) => {
            self.postMessage({ kind: "loading", runId, tier: loadingTier });
        });
        self.postMessage({ kind: "done", runId, output });
    } catch (err) {
        self.postMessage({ kind: "error", runId, message: err && err.message ? err.message : String(err) });
    }
};
