@blackblock/slow-network-checker
v0.1.2
Published
A simple package that uses NetworkInformation API to check if the user's network is slow.
Downloads
6
Maintainers
Readme
Slow Network Detector
A simple package that uses NetworkInformation API to check if the user's network is slow.
Usage
import {
isSlowNetwork
} from '@blackblock/slow-network-checker'
const slowNetworkDefinition = {
effectiveType: '2g',
downlink: 1,
saveData: true,
rtt: 1000
}
const networkInformation = navigator.connection || navigator.mozConnection || navigator.webkitConnection
const result = isSlowNetwork(slowNetworkDefinition)(networkInformation) //Return Boolean
Installation
npm i @blackblock/slow-network-checker
API Reference
Type Definition
Definition
An object that lists the definition of a slow network based on different metrics.
type Definition = { effectiveType :: String,
saveData :: Boolean,
downlink :: Int,
rtt :: Int}
NetworkPerformance
An object that records the performance of a network in different metrics. The NetworkInformation object should be passed in here.
You should also pass in the online status from Navigator.onLine in the performance object as online
, so that the status of offline and slow network can be differentiated.
type NetworkPerformance = {
online :: Boolean,
effectiveType :: String,
saveData :: Boolean,
downlink :: Int,
rtt :: Int}
Function
isSlowNetwork
Main function of this package. Test if the NetworkInformation
object met the definition of a slow network.
If the actual network performance is slower or equal to your definition defined as Definition
, it will be regarded as a slow network and return true
, false
otherwise.
If the user is offline, isSlowNetwork
will always return false
, so that the status of offline and slow network can be differentiated.
isSlowNetwork :: Definition -> NetworkPerformance -> Boolean