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

focusManager.ts

152 lines4.3 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
13 * the focus state.
14 */
15export class FocusManager extends Subscribable<Listener> {
16 #focused?: boolean
17 #cleanup?: () => void
18​
19 #setup: SetupFn
20​
21 constructor() {
22 super()
23 this.#setup = (onFocus) => {
24 // addEventListener does not exist in React Native, but window does
25 // oxlint-disable-next-line typescript/no-unnecessary-condition
26 if (typeof window !== 'undefined' && window.addEventListener) {
27 const listener = () => onFocus()
28 // Listen to visibilitychange
29 window.addEventListener('visibilitychange', listener, false)
30​
31 return () => {
32 // Be sure to unsubscribe if a new handler is set
33 window.removeEventListener('visibilitychange', listener)
34 }
35 }
36 return
37 }
38 }
39​
40 protected override onSubscribe(): void {
41 if (!this.#cleanup) {
42 this.setEventListener(this.#setup)
43 }
44 }
45​
46 protected override onUnsubscribe() {
47 if (!this.hasListeners()) {
48 this.#cleanup?.()
49 this.#cleanup = undefined
50 }
51 }
52​
53 /**
54 * `setEventListener` can be used to set a custom event listener that will be
55 * used to determine the focus state. The provided `setup` function receives a
56 * `setFocused` callback: call it with a `boolean` to manually set the focus
57 * state, or with no arguments to re-evaluate the current focus state and
58 * notify subscribers.
59 *
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 * @param setup - Receives the `setFocused` callback, registers the event
79 * listener, and may return a cleanup function that is called when the
80 * listener is replaced or no longer needed.
81 */
82 setEventListener(setup: SetupFn): void {
83 this.#setup = setup
84 this.#cleanup?.()
85 this.#cleanup = setup((focused) => {
86 if (typeof focused === 'boolean') {
87 this.setFocused(focused)
88 } else {
89 this.onFocus()
90 }
91 })
92 }
93​
94 /**
95 * `setFocused` can be used to manually set the focus state. Set `undefined`
96 * to fall back to the default focus check.
97 *
98 * @example
99 * ;```ts
100 * import { focusManager } from '@tanstack/query-core'
101 *
102 * // Set focused
103 * focusManager.setFocused(true)
104 *
105 * // Set unfocused
106 * focusManager.setFocused(false)
107 *
108 * // Fallback to the default focus check
109 * focusManager.setFocused(undefined)
110 * ```
111 *
112 * @param focused - The focus state, or `undefined` to use the default focus
113 * check.
114 */
115 setFocused(focused?: boolean): void {
116 const changed = this.#focused !== focused
117 if (changed) {
118 this.#focused = focused
119 this.onFocus()
120 }
121 }
122​
123 /** `onFocus` notifies all subscribed listeners with the current focus state. */
124 onFocus(): void {
125 const isFocused = this.isFocused()
126 this.listeners.forEach((listener) => {
127 listener(isFocused)
128 })
129 }
130​
131 /**
132 * `isFocused` can be used to get the current focus state.
133 *
134 * @returns The focus state set with `setFocused`, or otherwise whether the
135 * document is visible.
136 */
137 isFocused(): boolean {
138 if (typeof this.#focused === 'boolean') {
139 return this.#focused
140 }
141​
142 // document global can be unavailable in react native
143 // oxlint-disable-next-line typescript/no-unnecessary-condition
144 return globalThis.document?.visibilityState !== 'hidden'
145 }
147​
148/**
149 * Singleton instance of {@link FocusManager}, used to manage and observe the
150 * focus state within TanStack Query.
151 */
152export const focusManager = new FocusManager()