Skip to content

Commit

Permalink
refactor(query-core): optimization window.removeEventListener (#5475)
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jia-nan authored May 27, 2023
1 parent 6c3abf7 commit 00319a1
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/query-core/src/onlineManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ type SetupFn = (
setOnline: (online?: boolean) => void,
) => (() => void) | undefined

const onlineEvents = ['online', 'offline'] as const

export class OnlineManager extends Subscribable {
private online?: boolean
private cleanup?: () => void
Expand All @@ -19,13 +21,15 @@ export class OnlineManager extends Subscribable {
if (!isServer && window.addEventListener) {
const listener = () => onOnline()
// Listen to online
window.addEventListener('online', listener, false)
window.addEventListener('offline', listener, false)
onlineEvents.forEach((event) => {
window.addEventListener(event, listener, false)
})

return () => {
// Be sure to unsubscribe if a new handler is set
window.removeEventListener('online', listener)
window.removeEventListener('offline', listener)
onlineEvents.forEach((event) => {
window.removeEventListener(event, listener)
})
}
}

Expand Down

0 comments on commit 00319a1

Please sign in to comment.