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

OnlineManager.md

181 lines3.6 KB

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