Open History to see the last commit
1import { Subscribable } from './subscribable'
2β
3type Listener = (online: boolean) => void
4type SetupFn = (setOnline: Listener) => (() => void) | undefined
5β
6/**
7 * The `OnlineManager` manages the online state within TanStack Query. It can
8 * be used to change the default event listeners or to manually change the
9 * online state.
10 *
11 * By default, the `onlineManager` assumes an active network connection, and
12 * listens to the `online` and `offline` events on the `window` object to
13 * detect changes.
14 */
15export class OnlineManager extends Subscribable<Listener> {
16 #online = true
17 #cleanup?: () => void
18β
19 #setup: SetupFn
20β
21 constructor() {
22 super()
23 this.#setup = (onOnline) => {
24 // addEventListener does not exist in React Native, but window does
25 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
26 if (typeof window !== 'undefined' && window.addEventListener) {
27 const onlineListener = () => onOnline(true)
28 const offlineListener = () => onOnline(false)
29 // Listen to online
30 window.addEventListener('online', onlineListener, false)
31 window.addEventListener('offline', offlineListener, false)
32β
33 return () => {
34 // Be sure to unsubscribe if a new handler is set
35 window.removeEventListener('online', onlineListener)
36 window.removeEventListener('offline', offlineListener)
37 }
38 }
39β
40 return
41 }
42 }
43β
44 protected onSubscribe(): void {
45 if (!this.#cleanup) {
46 this.setEventListener(this.#setup)
47 }
48 }
49β
50 protected onUnsubscribe() {
51 if (!this.hasListeners()) {
52 this.#cleanup?.()
53 this.#cleanup = undefined
54 }
55 }
56β
57 /**
58 * `setEventListener` can be used to set a custom event listener that will
59 * be used to determine the online state. The provided `setup` function
60 * receives a `setOnline` callback that should be called with a `boolean`
61 * whenever the online state changes.
62 *
63 * @example
64 * ```ts
65 * import NetInfo from '@react-native-community/netinfo'
66 * import { onlineManager } from '@tanstack/query-core'
67 *
68 * onlineManager.setEventListener((setOnline) => {
69 * return NetInfo.addEventListener((state) => {
70 * setOnline(!!state.isConnected)
71 * })
72 * })
73 * ```
74 */
75 setEventListener(setup: SetupFn): void {
76 this.#setup = setup
77 this.#cleanup?.()
78 this.#cleanup = setup(this.setOnline.bind(this))
79 }
80β
81 /**
82 * `setOnline` can be used to manually set the online state.
83 *
84 * @example
85 * ```ts
86 * import { onlineManager } from '@tanstack/query-core'
87 *
88 * // Set to online
89 * onlineManager.setOnline(true)
90 *
91 * // Set to offline
92 * onlineManager.setOnline(false)
93 * ```
94 */
95 setOnline(online: boolean): void {
96 const changed = this.#online !== online
97β
98 if (changed) {
99 this.#online = online
100 this.listeners.forEach((listener) => {
101 listener(online)
102 })
103 }
104 }
105β
106 /**
107 * `isOnline` can be used to get the current online state.
108 */
109 isOnline(): boolean {
110 return this.#online
111 }
112}
113β
114/**
115 * Singleton instance of {@link OnlineManager}, used to manage and observe the online state within TanStack Query.
116 */
117export const onlineManager = new OnlineManager()