onlineManager is a singleton that manages network connectivity state and notifies subscribers when the online state changes. It listens to the browser’s online and offline events by default and can be customized with custom event listeners.
Import
Methods
subscribe
Subscribe to online state changes.(online: boolean) => void
required
Callback function that will be called when the online state changes. Receives a boolean indicating whether the network is online.
() => void
Returns an unsubscribe function that removes the listener when called.
Example
setEventListener
Set a custom event listener for online state changes. This allows you to customize how network connectivity is detected.SetupFn
required
A setup function that receives a
setOnline callback and returns an optional cleanup function.Type signature:The setup function should call
setOnline(true) when the network comes online and setOnline(false) when it goes offline.Example
setOnline
Manually set the online state.boolean
required
The new online state. Pass
true to indicate the network is online, false to indicate it’s offline.Example
isOnline
Get the current online state.boolean
Returns
true if the network is online, false otherwise.Example
Default Behavior
By default, theonlineManager listens to the browser’s online and offline events to detect network connectivity changes. The manager starts with the assumption that the network is online (true).
In environments where window.addEventListener is not available (like React Native), you need to set up a custom event listener using setEventListener.