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

onlineManager.ts

126 lines3.5 KB

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 be
8 * used to change the default event listeners or to manually change the online
9 * 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 detect
13 * 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 // oxlint-disable-next-line typescript/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 override onSubscribe(): void {
45 if (!this.#cleanup) {
46 this.setEventListener(this.#setup)
47 }
48 }
49​
50 protected override 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 be
59 * used to determine the online state. The provided `setup` function receives
60 * a `setOnline` callback that should be called with a `boolean` whenever the
61 * 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 * @param setup - Receives the `setOnline` callback, registers the event
76 * listener, and may return a cleanup function that is called when the
77 * listener is replaced or no longer needed.
78 */
79 setEventListener(setup: SetupFn): void {
80 this.#setup = setup
81 this.#cleanup?.()
82 this.#cleanup = setup(this.setOnline.bind(this))
83 }
84​
85 /**
86 * `setOnline` can be used to manually set the online state.
87 *
88 * @example
89 * ;```ts
90 * import { onlineManager } from '@tanstack/query-core'
91 *
92 * // Set to online
93 * onlineManager.setOnline(true)
94 *
95 * // Set to offline
96 * onlineManager.setOnline(false)
97 * ```
98 *
99 * @param online - The online state.
100 */
101 setOnline(online: boolean): void {
102 const changed = this.#online !== online
103​
104 if (changed) {
105 this.#online = online
106 this.listeners.forEach((listener) => {
107 listener(online)
108 })
109 }
110 }
111​
112 /**
113 * `isOnline` can be used to get the current online state.
114 *
115 * @returns `true` if online.
116 */
117 isOnline(): boolean {
118 return this.#online
119 }
121​
122/**
123 * Singleton instance of {@link OnlineManager}, used to manage and observe the
124 * online state within TanStack Query.
125 */
126export const onlineManager = new OnlineManager()