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
3 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. // visible again instead of waiting out the rest of a stale interval.
import { useEffect, useState } from "preact/hooks"; import { useEffect, useState } from "preact/hooks";
import { fmtAgo, fmtUntil } from "@hive/shared/time.js"; import { fmtAgo, fmtUntil } from "@hive/shared/time.js";
import { msUntilTextChanges } from "./next-change.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;
// `upcoming`: the instant is expected to be ahead (a schedule's next fire), // `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 // 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()); 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(() => { 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 = () => { const start = () => {
setNow(Date.now()); setNow(Date.now());
id = setInterval(() => setNow(Date.now()), TICK_MS); schedule();
}; };
const stop = () => { const stop = () => {
if (id !== undefined) clearInterval(id); if (id !== undefined) clearTimeout(id);
id = undefined; id = undefined;
}; };
if (document.visibilityState === "visible") start(); if (document.visibilityState === "visible") start();
const onVisibility = () => { const onVisibility = () => {
stop();
if (document.visibilityState === "visible") start(); if (document.visibilityState === "visible") start();
else stop();
}; };
document.addEventListener("visibilitychange", onVisibility); document.addEventListener("visibilitychange", onVisibility);
return () => { return () => {
stop(); stop();
document.removeEventListener("visibilitychange", onVisibility); document.removeEventListener("visibilitychange", onVisibility);
}; };
}, []); }, [epochMs, upcoming]);
const ageSeconds = (now - epochMs) / 1000; const ageSeconds = (now - epochMs) / 1000;
return ( 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));
}

View file

@ -0,0 +1,37 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { msUntilTextChanges } from "../src/ui/relative-time/next-change.ts";
test("an age under a minute changes every second", () => {
assert.equal(msUntilTextChanges(0, false), 1000);
assert.equal(msUntilTextChanges(2500, false), 500);
});
test("an age in minutes changes at the next minute", () => {
assert.equal(msUntilTextChanges(60_000, false), 60_000);
assert.equal(msUntilTextChanges(90_000, false), 30_000);
});
test("an age in hours changes at the next hour", () => {
assert.equal(msUntilTextChanges(3_600_000 + 1_000, false), 3_599_000);
});
test("an age in days changes at the next day", () => {
assert.equal(msUntilTextChanges(86_400_000, false), 86_400_000);
});
test("a countdown drops a step when the time left falls below it", () => {
// 5m 30s left reads "in 5m" until 5m, then "in 4m".
assert.equal(msUntilTextChanges(-330_000, true), 30_001);
// 20.5s left reads "in 20s" until 20s.
assert.equal(msUntilTextChanges(-20_500, true), 501);
});
test("a clock ahead of an age waits for it to catch up", () => {
assert.equal(msUntilTextChanges(-4000, false), 5000);
});
test("the wait never goes under the floor", () => {
assert.equal(msUntilTextChanges(999_990, false) >= 50, true);
assert.equal(msUntilTextChanges(-10, true) >= 50, true);
});