Files
ServerKit/frontend/src/hooks/usePolling.js
T
Juan DenisandClaude Opus 5 f21abe7af5 feat(ui): one visibility-aware polling convention, and a ratchet on the rest
Plan 76 milestone E2, drift/bug row 4. 45 hand-rolled setInterval sites across
35 files, with `visibilitychange` handled in exactly one of them and an
in-flight guard in exactly one of them — the documented poller-stampede shape.

Two failures every hand-rolled `setInterval(load, 5000)` shares:

- it starts a request every 5s whether or not the previous one returned. A slow
  endpoint does not slow the poller down, it makes it stack; each extra request
  makes the server slower, which makes the next overlap more likely. This is
  what a DevTools capture of /domains showed as a wall of pending rows.
- it keeps polling a tab nobody is looking at, where browsers throttle timers
  unevenly anyway, so it is not even a reliable refresh.

utils/pollScheduler.js holds the rules as a plain object so they are testable
without a DOM (the repo has no jsdom — this follows formState.js/useForm).
hooks/usePolling.js is the React binding, and useServerQuery gains
`refetchInterval` built on the same scheduler, so there is one implementation
rather than a second one for query users.

Two design points worth stating, since both differ from what the sites did:

- the next tick is armed by the previous run *finishing*, not by setInterval.
  With setInterval the gap is measured tick-to-tick regardless of how long the
  work took, so slow work silently turns a 5s gap into no gap. The in-flight
  guard therefore protects the out-of-band paths — refresh() and the visibility
  catch-up — rather than the schedule, and the test says so instead of implying
  a guard the schedule can no longer exercise.
- the callback is held in a ref, so an inline arrow re-created each render does
  not restart the timer. That is the usual reason a hand-rolled poller runs far
  faster than its stated interval.

Migrated four as reference implementations, per the plan's "migrate a
representative flow before broader adoption": DeployPill (which had worked all
of this out by hand and loses ~20 lines), MonitorsSummary, appdetail/LogsTab,
and ProcessListPanel. MonitorsSummary keeps its `refreshKey` prop as an explicit
out-of-band refresh rather than folding it into the schedule.

Ratchet: check-frontend-boundaries.mjs gains LEGACY_POLLERS with exact per-file
counts, now 41 sites in 31 files, so every future cleanup must shrink it and a
new bare setInterval fails lint. Genuine non-fetching timers (clock ticks,
countdowns) are listed too rather than pattern-matched away — they are harmless
but the exact count is what makes the ratchet trustworthy.

Gate: npm test (72 passed, 11 of them new), npm run lint (0 errors; 954
warnings, one fewer than the 955 baseline), npm run build clean, boundary check
green.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-18 20:45:59 -04:00

85 lines
3.0 KiB
JavaScript

import { useCallback, useEffect, useRef } from 'react';
import { createPollScheduler, DEFAULT_INTERVAL_MS } from '@/utils/pollScheduler';
// One convention for "keep this fresh while the user is looking at it".
//
// usePolling(loadStatus, 5000);
// usePolling(loadLogs, 3000, { enabled: autoRefresh });
//
// Replaces the `useEffect(() => { const t = setInterval(load, 5000); return
// () => clearInterval(t); })` block, which this codebase had ~47 of and which
// gets two things wrong every time:
//
// * it starts a new request every 5s whether or not the last one came back,
// so a slow endpoint turns into a growing pile of concurrent requests;
// * it keeps polling a tab nobody is looking at.
//
// The scheduling itself lives in utils/pollScheduler.js so it can be tested
// without a DOM; this is the React binding.
//
// `callback` is held in a ref, so an inline arrow re-created every render does
// NOT restart the timer — the usual reason a hand-rolled poller silently polls
// far faster than its stated interval.
//
// Returns a stable `refresh()` for "reload now" buttons, which goes through the
// same in-flight guard.
export function usePolling(callback, intervalMs = DEFAULT_INTERVAL_MS, options = {}) {
const {
enabled = true,
immediate = true,
pauseWhenHidden = true,
onError,
} = options;
const callbackRef = useRef(callback);
const onErrorRef = useRef(onError);
callbackRef.current = callback;
onErrorRef.current = onError;
const schedulerRef = useRef(null);
useEffect(() => {
if (!enabled || !intervalMs) return undefined;
const scheduler = createPollScheduler({
run: () => callbackRef.current?.(),
intervalMs,
setTimer: (fn, ms) => window.setTimeout(fn, ms),
clearTimer: (handle) => window.clearTimeout(handle),
isHidden: () => (
pauseWhenHidden
&& typeof document !== 'undefined'
&& document.visibilityState === 'hidden'
),
onError: (error) => onErrorRef.current?.(error),
});
schedulerRef.current = scheduler;
scheduler.start({ runNow: immediate });
let onVisibility;
if (pauseWhenHidden && typeof document !== 'undefined') {
onVisibility = () => scheduler.setHidden(document.visibilityState === 'hidden');
document.addEventListener('visibilitychange', onVisibility);
}
return () => {
if (onVisibility) document.removeEventListener('visibilitychange', onVisibility);
scheduler.stop();
schedulerRef.current = null;
};
}, [enabled, intervalMs, immediate, pauseWhenHidden]);
return useCallback(() => {
const scheduler = schedulerRef.current;
if (scheduler) {
scheduler.refresh();
return;
}
// Disabled or unmounted: a manual refresh should still do the work.
callbackRef.current?.();
}, []);
}
export default usePolling;