Open History to see the last commit
1import NetInfo from '@react-native-community/netinfo'
2import { AppState } from 'react-native'
3import type { AppStateStatus, NativeEventSubscription } from 'react-native'
4import type { OnlineDetector } from '../types'
5
6/**
7 * React Native online detector that uses RN APIs.
8 * Listens for:
9 * - Network connectivity changes via `@react-native-community/netinfo`
10 * - App state changes (foreground/background) via `AppState`
11 */
12export class ReactNativeOnlineDetector implements OnlineDetector {
13 private listeners: Set<() => void> = new Set()
14 private netInfoUnsubscribe: (() => void) | null = null
15 private appStateSubscription: NativeEventSubscription | null = null
16 private isListening = false
17 private wasConnected = true
18
19 constructor() {
20 this.startListening()
21 }
22
23 private startListening(): void {
24 if (this.isListening) {
25 return
26 }
27
28 this.isListening = true
29
30 if (typeof NetInfo.fetch === `function`) {
31 void NetInfo.fetch()
32 .then((state) => {
33 this.wasConnected = this.toConnectivityState(state)
34 })
35 .catch(() => {
36 // Ignore initial fetch failures and rely on subscription updates.
37 })
38 }
39
40 // Subscribe to network state changes
41 this.netInfoUnsubscribe = NetInfo.addEventListener((state) => {
42 const isConnected = this.toConnectivityState(state)
43
44 // Only notify when transitioning to online
45 if (isConnected && !this.wasConnected) {
46 this.notifyListeners()
47 }
48
49 this.wasConnected = isConnected
50 })
51
52 // Subscribe to app state changes (foreground/background)
53 this.appStateSubscription = AppState.addEventListener(
54 `change`,
55 this.handleAppStateChange,
56 )
57 }
58
59 private handleAppStateChange = (nextState: AppStateStatus): void => {
60 // Notify when app becomes active (foreground)
61 if (nextState === `active`) {
62 this.notifyListeners()
63 }
64 }
65
66 private stopListening(): void {
67 if (!this.isListening) {
68 return
69 }
70
71 this.isListening = false
72
73 if (this.netInfoUnsubscribe) {
74 this.netInfoUnsubscribe()
75 this.netInfoUnsubscribe = null
76 }
77
78 if (this.appStateSubscription) {
79 this.appStateSubscription.remove()
80 this.appStateSubscription = null
81 }
82 }
83
84 private notifyListeners(): void {
85 for (const listener of this.listeners) {
86 try {
87 listener()
88 } catch (error) {
89 console.warn(`ReactNativeOnlineDetector listener error:`, error)
90 }
91 }
92 }
93
94 subscribe(callback: () => void): () => void {
95 this.listeners.add(callback)
96
97 return () => {
98 this.listeners.delete(callback)
99
100 if (this.listeners.size === 0) {
101 this.stopListening()
102 }
103 }
104 }
105
106 notifyOnline(): void {
107 this.notifyListeners()
108 }
109
110 isOnline(): boolean {
111 return this.wasConnected
112 }
113
114 dispose(): void {
115 this.stopListening()
116 this.listeners.clear()
117 }
118
119 private toConnectivityState(state: {
120 isConnected: boolean | null
121 isInternetReachable: boolean | null
122 }): boolean {
123 return !!state.isConnected && state.isInternetReachable !== false
124 }
125}