mirror of
https://github.com/db9979/uconsole_asw_game.git
synced 2026-12-05 12:24:25 +01:00
Heatmap rasters (LOFAR/BTR/DEMON waterfalls) are packed into typed-array jobs and painted by a module worker into an OffscreenCanvas, which hands back an ImageBitmap. The main thread keeps drawing the previous bitmap at its anchor until the new one arrives, keeps one job in flight per plot and falls back to the shared synchronous painter when Worker/OffscreenCanvas is missing or the worker fails. Headless Chrome stays on the synchronous path unless opted in, because workers stall its virtual time. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AS36Eg72iLviiSVbCUVNAi
68 lines
2.1 KiB
JavaScript
68 lines
2.1 KiB
JavaScript
// Waterfall rasters are painted in a worker when the browser offers
|
|
// OffscreenCanvas there; otherwise (or after any worker failure) heatmap.js
|
|
// keeps painting on the main thread. <html data-heatmap-worker> reports which.
|
|
//
|
|
// Headless Chromium's virtual time never advances while a worker exists, so
|
|
// the page's many --virtual-time-budget probes would hang; headless pages paint
|
|
// on the main thread unless a probe sets globalThis.uJagdHeatmapWorker = true.
|
|
let client;
|
|
|
|
function mark(state) {
|
|
document.documentElement.dataset.heatmapWorker = state;
|
|
}
|
|
|
|
export function heatmapWorker() {
|
|
if (client !== undefined) return client;
|
|
client = null;
|
|
if (typeof Worker !== "function" || typeof OffscreenCanvas !== "function") {
|
|
mark("off");
|
|
return null;
|
|
}
|
|
if (globalThis.uJagdHeatmapWorker !== true && /HeadlessChrome/.test(navigator.userAgent)) {
|
|
mark("headless");
|
|
return null;
|
|
}
|
|
try {
|
|
client = workerClient(new Worker(new URL("./heatmap-worker.js", import.meta.url), {type: "module"}));
|
|
mark("on");
|
|
} catch {
|
|
client = null;
|
|
mark("off");
|
|
}
|
|
return client;
|
|
}
|
|
|
|
function workerClient(worker) {
|
|
// One job in flight per plot; a newer job while it runs replaces the
|
|
// waiting one, so a slow worker drops stale rasters instead of queueing.
|
|
const plots = new Map();
|
|
let sequence = 0;
|
|
const send = (id, job, done) => {
|
|
const seq = ++sequence;
|
|
plots.set(id, {seq, done, waiting: null});
|
|
worker.postMessage({id, seq, job}, [job.stamps.buffer, job.offsets.buffer, job.bins.buffer]);
|
|
};
|
|
worker.onmessage = ({data}) => {
|
|
const plot = plots.get(data.id);
|
|
if (!plot || plot.seq !== data.seq) { data.bitmap?.close?.(); return; }
|
|
plot.done(data.bitmap, data.anchor);
|
|
if (plot.waiting) send(data.id, plot.waiting.job, plot.waiting.done);
|
|
else plots.delete(data.id);
|
|
};
|
|
const fail = () => {
|
|
worker.terminate();
|
|
plots.clear();
|
|
client = null;
|
|
mark("off");
|
|
};
|
|
worker.onerror = fail;
|
|
worker.onmessageerror = fail;
|
|
return {
|
|
render(id, job, done) {
|
|
const plot = plots.get(id);
|
|
if (plot) plot.waiting = {job, done};
|
|
else send(id, job, done);
|
|
},
|
|
};
|
|
}
|