fix(mobile): pause session polling in background (#9875)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Neil
2026-07-22 14:12:59 -07:00
committed by GitHub
co-authored by Orca
parent 4468d54f3c
commit 76f5b8318c
2 changed files with 37 additions and 6 deletions
+17 -6
View File
@@ -2706,14 +2706,25 @@ export default function SessionScreen() {
if (connState !== 'connected') {
return
}
void fetchSessionTabs()
void fetchTerminals()
// Why: live subscription keeps stream ownership, but the fallback list poll should stop while this route is hidden.
const interval = setInterval(() => {
const refreshOnForeground = () => {
if (AppState.currentState !== 'active') {
return
}
void fetchSessionTabs()
void fetchTerminals()
}, 2000)
return () => clearInterval(interval)
}
const appStateSubscription = AppState.addEventListener('change', (state) => {
if (state === 'active') {
refreshOnForeground()
}
})
// Why: live subscription keeps stream ownership, but the fallback list poll should stop while this route is hidden or backgrounded.
const interval = setInterval(refreshOnForeground, 2000)
refreshOnForeground()
return () => {
clearInterval(interval)
appStateSubscription.remove()
}
}, [connState, fetchSessionTabs, fetchTerminals])
)
@@ -29,6 +29,26 @@ describe('mobile session startup', () => {
expect(autoCreateEffect).toContain('void handleCreateTerminal()')
})
it('stops fallback list polling in the background and reconciles on resume', () => {
const pollEffect = sliceBetween(
"const refreshOnForeground = () => {\n if (AppState.currentState !== 'active')",
'// Why: pick up Settings → Terminal text size on return'
)
expect(pollEffect).toContain(
"if (AppState.currentState !== 'active') {\n return\n }\n void fetchSessionTabs()"
)
expect(pollEffect).toContain('void fetchTerminals()')
expect(pollEffect).toContain("AppState.addEventListener('change'")
expect(pollEffect).toContain("if (state === 'active') {\n refreshOnForeground()")
expect(pollEffect).toContain('const interval = setInterval(refreshOnForeground, 2000)')
expect(pollEffect.lastIndexOf('\n refreshOnForeground()')).toBeGreaterThan(
pollEffect.indexOf("AppState.addEventListener('change'")
)
expect(pollEffect).toContain('clearInterval(interval)')
expect(pollEffect).toContain('appStateSubscription.remove()')
})
it('loads session tabs without waiting for desktop activation', () => {
const startupEffect = sliceBetween(
'void (async () => {',