Electron tray app - reliably fetch in background after device suspended #10224
-
|
Hi all! I've been working on refactoring two existing tray/menubar Electron applications to use tanstack query. These apps run mostly in the background / hidden state. When configuring useQuery to That said, I've been having edge-case issues when waking devices that have hibernated for an extended period (ie: overnight). The background interval fetching seems to stop, only resuming after the first interaction with the tray/menubar app (note, i also have Any tips would be welcomed. For the time being, i've reverted to using an out-of-band interval polling function that calls refetch |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
The gap here is between browser timer APIs and OS-level suspend/resume. The cleanest fix for Electron: use // main process
powerMonitor.on('resume', () => {
BrowserWindow.getAllWindows().forEach(win => win.webContents.send('power:resume'))
})
// renderer hook
function useRefetchOnResume() {
const queryClient = useQueryClient()
useEffect(() => {
return window.electronPower?.onResume(() => {
queryClient.refetchQueries({ type: 'active' })
})
}, [queryClient])
}You can also wire this into TanStack Query's A simpler alternative if you don't want to wire IPC: timer drift detection. Run a 5-second |
Beta Was this translation helpful? Give feedback.
The gap here is between browser timer APIs and OS-level suspend/resume.
refetchIntervalusessetIntervalunder the hood, and when the OS suspends, the JS event loop freezes. On resume,setIntervaldoesn't retroactively fire for missed ticks. AndrefetchOnWindowFocusrelies onvisibilitychange, which never fires for a tray app that was never "hidden."The cleanest fix for Electron: use
powerMonitor.on('resume')in the main process, IPC it to the renderer, and callqueryClient.refetchQueries({ type: 'active' }). That's the OS-level truth; no heuristics, no drift detection.