Poll every 8 s while visible, pause in background tabs

Conversations and the owner screen refreshed every 20 s and 30 s. A shared
keepFresh helper now checks every 8 s while the tab is visible, pauses when
it is hidden, catches up at once when it returns, never overlaps runs, and
stops when the screen is gone or the owner token is. The latter also stops
the sign-in form being redrawn under someone typing.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 20:58:45 +01:00
co-authored by Claude Sonnet 5.5
parent 71e6dc8658
commit dc690a72c9
6 changed files with 201 additions and 23 deletions
+96
View File
@@ -0,0 +1,96 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { JSDOM } from "jsdom";
import { FOREGROUND_MS, keepFresh } from "../src/fresh.ts";
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
/** A document whose visibility the test controls. */
function page() {
const doc = new JSDOM("<!doctype html>").window.document;
let hidden = false;
Object.defineProperty(doc, "hidden", { get: () => hidden });
return {
doc,
setHidden(h: boolean) {
hidden = h;
doc.dispatchEvent(new doc.defaultView!.Event("visibilitychange"));
},
};
}
test("it checks again and again while the page is visible", async () => {
const p = page();
let runs = 0;
let alive = true;
keepFresh(() => alive, async () => void runs++, 20, p.doc);
await sleep(110);
alive = false;
assert.ok(runs >= 3, `ran ${runs} times`);
});
test("it pauses in a background tab and catches up at once when the tab returns", async () => {
const p = page();
let runs = 0;
let alive = true;
keepFresh(() => alive, async () => void runs++, 20, p.doc);
await sleep(50);
p.setHidden(true);
await sleep(30); // let any run that was already due finish
const paused = runs;
await sleep(120);
assert.equal(runs, paused, "nothing runs while hidden");
p.setHidden(false);
await sleep(5); // far less than the interval: the catch-up is immediate
assert.equal(runs, paused + 1, "one check right away");
await sleep(60);
assert.ok(runs > paused + 1, "and it keeps going");
alive = false;
});
test("it stops for good once the screen is gone", async () => {
const p = page();
let runs = 0;
let alive = true;
keepFresh(() => alive, async () => void runs++, 20, p.doc);
await sleep(50);
alive = false;
await sleep(40);
const stopped = runs;
p.setHidden(true);
p.setHidden(false); // coming back does not revive it
await sleep(80);
assert.equal(runs, stopped);
});
test("a slow or failing check never overlaps itself and does not end the loop", async () => {
const p = page();
let active = 0;
let peak = 0;
let runs = 0;
let alive = true;
keepFresh(
() => alive,
async () => {
runs++;
active++;
peak = Math.max(peak, active);
await sleep(35); // slower than the interval
active--;
if (runs === 2) throw new Error("network down");
},
10,
p.doc,
);
await sleep(300);
alive = false;
assert.equal(peak, 1, "runs do not overlap");
assert.ok(runs >= 4, `kept going after a failure (${runs} runs)`);
});
test("the default is a few seconds, not half a minute", () => {
assert.ok(FOREGROUND_MS >= 5_000 && FOREGROUND_MS <= 10_000);
});