react-native-offline
Advanced tools
Comparing version 4.0.0 to 4.1.0
{ | ||
"name": "react-native-offline", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Handy toolbelt to deal with offline mode in React Native applications. Cross-platform, provides a smooth redux integration.", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -9,3 +9,3 @@ # react-native-offline | ||
## Important (Please read) | ||
**This is the documentation for version 4.0.0. If you are migrating from v3 to v4, check the [release notes](https://github.com/rgommezz/react-native-offline/releases/tag/v4.0.0).** | ||
**This is the documentation for version 4.x.x. If you are migrating from v3 to v4, check the [release notes](https://github.com/rgommezz/react-native-offline/releases/tag/v4.0.0).** | ||
@@ -27,3 +27,3 @@ ## Contents | ||
+ [`networkSaga`](#networksaga) | ||
+ [`createNetworkMiddleware()`](#createnetworkmiddleware) | ||
+ [`createNetworkMiddleware`](#createnetworkmiddleware) | ||
+ [`Offline Queue`](#offline-queue) | ||
@@ -76,3 +76,3 @@ * [Other Utilities](#other-utilities) | ||
``` | ||
$ yarn add react-native-offline@4.0.0 | ||
$ yarn add react-native-offline | ||
``` | ||
@@ -432,4 +432,9 @@ | ||
**Note**: It's recommended to always set `shouldPing` to `true` (the default behaviour), in order to prevent inconsistent behaviour on iOS for RN < 0.57. | ||
```js | ||
checkInternetConnection(url?: string = 'https://www.google.com/', pingTimeout?: number = 3000): Promise<boolean> | ||
checkInternetConnection( | ||
url?: string = 'https://www.google.com/', | ||
pingTimeout?: number = 3000, | ||
shouldPing?: boolean = true | ||
): Promise<boolean> | ||
``` | ||
@@ -436,0 +441,0 @@ |
/* @flow */ | ||
import { Platform, NetInfo } from 'react-native'; | ||
import { NetInfo } from 'react-native'; | ||
import checkInternetAccess from './checkInternetAccess'; | ||
@@ -9,37 +9,19 @@ import { DEFAULT_PING_SERVER_URL, DEFAULT_TIMEOUT } from './constants'; | ||
* Utility that allows to query for internet connectivity on demand | ||
* On iOS, the listener is fired immediately after registration | ||
* On Android, we need to use `isConnected.fetch`, that returns a promise which resolves with a boolean | ||
* @param url | ||
* @param timeout | ||
* @param shouldPing | ||
* @returns {Promise<boolean>} | ||
*/ | ||
export default function checkInternetConnection( | ||
export default async function checkInternetConnection( | ||
url: string = DEFAULT_PING_SERVER_URL, | ||
timeout: number = DEFAULT_TIMEOUT, | ||
shouldPing: boolean = true, | ||
): Promise<boolean> { | ||
let connectionChecked: Promise<boolean>; | ||
if (Platform.OS === 'ios') { | ||
connectionChecked = new Promise((resolve: Function) => { | ||
const handleFirstConnectivityChangeIOS = (isConnected: boolean) => { | ||
NetInfo.isConnected.removeEventListener( | ||
'connectionChange', | ||
handleFirstConnectivityChangeIOS, | ||
); | ||
resolve(isConnected); | ||
}; | ||
NetInfo.isConnected.addEventListener( | ||
'connectionChange', | ||
handleFirstConnectivityChangeIOS, | ||
); | ||
}); | ||
} else { | ||
connectionChecked = NetInfo.isConnected.fetch(); | ||
} | ||
return connectionChecked.then((isConnected: boolean) => { | ||
if (isConnected) { | ||
return checkInternetAccess({ timeout, url }); | ||
return NetInfo.isConnected.fetch().then(async (isConnected: boolean) => { | ||
if (shouldPing) { | ||
const hasInternetAccess = await checkInternetAccess({ timeout, url }); | ||
return hasInternetAccess; | ||
} | ||
return Promise.resolve(false); | ||
return isConnected; | ||
}); | ||
} |
689
66620
971