Watch
0
0
Fork
You've already forked hyperhive
0

swarm-ui: a relative-time label wakes when its text changes, not every second

Each <RelativeTime> ran its own one-second interval and redrew each tick, though
"5m ago" holds for a minute and "3h ago" for an hour. Every label now sets a
timeout for the moment its text next changes (next-change.ts), so a list of
timestamps in minutes redraws once a minute.
This commit is contained in:
iris 2026-11-28 02:19:16 +01:00 • committed by mara
commit ad8263740b
2 changed files with 94 additions and 10 deletions

View file

@ -10,11 +10,7 @@
// visible again instead of waiting out the rest of a stale interval.
import { useEffect, useState } from "preact/hooks";
import { fmtAgo, fmtUntil } from "@hive/shared/time.js";
// Cheap at the handful of rows/chips this renders for today, and coarse
// enough to be correct without visibly ticking every frame — the same
// choice a wall clock makes.
const TICK_MS = 1000;
import { msUntilTextChanges } from "./next-change.js";
// `upcoming`: the instant is expected to be ahead (a schedule's next fire),
// so one still ahead reads "in 5m" instead of clamping to "0s ago". Off by
@ -29,29 +25,40 @@ export function RelativeTime({
}) {
const [now, setNow] = useState(() => Date.now());
// One timer per label, set for the moment its text changes (`next-change.ts`),
// not a tick a second: "5m ago" holds for a minute.
useEffect(() => {
let id: ReturnType<typeof setInterval> | undefined;
let id: ReturnType<typeof setTimeout> | undefined;
const schedule = () => {
id = setTimeout(
() => {
setNow(Date.now());
schedule();
},
msUntilTextChanges(Date.now() - epochMs, upcoming),
);
};
const start = () => {
setNow(Date.now());
id = setInterval(() => setNow(Date.now()), TICK_MS);
schedule();
};
const stop = () => {
if (id !== undefined) clearInterval(id);
if (id !== undefined) clearTimeout(id);
id = undefined;
};
if (document.visibilityState === "visible") start();
const onVisibility = () => {
stop();
if (document.visibilityState === "visible") start();
else stop();
};
document.addEventListener("visibilitychange", onVisibility);
return () => {
stop();
document.removeEventListener("visibilitychange", onVisibility);
};
}, []);
}, [epochMs, upcoming]);
const ageSeconds = (now - epochMs) / 1000;
return (

View file

@ -0,0 +1,40 @@
// When a relative-time label next needs to change. `fmtAgo` and `fmtUntil`
// (`@hive/shared/time.js`) show one unit, so "5m ago" holds for a minute and
// "3h ago" for an hour; a label that redraws every second does a thousandfold
// of the work it needs to. Kept apart from the component, with no imports, so a
// test reaches it without a DOM.
// The unit, in seconds, the label shows for a span of this many seconds.
function unitFor(seconds: number): number {
if (seconds < 60) return 1;
if (seconds < 3600) return 60;
if (seconds < 86400) return 3600;
return 86400;
}
// Never sooner than this, so a boundary a hair away can't spin the timer.
const FLOOR_MS = 50;
/**
* Milliseconds until the label's text changes. `deltaMs` is how far now is past
* the instant (negative while it is still ahead). `upcoming` is as on
* `<RelativeTime>`: an instant still ahead counts down instead of clamping to
* "0s ago".
*/
export function msUntilTextChanges(deltaMs: number, upcoming: boolean): number {
if (deltaMs < 0 && upcoming) {
// Counting down: the text drops a step when the time left falls below the
// current step's floor.
const leftMs = -deltaMs;
const unit = unitFor(Math.floor(leftMs / 1000));
const step = Math.floor(leftMs / 1000 / unit) * unit * 1000;
return Math.max(FLOOR_MS, Math.ceil(leftMs - step) + 1);
}
if (deltaMs < 0) {
// A clock ahead of the instant reads "0s ago" until it catches up.
return Math.max(FLOOR_MS, Math.ceil(-deltaMs) + 1000);
}
const unit = unitFor(Math.floor(deltaMs / 1000));
const next = (Math.floor(deltaMs / 1000 / unit) + 1) * unit * 1000;
return Math.max(FLOOR_MS, Math.ceil(next - deltaMs));
}