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

onlineManager.ts

119 lines3.4 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
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 // 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
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 * @param setup - Receives the `setOnline` callback, registers the event listener, and may return
63 * a cleanup function that is called when the listener is replaced or no longer needed.
64 * @example
65 * ```ts
66 * import NetInfo from '@react-native-community/netinfo'
67 * import { onlineManager } from '@tanstack/query-core'
68 *
69 * onlineManager.setEventListener((setOnline) => {
70 * return NetInfo.addEventListener((state) => {
71 * setOnline(!!state.isConnected)
72 * })
73 * })
74 * ```
75 */
76 setEventListener(setup: SetupFn): void {
77 this.#setup = setup
78 this.#cleanup?.()
79 this.#cleanup = setup(this.setOnline.bind(this))
80 }
81​
82 /**
83 * `setOnline` can be used to manually set the online state.
84 * @param online - The online state.
85 * @example
86 * ```ts
87 * import { onlineManager } from '@tanstack/query-core'
88 *
89 * // Set to online
90 * onlineManager.setOnline(true)
91 *
92 * // Set to offline
93 * onlineManager.setOnline(false)
94 * ```
95 */
96 setOnline(online: boolean): void {
97 const changed = this.#online !== online
98​
99 if (changed) {
100 this.#online = online
101 this.listeners.forEach((listener) => {
102 listener(online)
103 })
104 }
105 }
106​
107 /**
108 * `isOnline` can be used to get the current online state.
109 * @returns `true` if online.
110 */
111 isOnline(): boolean {
112 return this.#online
113 }
115​
116/**
117 * Singleton instance of {@link OnlineManager}, used to manage and observe the online state within TanStack Query.
118 */
119export const onlineManager = new OnlineManager()