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

OnlineManager.md

184 lines3.7 KB

1---
2id: OnlineManager
3title: OnlineManager
4redirect_from:
5 - reference/onlineManager
6 - framework/react/reference/onlineManager
7---
8​
9Defined in: [packages/query-core/src/onlineManager.ts:15](https://github.com/TanStack/query/blob/main/packages/query-core/src/onlineManager.ts#L15)
10​
11The `OnlineManager` manages the online state within TanStack Query. It can
12be used to change the default event listeners or to manually change the
13online state.
14​
15By default, the `onlineManager` assumes an active network connection, and
16listens to the `online` and `offline` events on the `window` object to
17detect changes.
18​
19## Extends
20​
21- `Subscribable`\<`Listener`\>
22​
23## Methods
24​
25### hasListeners()
26​
27```ts
28hasListeners(): boolean;
29```
30​
31Defined in: [packages/query-core/src/subscribable.ts:43](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L43)
32​
33Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
34​
35#### Returns
36​
37`boolean`
38​
39`true` if at least one listener is registered.
40​
41#### Inherited from
42​
43```ts
44Subscribable.hasListeners
45```
46​
47***
48​
49### isOnline()
50​
51```ts
52isOnline(): boolean;
53```
54​
55Defined in: [packages/query-core/src/onlineManager.ts:111](https://github.com/TanStack/query/blob/main/packages/query-core/src/onlineManager.ts#L111)
56​
57`isOnline` can be used to get the current online state.
58​
59#### Returns
60​
61`boolean`
62​
63`true` if online.
64​
65***
66​
67### setEventListener()
68​
69```ts
70setEventListener(setup: SetupFn): void;
71```
72​
73Defined in: [packages/query-core/src/onlineManager.ts:76](https://github.com/TanStack/query/blob/main/packages/query-core/src/onlineManager.ts#L76)
74​
75`setEventListener` can be used to set a custom event listener that will
76be used to determine the online state. The provided `setup` function
77receives a `setOnline` callback that should be called with a `boolean`
78whenever the online state changes.
79​
80#### Parameters
81​
82##### setup
83​
84`SetupFn`
85​
86Receives the `setOnline` callback, registers the event listener, and may return
87a cleanup function that is called when the listener is replaced or no longer needed.
88​
89#### Returns
90​
91`void`
92​
93#### Example
94​
95```ts
96import NetInfo from '@react-native-community/netinfo'
97import { onlineManager } from '@tanstack/query-core'
98​
99onlineManager.setEventListener((setOnline) => {
100 return NetInfo.addEventListener((state) => {
101 setOnline(!!state.isConnected)
102 })
103})
104```
105​
106***
107​
108### setOnline()
109​
110```ts
111setOnline(online: boolean): void;
112```
113​
114Defined in: [packages/query-core/src/onlineManager.ts:96](https://github.com/TanStack/query/blob/main/packages/query-core/src/onlineManager.ts#L96)
115​
116`setOnline` can be used to manually set the online state.
117​
118#### Parameters
119​
120##### online
121​
122`boolean`
123​
124The online state.
125​
126#### Returns
127​
128`void`
129​
130#### Example
131​
132```ts
133import { onlineManager } from '@tanstack/query-core'
134​
135// Set to online
136onlineManager.setOnline(true)
137​
138// Set to offline
139onlineManager.setOnline(false)
140```
141​
142***
143​
144### subscribe()
145​
146```ts
147subscribe(listener: Listener): () => void;
148```
149​
150Defined in: [packages/query-core/src/subscribable.ts:28](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L28)
151​
152Registers a listener to be called on every update this object notifies about. Returns a function
153that removes the listener again β€” call it to stop listening. The base class never drops a listener
154on its own, though some subclasses clear all of theirs in `destroy()`.
155​
156#### Parameters
157​
158##### listener
159​
160`Listener`
161​
162Called on each update, with whatever the subclass passes to its subscribers.
163​
164#### Returns
165​
166A function that removes the listener.
167​
168() => `void`
169​
170#### Example
171​
172```ts
173const unsubscribe = subscribable.subscribe(() => {
174 // react to the update
175})
176​
177unsubscribe()
178```
179​
180#### Inherited from
181​
182```ts
183Subscribable.subscribe
184```