Skip to content

perf(web): pause snapshot polling in hidden tabs - #249

Merged
BaaRai merged 1 commit into
mainfrom
perf/visible-polling
Nov 15, 2026
Merged

BaaRai merged 1 commit into
mainfrom
perf/visible-polling

Conversation

@BaaRai

@BaaRai BaaRai commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Backported from #209 (contributed by @renaesop).

Base: main at 2ad8ba1.

Change

  • web/src/lib/polling.ts (new): setVisibleInterval(callback, delay), as in Reduce task-tab polling traffic and add online query indexes #209.
    • The next tick is scheduled with setTimeout only after the previous poll completes.
    • There are no ticks while document.visibilityState === "hidden", and there is an immediate refresh when the tab becomes visible again.
    • No overlap: a timer or visibility change while a poll is still running does not start a second one.
    • Errors are swallowed; the next interval retries.
    • The returned cleanup stops the polling and removes the visibilitychange listener.
  • web/scripts/polling.test.mjs (new): the three polling tests from Reduce task-tab polling traffic and add online query indexes #209. They transpile the helper with the typescript package and run it in vm, with a fake document and fake timers. The tests cover:
    • hidden tab, return and cleanup;
    • no overlap and no revival after dispose;
    • a failed poll retries.
  • web/package.json: adds "test:polling": "node --test scripts/polling.test.mjs".
  • CI: the web job runs npm run test:polling after npx tsc --noEmit and before npm run build:static.

Adoption

Every poller that calls the API now uses setVisibleInterval. The initial call stays immediate, and the useEffect returns or calls the cleanup.

File Pollers
dashboard/page.tsx 3
function/findings/page.tsx 2
function/tasks/detail/page.tsx 2
function/tasks/page.tsx 2 (task list, archive list)
_tabs/sessions-tab.tsx 6
_tabs/graph-tab.tsx, findings-tab.tsx, assets-tab.tsx, overview-tab.tsx, credentials-tab.tsx, broadcast-tab.tsx 1 each
system/llm/page.tsx, _components/sidebar/nav-main.tsx, components/approval-records.tsx, hooks/use-side-questions.ts, function/traffic/page.tsx 1 each

Local clock ticks keep setInterval, because they make no HTTP call: tasks/page.tsx (setNowSec), sessions-tab.tsx (setTick) and broadcast-tab.tsx (setNow). SSE streams are untouched.

The rest of #209 is not included: inflight.ts, session pagination, the partial graph and the snapshot restructuring. The two InflightReads tests from #209's test file are therefore left out too.

Verification (node v24.19.0, npm 11.19.0)

  • npm ci: OK.
  • npx tsc --noEmit: OK.
  • npm run test:polling: 3 tests, 3 pass, 0 fail.
  • npm run build:static: OK.

Adds setVisibleInterval (web/src/lib/polling.ts): no ticks while the tab is hidden, an immediate refresh on return, no overlapping polls, errors swallowed, cleanup removes the listener. API pollers use it; local clock ticks keep setInterval. CI runs the helper tests. Backported from #209 (contributed by @renaesop).
@BaaRai
BaaRai merged commit be317a1 into main Nov 15, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant