perf(webui): stop login-waves rAF under reduced-motion; gate deliveries poll on visibility (#185)

login-waves ran its requestAnimationFrame loop unconditionally, ignoring
reduced-motion. Honour it (OS `prefers-reduced-motion` or the app's
`data-reduce-motion`): when reduced, paint a single static frame and never start
the loop; otherwise animate. React live to OS preference changes, and repaint the
static frame on resize while reduced so the field doesn't blank out. The loop is
torn down on unmount as before.

deliveries-widget polled /api/notifications/recent every 15s regardless of tab
visibility. Skip the poll while `document.hidden`, refresh immediately on
becoming visible, and add an in-flight guard so a slow request can't stack
overlapping polls.
This commit is contained in:
DrSmoothl
2026-07-07 22:34:07 +08:00
parent d984833914
commit fc424ab740
2 changed files with 33 additions and 4 deletions
+19 -2
View File
@@ -89,6 +89,10 @@ export function LoginWaves() {
let lines: Pt[][] = [];
let paths: SVGPathElement[] = [];
let raf = 0;
// Honour reduced-motion (the OS setting or the app's own data-reduce-motion
// toggle): draw a single static frame instead of running the rAF loop. #185.
const prefersReduced = window.matchMedia('(prefers-reduced-motion: reduce)');
const reduced = () => prefersReduced.matches || document.documentElement.dataset.reduceMotion === '1';
const setSize = () => {
bounding = host.getBoundingClientRect();
@@ -143,7 +147,7 @@ export function LoginWaves() {
const t = e.touches[0];
if (t) updateMouse(t.clientX, t.clientY);
};
const onResize = () => { setSize(); setLines(); };
const onResize = () => { setSize(); setLines(); if (reduced()) { movePoints(0); drawLines(); } };
const movePoints = (time: number) => {
lines.forEach((points) => {
@@ -213,10 +217,23 @@ export function LoginWaves() {
window.addEventListener('resize', onResize);
window.addEventListener('mousemove', onMouseMove);
window.addEventListener('touchmove', onTouchMove, { passive: true });
raf = requestAnimationFrame(tick);
// Animate only when motion is allowed; otherwise paint one static frame.
// React live to changes of the OS reduced-motion preference.
const applyMotion = () => {
if (reduced()) {
if (raf) { cancelAnimationFrame(raf); raf = 0; }
movePoints(0);
drawLines();
} else if (!raf) {
raf = requestAnimationFrame(tick);
}
};
prefersReduced.addEventListener('change', applyMotion);
applyMotion();
return () => {
cancelAnimationFrame(raf);
prefersReduced.removeEventListener('change', applyMotion);
window.removeEventListener('resize', onResize);
window.removeEventListener('mousemove', onMouseMove);
window.removeEventListener('touchmove', onTouchMove);
@@ -18,7 +18,12 @@ export function DeliveriesWidget() {
useEffect(() => {
let active = true;
const pull = () =>
let inFlight = false;
const pull = () => {
// Skip while the tab is backgrounded, or if a request is still in flight
// (a slow endpoint must not stack overlapping polls). #185.
if (inFlight || document.hidden) return;
inFlight = true;
api.notifications
.recent(50)
.then((r) => {
@@ -26,12 +31,19 @@ export function DeliveriesWidget() {
})
.catch(() => {
if (active) setRecords((prev) => prev ?? []);
});
})
.finally(() => { inFlight = false; });
};
void pull();
const id = window.setInterval(() => void pull(), POLL_MS);
// The interval keeps firing but no-ops while hidden, so refresh immediately
// when the tab becomes visible again to shed any staleness.
const onVisible = () => { if (!document.hidden) pull(); };
document.addEventListener('visibilitychange', onVisible);
return () => {
active = false;
window.clearInterval(id);
document.removeEventListener('visibilitychange', onVisible);
};
}, [api]);