Tighten worker shutdown and poll helper cleanup
This commit is contained in:
+20
-3
@@ -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 };
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user