Repository navigation
Description
Context
Several timers in ui-react keep running while the installed app is hidden, or duplicate a shared ticker. Android resumes a backgrounded PWA rather than relaunching it, so the waste adds up over hours, and a return to the foreground waits for the next tick instead of refreshing at once.
- Connectivity poller.
useVpnStatusreschedules every 15 s, or every 3 s when the server is unreachable, whether the page is hidden or the device offline (packages/ui-react/src/hooks/use-vpn-status.ts:105-120, intervals at:9-12). It already re-checks ononlineand on becoming visible (:122-131), so hidden polling buys nothing. Each check builds anAbortControllerwith a manualsetTimeout/clearTimeoutpair (fetchVpnStatus,use-vpn-status.ts:27-44) thatAbortSignal.timeout()replaces. - Inbox badge poller.
startInboxPollingloads the inbox every 60 s for the page's lifetime, hidden or not, and does not refresh when the page returns (packages/ui-react/src/stores/activity-state.ts:226-235, cleared at:315-316). The socket's liveness handling already shows the pattern to copy:visibilitychange,resume,freezeandpageshow(packages/ui-react/src/connection.ts:916-947). - Elapsed tickers.
TurnStatus(packages/ui-react/src/components/chat/turn-status.tsx:26-32) andRunLanes(packages/ui-react/src/components/activity/run-lanes.tsx:54-61) each own a 1 ssetInterval, althoughuseNow(packages/ui-react/src/hooks/use-now.ts:13-32) exists to share one ticker between all such labels and is already used by four components. Its one shared interval takes the cadence of whichever subscriber mounted first (use-now.ts:17-21), and two existing consumers ask for 30-second updates (AGE_TICK_MS:packages/ui-react/src/components/chat/composer-row.tsx:68,packages/ui-react/src/components/chat/session-drawer.tsx:56). Moving the 1 s elapsed labels onto it unchanged would make them tick every 30 s whenever a 30 s consumer mounted first. - Transcription sync. Every
RecordingItemin thetranscribingstate starts its own 2 s interval callingrecordings.syncTranscriptions()(packages/ui-react/src/components/voice/recordings-tray.tsx:145-149), so three transcribing rows make three calls every 2 s for the same root-wide sync. - Storage guidance.
createEvidencetimes its request out with a manualsetTimeout(() => request.abort(), 10_000)(packages/ui-react/src/components/settings/storage/use-storage-guidance.ts:53) beside an abort controller thatclear()also aborts.
Guide: modern-web-guidance performance: "DON'T block the thread with recurring timers: Avoid heavy polling with setInterval that starves the main thread."
Support (MDN BCD 8.1.6): AbortSignal.timeout is in Chrome for Android 124, Samsung Internet 27 and Firefox for Android 100; AbortSignal.any in 116, 24 and 124. freeze/resume are Chromium-only (Chrome for Android 68, Samsung Internet 10, not in Firefox), so visibilitychange stays the baseline and they are additive listeners only.
Binding: docs/decisions/session-principals.md:114 cites ui-react/src/hooks/use-vpn-status.ts:36 (res.status === 401). A PR that moves that line updates the citation (tests/decision-citations.test.ts).
Scope
useVpnStatus: do not schedule the next check whiledocument.visibilityState === "hidden"ornavigator.onLine === false. The existingvisibilitychangeandonlinehandlers start it again. Replace the manual timeout infetchVpnStatuswithAbortSignal.timeout(TIMEOUT_MS).activity-state.ts: stop the inbox interval on hidden and onfreeze; on visible,resumeorpageshow, load the inbox once and restart the interval. Remove the listeners when the store is disposed.useNow: add an optionalenabledargument (defaulttrue). Replace the single first-subscriber interval with one shared interval per distinct cadence: subscribers that ask for the sameintervalMsshare one timer, and each subscriber is driven only by its own cadence, so a 30 s consumer never re-renders every second and a 1 s consumer never waits 30 s, whatever the mount order. A cadence's interval exists only while it has at least one enabled subscriber and is cleared with its last one. When the page is hidden every cadence's interval stops; on becoming visible each active cadence ticks once immediately and resumes.TurnStatusandRunLanesuseuseNow(1000, measured)anduseNow(1000, running)instead of their own intervals.RecordingsTray: one interval for the tray, running while any listed row istranscribingand the existing per-row conditions (!busy && !offline && !localOnly) hold for at least one such row, paused while hidden.RecordingItemno longer starts one.use-storage-guidance.ts: passAbortSignal.any([request.signal, AbortSignal.timeout(10_000)])to both calls and drop the manual timer.
Out of scope
- Changing any interval length, the inbox API or the connectivity semantics (
connected,unauthorized,unreachable). connection.ts's socket liveness, which is already visibility-aware.fetchpriority hints (the modern-web-guidancedeprioritize-background-fetchesguide):api.activityInbox()takes no request options, and adding them is a separate change.- Background Sync or Periodic Background Sync.
- Other
setTimeoutuses that are one-shot (focus moves, debounces).
Acceptance criteria
- With the page hidden, no
/api/vpn-checkrequest is made after the in-flight one settles; becoming visible makes one immediately. Same fornavigator.onLine === falseandonline. A fake-timer test fails before the change. - With the page hidden, the inbox poller makes no request; becoming visible makes one immediately and resumes the 60 s cadence. A
freezeevent stops it in the same way. Tested, failing before the change. -
useNowmixed cadence, with fake timers, each failing on the current tree where it applies: a 30 s subscriber mounted before a 1 s subscriber leaves the 1 s subscriber updating every second and the 30 s one every 30 s; unmounting the 1 s subscriber leaves the 30 s cadence intact and clears the 1 s interval; unmounting the last subscriber leaves no timer;enabled: falsesubscribes nothing; hiding the page stops all cadences, and showing it ticks each active cadence once and resumes both at their own rates. -
git grep -n "setInterval" packages/ui-react/src/components/chat/turn-status.tsx packages/ui-react/src/components/activity/run-lanes.tsxreturns nothing, and their elapsed labels still advance each second while visible (existing tests or a new render test). - With three transcribing rows,
syncTranscriptionsis called once per 2 s, not three times. Tested. - A request that times out still reads as
unreachable(VPN) or a failed guidance load, andclear()still aborts an in-flight guidance request. - Every listener added here is removed on unmount or dispose; a test asserts no timer remains after dispose.
- Firefox for Android (no
freeze/resume) pauses and resumes throughvisibilitychangealone.
Verification
bun run test packages/ui-react/tests
bun run typecheck
bun run lintContract impact
- This touches the integration contract, so it needs the
contractlabel, aCONTRACT:commit prefix, and the doc updated in the same commit. - This changes a published package's behaviour, so it needs a changeset.
Not contract: no exported signature changes (useNow is internal, not exported from src/index.ts).
2026-10-24: specified useNow's cadence selection (one shared interval per distinct cadence) and its mixed-cadence tests, because the shared ticker took the first subscriber's interval and existing 30 s consumers would have slowed the 1 s elapsed labels.
Activity