Skip to content

Commit

Permalink
fix(core): pass isFocused boolean to focusManager callback (#7066)
Browse files Browse the repository at this point in the history
  • Loading branch information
TkDodo committed Mar 11, 2024
1 parent 181e9d2 commit 2819dcd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions packages/query-core/src/focusManager.ts
@@ -1,11 +1,13 @@
import { Subscribable } from './subscribable'
import { isServer } from './utils'

type Listener = (focused: boolean) => void

type SetupFn = (
setFocused: (focused?: boolean) => void,
) => (() => void) | undefined

export class FocusManager extends Subscribable {
export class FocusManager extends Subscribable<Listener> {
#focused?: boolean
#cleanup?: () => void

Expand Down Expand Up @@ -64,8 +66,9 @@ export class FocusManager extends Subscribable {
}

onFocus(): void {
const isFocused = this.isFocused()
this.listeners.forEach((listener) => {
listener()
listener(isFocused)
})
}

Expand Down
4 changes: 2 additions & 2 deletions packages/query-core/src/queryClient.ts
Expand Up @@ -75,8 +75,8 @@ export class QueryClient {
this.#mountCount++
if (this.#mountCount !== 1) return

this.#unsubscribeFocus = focusManager.subscribe(() => {
if (focusManager.isFocused()) {
this.#unsubscribeFocus = focusManager.subscribe((focused) => {
if (focused) {
this.resumePausedMutations()
this.#queryCache.onFocus()
}
Expand Down
3 changes: 3 additions & 0 deletions packages/query-core/src/tests/focusManager.test.tsx
Expand Up @@ -146,15 +146,18 @@ describe('focusManager', () => {
focusManager.setFocused(true)

expect(listener).toHaveBeenCalledTimes(1)
expect(listener).toHaveBeenNthCalledWith(1, true)

focusManager.setFocused(false)
focusManager.setFocused(false)

expect(listener).toHaveBeenCalledTimes(2)
expect(listener).toHaveBeenNthCalledWith(2, false)

focusManager.setFocused(undefined)
focusManager.setFocused(undefined)

expect(listener).toHaveBeenCalledTimes(3)
expect(listener).toHaveBeenNthCalledWith(3, true)
})
})

0 comments on commit 2819dcd

Please sign in to comment.