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

OnlineManager.md

183 lines3.7 KB

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