Network status
Allows tracking network status with Events and Stores.
INFO
Uses Navigator.onLine, Window: online event and Window: offline event under the hood
Usage
All you need to do is to create an integration by calling trackNetworkStatus
with an integration options:
setup
: after this Event all listeners will be installed, and the integration will be ready to use; it is required because it is better to use explicit initialization Event in the application.teardown?
: after this Event all listeners will be removed, and the integration will be ready to be destroyed.
ts
import { trackNetworkStatus } from '@withease/web-api';
const { online, offline, $online, $offline } = trackNetworkStatus({
setup: appStarted,
});
Returns an object with:
online
: Event that fires on connection restoreoffline
: Event that fires on connection loss$online
: Store withtrue
if connection is restored andfalse
if connection is lost$offline
: Store withtrue
if connection is lost andfalse
if connection is restored
TIP
It supports @@trigger
protocol. Since it allows firing only one Event trackNetworkStatus
triggers online
as a fired
in case of @@trigger
protocol.
ts
import { trackNetworkStatus } from '@withease/web-api';
somethingExpectsTrigger(trackNetworkStatus);
Live demo
Let us show you a live demo of how it works. The following demo displays a $online
and $offilne
values of the current network status. Turn off Wi-Fi to see how it works.