Skip to content

Commit

Permalink
fix: focus handler typings (#2152)
Browse files Browse the repository at this point in the history
Typings did not allow passing a function taking `setFocused` to addEventListener

Fixing by using the same typings as in `onlineManager.ts`, same behavior as set in #2053
  • Loading branch information
fmauquie committed Apr 24, 2021
1 parent e49afc0 commit 07ce2a5
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/core/focusManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class FocusManager extends Subscribable {
}

setEventListener(
setup: (onFocus: () => void) => (focused?: boolean) => void
setup: (setFocused: (focused?: boolean) => void) => () => void
): void {
if (this.removeEventListener) {
this.removeEventListener()
}
this.removeEventListener = setup((focused?: boolean) => {
this.removeEventListener = setup((focused) => {
if (typeof focused === 'boolean') {
this.setFocused(focused)
} else {
Expand Down Expand Up @@ -58,14 +58,15 @@ class FocusManager extends Subscribable {
private setDefaultEventListener() {
if (!isServer && window?.addEventListener) {
this.setEventListener(onFocus => {
const listener = () => onFocus()
// Listen to visibillitychange and focus
window.addEventListener('visibilitychange', onFocus, false)
window.addEventListener('focus', onFocus, false)
window.addEventListener('visibilitychange', listener, false)
window.addEventListener('focus', listener, false)

return () => {
// Be sure to unsubscribe if a new handler is set
window.removeEventListener('visibilitychange', onFocus)
window.removeEventListener('focus', onFocus)
window.removeEventListener('visibilitychange', listener)
window.removeEventListener('focus', listener)
}
})
}
Expand Down

1 comment on commit 07ce2a5

@vercel
Copy link

@vercel vercel bot commented on 07ce2a5 Apr 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.