codehygiene / web /src /score-worker.js
brianbellx's picture
Add asset cache-busting hashes, Cache-Control headers, and squiggle-windowed long-file cleanup
7667cef verified
Raw History Blame Contribute Delete
892 Bytes
import { loadWeights, scoreCode } from "shared:engine";
const WARMUP_CODE = ["def warm(items):", " for item in items:", " return item", ""].join("\n");
const ASSET_QUERY = new URL(import.meta.url).search;
const weightsReady = loadWeights(new URL(`./weights.npz${ASSET_QUERY}`, import.meta.url).href).then((weights) => {
scoreCode(WARMUP_CODE, weights);
return weights;
});
globalThis.addEventListener("message", async (event) => {
const { id, code, threshold } = event.data;
try {
const weights = await weightsReady;
const started = performance.now();
const { score, squiggles } = scoreCode(code, weights, threshold);
const ms = performance.now() - started;
globalThis.postMessage({ id, result: { score, squiggles, ms } });
} catch (error) {
globalThis.postMessage({ id, error: String(error && error.message ? error.message : error) });
}
});