Skip to content

Commit

Permalink
fix: onWindowFocus - returned stop function does not clear visibility…
Browse files Browse the repository at this point in the history
…change listener
  • Loading branch information
GreatAuk committed Feb 26, 2024
1 parent 4f87c65 commit 0c8ff48
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/core/src/onWindowFocus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ import { debounce } from './vendor'
export function onWindowFocus(callback: (...args: any[]) => any) {
const listener = debounce(100, callback)

window.addEventListener('focus', listener, false)
window.addEventListener('visibilitychange', () => {
const visibilitychangeListener = () => {
if (document.visibilityState === 'visible')
listener()
}, false)
}

window.addEventListener('focus', listener, false)
window.addEventListener('visibilitychange', visibilitychangeListener, false)

return () => {
window.removeEventListener('focus', listener)
window.removeEventListener('visibilitychange', listener)
window.removeEventListener('visibilitychange', visibilitychangeListener)
}
}

0 comments on commit 0c8ff48

Please sign in to comment.