Skip to content

Commit 07ce2a5

Browse files
authored
fix: focus handler typings (TanStack#2152)
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 TanStack#2053
1 parent e49afc0 commit 07ce2a5

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/core/focusManager.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ class FocusManager extends Subscribable {
1212
}
1313

1414
setEventListener(
15-
setup: (onFocus: () => void) => (focused?: boolean) => void
15+
setup: (setFocused: (focused?: boolean) => void) => () => void
1616
): void {
1717
if (this.removeEventListener) {
1818
this.removeEventListener()
1919
}
20-
this.removeEventListener = setup((focused?: boolean) => {
20+
this.removeEventListener = setup((focused) => {
2121
if (typeof focused === 'boolean') {
2222
this.setFocused(focused)
2323
} else {
@@ -58,14 +58,15 @@ class FocusManager extends Subscribable {
5858
private setDefaultEventListener() {
5959
if (!isServer && window?.addEventListener) {
6060
this.setEventListener(onFocus => {
61+
const listener = () => onFocus()
6162
// Listen to visibillitychange and focus
62-
window.addEventListener('visibilitychange', onFocus, false)
63-
window.addEventListener('focus', onFocus, false)
63+
window.addEventListener('visibilitychange', listener, false)
64+
window.addEventListener('focus', listener, false)
6465

6566
return () => {
6667
// Be sure to unsubscribe if a new handler is set
67-
window.removeEventListener('visibilitychange', onFocus)
68-
window.removeEventListener('focus', onFocus)
68+
window.removeEventListener('visibilitychange', listener)
69+
window.removeEventListener('focus', listener)
6970
}
7071
})
7172
}

0 commit comments

Comments
 (0)