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

OnlineManager.md

186 lines3.8 KB

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