Tighten worker shutdown and poll helper cleanup

This commit is contained in:
Dymas
2026-05-16 10:26:55 +02:00
parent d12afb1284
commit 0194476b33
2 changed files with 34 additions and 10 deletions
+20 -3
View File
@@ -37,15 +37,32 @@ BROWSER_API_HELPER = r"""
BROWSER_POLL_HELPER = r"""
function startSerialPoll(callback, intervalMs) {
let inFlight = false;
return window.setInterval(async () => {
if (inFlight) return;
let active = true;
let timer = null;
const stop = () => {
active = false;
if (timer !== null) {
window.clearTimeout(timer);
timer = null;
}
};
const tick = async () => {
if (!active || inFlight) {
if (active) timer = window.setTimeout(tick, intervalMs);
return;
}
inFlight = true;
try {
await callback();
} finally {
inFlight = false;
if (active) timer = window.setTimeout(tick, intervalMs);
}
}, intervalMs);
};
window.addEventListener("pagehide", stop, { once: true });
window.addEventListener("beforeunload", stop, { once: true });
timer = window.setTimeout(tick, intervalMs);
return { stop };
}
"""