feat(web): drive every status dot from one shared blink clock

Each dot used its own CSS animation ("pulse" in app.css, "tree-dot-blink" in
chat.css), so dots drifted out of phase and the tree looked noisy. One
ref-counted 500ms clock in lib/blink.ts now writes data-pi-blink-fast and
data-pi-blink-slow on <html>, and CSS derives the dim phase from those
attributes: one timer for the whole app, no React re-renders, and
prefers-reduced-motion pins every dot solid.
This commit is contained in:
yuez committed 2026-10-27 18:42:54 +08:00
1 parent 48411dccd2
commit 81e7f96a64
4 files changed
+97 -36

No files matched your search

+79
View File
@@ -0,0 +1,79 @@
export const BLINK_MS = 500;
let subscribers = 0;
let timerId: number | null = null;
let tick = 0;
function updateDom(fast: "on" | "dim", slow: "on" | "dim"): void {
if (typeof document === "undefined" || !document.documentElement) return;
document.documentElement.setAttribute("data-pi-blink-fast", fast);
document.documentElement.setAttribute("data-pi-blink-slow", slow);
}
function clearDom(): void {
if (typeof document === "undefined" || !document.documentElement) return;
document.documentElement.removeAttribute("data-pi-blink-fast");
document.documentElement.removeAttribute("data-pi-blink-slow");
}
function syncDomForTick(t: number): void {
const fast = t % 2 === 0 ? "on" : "dim";
const slow = Math.floor(t / 2) % 2 === 0 ? "on" : "dim";
updateDom(fast, slow);
}
export function blinkTick(): number {
return tick;
}
export function startBlinkClock(): () => void {
subscribers++;
if (subscribers === 1) {
tick = 0;
const prefersReducedMotion =
typeof window !== "undefined" &&
typeof window.matchMedia === "function" &&
window.matchMedia("(prefers-reduced-motion: reduce)").matches;
if (prefersReducedMotion) {
updateDom("on", "on");
} else {
syncDomForTick(tick);
if (typeof window !== "undefined") {
timerId = window.setInterval(() => {
tick++;
syncDomForTick(tick);
}, BLINK_MS);
}
}
}
let unsubscribed = false;
return () => {
if (unsubscribed) return;
unsubscribed = true;
subscribers--;
if (subscribers <= 0) {
subscribers = 0;
if (timerId !== null) {
window.clearInterval(timerId);
timerId = null;
}
tick = 0;
clearDom();
}
};
}
export function resetBlinkClock(): void {
subscribers = 0;
if (timerId !== null) {
window.clearInterval(timerId);
timerId = null;
}
tick = 0;
clearDom();
}