Open History to see the last commit
1import { Subscribable } from './subscribable'
2β
3type Listener = (focused: boolean) => void
4β
5type SetupFn = (
6 setFocused: (focused?: boolean) => void,
7) => (() => void) | undefined
8β
9/**
10 * The `FocusManager` manages the focus state within TanStack Query.
11 *
12 * It can be used to change the default event listeners or to manually change the focus state.
13 */
14export class FocusManager extends Subscribable<Listener> {
15 #focused?: boolean
16 #cleanup?: () => void
17β
18 #setup: SetupFn
19β
20 constructor() {
21 super()
22 this.#setup = (onFocus) => {
23 // addEventListener does not exist in React Native, but window does
24 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
25 if (typeof window !== 'undefined' && window.addEventListener) {
26 const listener = () => onFocus()
27 // Listen to visibilitychange
28 window.addEventListener('visibilitychange', listener, false)
29β
30 return () => {
31 // Be sure to unsubscribe if a new handler is set
32 window.removeEventListener('visibilitychange', listener)
33 }
34 }
35 return
36 }
37 }
38β
39 protected onSubscribe(): void {
40 if (!this.#cleanup) {
41 this.setEventListener(this.#setup)
42 }
43 }
44β
45 protected onUnsubscribe() {
46 if (!this.hasListeners()) {
47 this.#cleanup?.()
48 this.#cleanup = undefined
49 }
50 }
51β
52 /**
53 * `setEventListener` can be used to set a custom event listener that will
54 * be used to determine the focus state. The provided `setup` function
55 * receives a `setFocused` callback: call it with a `boolean` to manually
56 * set the focus state, or with no arguments to re-evaluate the current
57 * focus state and notify subscribers.
58 *
59 * @example
60 * ```ts
61 * import { focusManager } from '@tanstack/query-core'
62 *
63 * focusManager.setEventListener((handleFocus) => {
64 * const listener = () => handleFocus()
65 * // Listen to visibilitychange
66 * if (typeof window !== 'undefined' && window.addEventListener) {
67 * window.addEventListener('visibilitychange', listener, false)
68 * }
69 *
70 * return () => {
71 * // Be sure to unsubscribe if a new handler is set
72 * window.removeEventListener('visibilitychange', listener)
73 * }
74 * })
75 * ```
76 */
77 setEventListener(setup: SetupFn): void {
78 this.#setup = setup
79 this.#cleanup?.()
80 this.#cleanup = setup((focused) => {
81 if (typeof focused === 'boolean') {
82 this.setFocused(focused)
83 } else {
84 this.onFocus()
85 }
86 })
87 }
88β
89 /**
90 * `setFocused` can be used to manually set the focus state. Set `undefined`
91 * to fall back to the default focus check.
92 *
93 * @example
94 * ```ts
95 * import { focusManager } from '@tanstack/query-core'
96 *
97 * // Set focused
98 * focusManager.setFocused(true)
99 *
100 * // Set unfocused
101 * focusManager.setFocused(false)
102 *
103 * // Fallback to the default focus check
104 * focusManager.setFocused(undefined)
105 * ```
106 */
107 setFocused(focused?: boolean): void {
108 const changed = this.#focused !== focused
109 if (changed) {
110 this.#focused = focused
111 this.onFocus()
112 }
113 }
114β
115 /**
116 * `onFocus` notifies all subscribed listeners with the current focus state.
117 */
118 onFocus(): void {
119 const isFocused = this.isFocused()
120 this.listeners.forEach((listener) => {
121 listener(isFocused)
122 })
123 }
124β
125 /**
126 * `isFocused` can be used to get the current focus state.
127 */
128 isFocused(): boolean {
129 if (typeof this.#focused === 'boolean') {
130 return this.#focused
131 }
132β
133 // document global can be unavailable in react native
134 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
135 return globalThis.document?.visibilityState !== 'hidden'
136 }
137}
138β
139/**
140 * Singleton instance of {@link FocusManager}, used to manage and observe the focus state within TanStack Query.
141 */
142export const focusManager = new FocusManager()