bitandblack-online-offline-handler
v0.1.0
Published
A lightweight browser utility to watch and react to online/offline network connection changes.
Maintainers
Readme
Bit&Black Online/Offline Handler
A lightweight browser utility to watch and react to online/offline network connection changes.
Installation
This library is made for the use with Node. Add it to your project by running $ npm install bitandblack-online-offline-handler or add it with another dependency manager by your choice.
Usage
Import the NetworkConnectionWatcher into your project:
import { NetworkConnectionWatcher } from "bitandblack-online-offline-handler";Create a new instance and register your callbacks:
const networkConnectionWatcher = new NetworkConnectionWatcher();
networkConnectionWatcher
.setOnGoingOnlineCallback((event) => {
console.log("Back online!", event);
})
.setOnGoingOfflineCallback((event) => {
console.log("Gone offline!", event);
})
;You can also check the current connection state at any time:
if (true === networkConnectionWatcher.isOnline()) {
console.log("Currently online.");
} else {
console.log("Currently offline.");
}API
new NetworkConnectionWatcher()
Creates a new NetworkConnectionWatcher instance and immediately starts listening for online and offline window events.
isOnline(): boolean
Returns true if the browser is currently online, false otherwise.
isOffline(): boolean
The opposite: Returns false if the browser is currently online, true otherwise.
setOnGoingOnlineCallback(callback: (event: Event) => void): this
Registers a callback that is invoked when the browser transitions to an online state. Returns the watcher instance to allow method chaining.
setOnGoingOfflineCallback(callback: (event: Event) => void): this
Registers a callback that is invoked when the browser transitions to an offline state. Returns the watcher instance to allow method chaining.
Contact
If you have any questions, feel free to contact us under [email protected].
Further information about Bit&Black can be found at www.bitandblack.com.
