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

FocusManager.md

199 lines3.9 KB

1---
2id: FocusManager
3title: FocusManager
4---
5​
6Defined in: [packages/query-core/src/focusManager.ts:14](https://github.com/TanStack/query/blob/main/packages/query-core/src/focusManager.ts#L14)
7​
8The `FocusManager` manages the focus state within TanStack Query.
9​
10It can be used to change the default event listeners or to manually change the focus state.
11​
12## Extends
13​
14- `Subscribable`\<`Listener`\>
15​
16## Methods
17​
18### hasListeners()
19​
20```ts
21hasListeners(): boolean;
22```
23​
24Defined in: [packages/query-core/src/subscribable.ts:41](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L41)
25​
26Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
27​
28#### Returns
29​
30`boolean`
31​
32#### Inherited from
33​
34```ts
35Subscribable.hasListeners
36```
37​
38***
39​
40### isFocused()
41​
42```ts
43isFocused(): boolean;
44```
45​
46Defined in: [packages/query-core/src/focusManager.ts:128](https://github.com/TanStack/query/blob/main/packages/query-core/src/focusManager.ts#L128)
47​
48`isFocused` can be used to get the current focus state.
49​
50#### Returns
51​
52`boolean`
53​
54***
55​
56### onFocus()
57​
58```ts
59onFocus(): void;
60```
61​
62Defined in: [packages/query-core/src/focusManager.ts:118](https://github.com/TanStack/query/blob/main/packages/query-core/src/focusManager.ts#L118)
63​
64`onFocus` notifies all subscribed listeners with the current focus state.
65​
66#### Returns
67​
68`void`
69​
70***
71​
72### setEventListener()
73​
74```ts
75setEventListener(setup: SetupFn): void;
76```
77​
78Defined in: [packages/query-core/src/focusManager.ts:77](https://github.com/TanStack/query/blob/main/packages/query-core/src/focusManager.ts#L77)
79​
80`setEventListener` can be used to set a custom event listener that will
81be used to determine the focus state. The provided `setup` function
82receives a `setFocused` callback: call it with a `boolean` to manually
83set the focus state, or with no arguments to re-evaluate the current
84focus state and notify subscribers.
85​
86#### Parameters
87​
88##### setup
89​
90`SetupFn`
91​
92#### Returns
93​
94`void`
95​
96#### Example
97​
98```ts
99import { focusManager } from '@tanstack/query-core'
100​
101focusManager.setEventListener((handleFocus) => {
102 const listener = () => handleFocus()
103 // Listen to visibilitychange
104 if (typeof window !== 'undefined' && window.addEventListener) {
105 window.addEventListener('visibilitychange', listener, false)
106 }
107​
108 return () => {
109 // Be sure to unsubscribe if a new handler is set
110 window.removeEventListener('visibilitychange', listener)
111 }
112})
113```
114​
115***
116​
117### setFocused()
118​
119```ts
120setFocused(focused?: boolean): void;
121```
122​
123Defined in: [packages/query-core/src/focusManager.ts:107](https://github.com/TanStack/query/blob/main/packages/query-core/src/focusManager.ts#L107)
124​
125`setFocused` can be used to manually set the focus state. Set `undefined`
126to fall back to the default focus check.
127​
128#### Parameters
129​
130##### focused?
131​
132`boolean`
133​
134#### Returns
135​
136`void`
137​
138#### Example
139​
140```ts
141import { focusManager } from '@tanstack/query-core'
142​
143// Set focused
144focusManager.setFocused(true)
145​
146// Set unfocused
147focusManager.setFocused(false)
148​
149// Fallback to the default focus check
150focusManager.setFocused(undefined)
151```
152​
153***
154​
155### subscribe()
156​
157```ts
158subscribe(listener: Listener): () => void;
159```
160​
161Defined in: [packages/query-core/src/subscribable.ts:27](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L27)
162​
163Registers a listener to be called on every update this object notifies about. Returns a function
164that removes the listener again β€” call it to stop listening. The base class never drops a listener
165on its own, though some subclasses clear all of theirs in `destroy()`.
166​
167#### Parameters
168​
169##### listener
170​
171`Listener`
172​
173Called on each update, with whatever the subclass passes to its subscribers.
174​
175#### Returns
176​
177```ts
178(): void;
179```
180​
181##### Returns
182​
183`void`
184​
185#### Example
186​
187```ts
188const unsubscribe = subscribable.subscribe(() => {
189 // react to the update
190})
191​
192unsubscribe()
193```
194​
195#### Inherited from
196​
197```ts
198Subscribable.subscribe
199```