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
+42 -10
View File
@@ -671,6 +671,46 @@ function cellMetres(hash) {
};
}
// src/fresh.ts
var FOREGROUND_MS = 8e3;
function keepFresh(alive, run, ms = FOREGROUND_MS, doc = document) {
let timer;
let running = false;
const stop = () => {
clearTimeout(timer);
doc.removeEventListener("visibilitychange", onVisible);
};
const schedule = () => {
clearTimeout(timer);
timer = setTimeout(() => void fire(), ms);
};
const fire = async () => {
if (!alive()) return stop();
if (doc.hidden) return;
if (running) return schedule();
running = true;
try {
await run();
} catch {
} finally {
running = false;
}
if (!alive()) return stop();
schedule();
};
const onVisible = () => {
if (!alive()) return stop();
if (doc.hidden) {
clearTimeout(timer);
return;
}
clearTimeout(timer);
void fire();
};
doc.addEventListener("visibilitychange", onVisible);
schedule();
}
// node_modules/@noble/ciphers/utils.js
/*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) */
function isBytes(a) {
@@ -4317,7 +4357,6 @@ function conversationHash(author, id, secret) {
}
// src/chat.ts
var POLL_MS = 2e4;
var OVERLAP = 3600;
var app = () => document.getElementById("app");
var fromHex = (s) => Uint8Array.from(s.match(/../g) ?? [], (b) => parseInt(b, 16));
@@ -4625,9 +4664,9 @@ async function showConversation(author, id, secret) {
}
if (gen !== state.generation) return;
render(rejected, failed);
setTimeout(() => void cycle(), POLL_MS);
};
await cycle();
keepFresh(() => gen === state.generation, cycle);
}
function deleteControl(c) {
const slot = h("p", null);
@@ -4795,7 +4834,6 @@ async function renewListing(author, id, days = MAX_LIFETIME_DAYS) {
}
// src/owner.ts
var REFRESH_MS = 3e4;
var VALID_DAYS = [1, 3, 7, 14, 30];
var DEFAULT_DAYS = 7;
var app2 = () => document.getElementById("app");
@@ -5014,13 +5052,7 @@ async function showOwner(listingId, thread) {
}
renderListings();
await refresh();
const tick2 = () => {
setTimeout(() => {
if (gen !== state.generation) return;
void refresh().then(tick2);
}, REFRESH_MS);
};
tick2();
keepFresh(() => gen === state.generation && getToken() !== null, refresh);
}
var DAY2 = 86400;
var SOON_DAYS = 5;