Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

focusManager.ts

144 lines4.2 KB

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 // oxlint-disable-next-line typescript/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 override onSubscribe(): void {
40 if (!this.#cleanup) {
41 this.setEventListener(this.#setup)
42 }
43 }
44​
45 protected override 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 * @param setup - Receives the `setFocused` callback, registers the event listener, and may return
59 * a cleanup function that is called when the listener is replaced or no longer needed.
60 * @example
61 * ```ts
62 * import { focusManager } from '@tanstack/query-core'
63 *
64 * focusManager.setEventListener((handleFocus) => {
65 * const listener = () => handleFocus()
66 * // Listen to visibilitychange
67 * if (typeof window !== 'undefined' && window.addEventListener) {
68 * window.addEventListener('visibilitychange', listener, false)
69 * }
70 *
71 * return () => {
72 * // Be sure to unsubscribe if a new handler is set
73 * window.removeEventListener('visibilitychange', listener)
74 * }
75 * })
76 * ```
77 */
78 setEventListener(setup: SetupFn): void {
79 this.#setup = setup
80 this.#cleanup?.()
81 this.#cleanup = setup((focused) => {
82 if (typeof focused === 'boolean') {
83 this.setFocused(focused)
84 } else {
85 this.onFocus()
86 }
87 })
88 }
89​
90 /**
91 * `setFocused` can be used to manually set the focus state. Set `undefined`
92 * to fall back to the default focus check.
93 * @param focused - The focus state, or `undefined` to use the default focus check.
94 * @example
95 * ```ts
96 * import { focusManager } from '@tanstack/query-core'
97 *
98 * // Set focused
99 * focusManager.setFocused(true)
100 *
101 * // Set unfocused
102 * focusManager.setFocused(false)
103 *
104 * // Fallback to the default focus check
105 * focusManager.setFocused(undefined)
106 * ```
107 */
108 setFocused(focused?: boolean): void {
109 const changed = this.#focused !== focused
110 if (changed) {
111 this.#focused = focused
112 this.onFocus()
113 }
114 }
115​
116 /**
117 * `onFocus` notifies all subscribed listeners with the current focus state.
118 */
119 onFocus(): void {
120 const isFocused = this.isFocused()
121 this.listeners.forEach((listener) => {
122 listener(isFocused)
123 })
124 }
125​
126 /**
127 * `isFocused` can be used to get the current focus state.
128 * @returns The focus state set with `setFocused`, or otherwise whether the document is visible.
129 */
130 isFocused(): boolean {
131 if (typeof this.#focused === 'boolean') {
132 return this.#focused
133 }
134​
135 // document global can be unavailable in react native
136 // oxlint-disable-next-line typescript/no-unnecessary-condition
137 return globalThis.document?.visibilityState !== 'hidden'
138 }
140​
141/**
142 * Singleton instance of {@link FocusManager}, used to manage and observe the focus state within TanStack Query.
143 */
144export const focusManager = new FocusManager()