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:
+2
-2
@@ -4,6 +4,7 @@
|
||||
|
||||
import { HttpError, getListing, pollMailbox, postMessage } from "./api.ts";
|
||||
import { append, clear, h, sendOnCtrlEnter } from "./dom.ts";
|
||||
import { keepFresh } from "./fresh.ts";
|
||||
import { date } from "./format.ts";
|
||||
import { t, type Key } from "./i18n.ts";
|
||||
import {
|
||||
@@ -33,7 +34,6 @@ import {
|
||||
} from "./threads.ts";
|
||||
import { verifyListing, type Listing } from "./verify.ts";
|
||||
|
||||
const POLL_MS = 20_000;
|
||||
/** Re-read this much of the mailbox each time, so a clock that is a little off loses nothing. */
|
||||
const OVERLAP = 3600;
|
||||
const app = () => document.getElementById("app")!;
|
||||
@@ -375,9 +375,9 @@ export async function showConversation(author: string, id: string, secret: strin
|
||||
}
|
||||
if (gen !== state.generation) return;
|
||||
render(rejected, failed);
|
||||
setTimeout(() => void cycle(), POLL_MS);
|
||||
};
|
||||
await cycle();
|
||||
keepFresh(() => gen === state.generation, cycle);
|
||||
}
|
||||
|
||||
function deleteControl(c: Conversation): HTMLElement {
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
// Keeps a screen up to date without a live connection: runs `run` every `ms`
|
||||
// while the page is visible, pauses while it is in a background tab, and runs
|
||||
// at once when the tab comes back. There is no push channel (messages are
|
||||
// relayed through the network and fetched), so this is as live as it gets.
|
||||
|
||||
/** How often an open screen checks for news, in milliseconds. */
|
||||
export const FOREGROUND_MS = 8_000;
|
||||
|
||||
/**
|
||||
* Calls `run` every `ms` while `alive()` is true and `doc` is visible. The first
|
||||
* call is the caller's job (do it before calling this); the next one happens
|
||||
* after `ms`. Runs never overlap. Stops for good once `alive()` is false.
|
||||
*/
|
||||
export function keepFresh(alive: () => boolean, run: () => Promise<void>, ms = FOREGROUND_MS, doc: Document = document): void {
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
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; // paused: becoming visible resumes it
|
||||
if (running) return schedule();
|
||||
running = true;
|
||||
try {
|
||||
await run();
|
||||
} catch {
|
||||
// A failed check is the caller's to show; the next one still happens.
|
||||
} 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();
|
||||
}
|
||||
+3
-8
@@ -8,13 +8,13 @@
|
||||
|
||||
import { HttpError } from "./api.ts";
|
||||
import { append, clear, h, sendOnCtrlEnter } from "./dom.ts";
|
||||
import { keepFresh } from "./fresh.ts";
|
||||
import { date, price, when } from "./format.ts";
|
||||
import { t, type Key } from "./i18n.ts";
|
||||
import * as o from "./ownerApi.ts";
|
||||
import { nowSeconds, state } from "./state.ts";
|
||||
import type { Listing } from "./verify.ts";
|
||||
|
||||
const REFRESH_MS = 30_000;
|
||||
const VALID_DAYS = [1, 3, 7, 14, 30];
|
||||
const DEFAULT_DAYS = 7;
|
||||
const app = () => document.getElementById("app")!;
|
||||
@@ -274,13 +274,8 @@ export async function showOwner(listingId?: string, thread?: string): Promise<vo
|
||||
|
||||
renderListings();
|
||||
await refresh();
|
||||
const tick = () => {
|
||||
setTimeout(() => {
|
||||
if (gen !== state.generation) return;
|
||||
void refresh().then(tick);
|
||||
}, REFRESH_MS);
|
||||
};
|
||||
tick();
|
||||
// Signed out (or a rejected token) ends the checks, so the sign-in form is not redrawn under the user.
|
||||
keepFresh(() => gen === state.generation && o.getToken() !== null, refresh);
|
||||
}
|
||||
|
||||
// --- pieces ------------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user