Files
archived-orca/mobile/src/mobile-web-shell/foreground-timer.ts
T
Jinwoo Hong 60834fb1b6 fix(mobile): a failed page remounts instead of re-downloading; deadlines count only foreground time (#38868)
* fix(mobile): a failed page remounts instead of re-downloading; deadlines count only foreground time

Page deadlines now pause in the background and restart on return, so React Native firing overdue timers on resume no longer reads as a page failure. A failed document remounts the same files once, then shows the failure screen; it never deletes and re-downloads on its own. Try again after a page failure or an unreadable generation deletes the host cache first, so it downloads fresh. Replaces #34550 without its file-size check.

* fix(mobile): drop reports a remounted page's retired view already queued

A failed page remounts at once, minting the replacement's session before
React commits it. A fault or ready the old view had already queued then
landed on the replacement: a second fault showed the failure screen
before the new page loaded, and a late ready marked it ready and
cancelled its load deadline. Each report now carries the session of the
view that rendered it and is dropped once that view is retired.
2026-11-06 15:31:57 -05:00

42 lines
1.2 KiB
TypeScript

import { AppState, type AppStateStatus } from 'react-native'
function outsideForeground(state: AppStateStatus): boolean {
return state === 'background' || state === 'inactive'
}
// Restart on every return to the foreground: RN fires overdue timers on resume, before a suspended WebView has had any chance to load.
export function setForegroundTimer(run: () => void, delayMs: number): () => void {
let handle: ReturnType<typeof setTimeout> | null = null
const pause = (): void => {
if (handle !== null) {
clearTimeout(handle)
handle = null
}
}
const arm = (): void => {
handle ??= setTimeout(() => {
handle = null
// A fire queued behind a not-yet-delivered background change must wait for the next activation.
if (outsideForeground(AppState.currentState)) {
return
}
subscription.remove()
run()
}, delayMs)
}
const subscription = AppState.addEventListener('change', (state) => {
if (outsideForeground(state)) {
pause()
} else {
arm()
}
})
if (!outsideForeground(AppState.currentState)) {
arm()
}
return () => {
pause()
subscription.remove()
}
}