diff --git a/README.md b/README.md index 3a520459..3f7af3b6 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ Check out [this medium article](https://blog.callstack.io/your-react-native-offl + [`Network reducer`](#network-reducer) + [`createNetworkMiddleware()`](#createnetworkmiddleware) + [`Offline Queue`](#offline-queue) + * [Other Utilities](#other-utilities) + + [`checkInternetConnection`](#checkinternetconnection) * [Miscellanea](#miscellanea) * [FAQ](#faq) * [Contributions](#contributions) @@ -408,6 +410,31 @@ fetchData.meta = { } ``` +### Other utilities + +#### `checkInternetConnection()` +Utility function that allows you to query for internet connectivity on demand. If you have integrated this library with redux, you can then dispatch a `CONNECTION_CHANGE` action type to inform the `network` reducer accordingly and keep it up to date. Check the example below. + +```js +checkInternetConnection(timeout?: number = 3000, url?: string = 'http://www.google.com/'): Promise +``` + +##### Example + +```js +import { checkInternetConnection, offlineActionTypes } from 'react-native-offline'; + +async function internetChecker(dispatch) { + const isConnected = await checkInternetConnection(); + // Dispatching can be done inside a connected component, a thunk (where dispatch is injected), saga, or any sort of middleware + // In this example we are using a thunk + dispatch({ + type: offlineActionTypes.CONNECTION_CHANGE, + payload: isConnected, + }); +} +``` + ## Miscellanea ### FAQ @@ -427,7 +454,7 @@ As you can see in the snippets below, we create the `store` instance as usual an import { AsyncStorage, Platform, NetInfo } from 'react-native'; import { createStore, applyMiddleware, compose } from 'redux'; import { persistStore, autoRehydrate } from 'redux-persist'; -import { createNetworkMiddleware, offlineActionTypes, checkInternetConnectionOnStartup } from 'react-native-offline'; +import { createNetworkMiddleware, offlineActionTypes, checkInternetConnection } from 'react-native-offline'; import rootReducer from '../reducers'; const networkMiddleware = createNetworkMiddleware(); @@ -450,7 +477,7 @@ export default function configureStore(callback) { }, () => { // After rehydration completes, we detect initial connection - checkInternetConnectionOnStartup().then(isConnected => { + checkInternetConnection().then(isConnected => { store.dispatch({ type: offlineActionTypes.CONNECTION_CHANGE, payload: isConnected, diff --git a/package.json b/package.json index 9735937c..7eca3c08 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "name": "react-native-offline", - "version": "3.9.1", + "version": "3.10.0", "description": "Handy toolbelt to deal with offline mode in React Native applications. Cross-platform, provides a smooth redux integration.", "main": "./src/index.js", - "author": "Raul Gomez Acuña (https://github.com/rauliyohmc)", + "author": "Raul Gomez Acuña (https://github.com/rgommezz)", "license": "MIT", "scripts": { "lint": "eslint src", @@ -17,7 +17,7 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/rauliyohmc/react-native-offline.git" + "url": "git+https://github.com/rgommezz/react-native-offline.git" }, "keywords": [ "react-native", @@ -30,9 +30,9 @@ "src" ], "bugs": { - "url": "https://github.com/rauliyohmc/react-native-offline/issues" + "url": "https://github.com/rgommezz/react-native-offline/issues" }, - "homepage": "https://github.com/rauliyohmc/react-native-offline#readme", + "homepage": "https://github.com/rgommezz/react-native-offline#readme", "devDependencies": { "babel-eslint": "^7.2.1", "babel-jest": "^19.0.0", diff --git a/src/checkInternetConnectionOnStartup.js b/src/checkInternetConnection.js similarity index 76% rename from src/checkInternetConnectionOnStartup.js rename to src/checkInternetConnection.js index 3b814041..a446e39d 100644 --- a/src/checkInternetConnectionOnStartup.js +++ b/src/checkInternetConnection.js @@ -3,9 +3,15 @@ import { Platform, NetInfo } from 'react-native'; import checkInternetAccess from './checkInternetAccess'; -// 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 -export default function checkInternetConnectionOnStartup( +/** + * 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 timeout + * @param url + * @returns {Promise} + */ +export default function checkInternetConnection( timeout: number = 3000, url: string = 'http://www.google.com/', ): Promise { diff --git a/src/index.js b/src/index.js index e0ce4028..2d166be3 100644 --- a/src/index.js +++ b/src/index.js @@ -17,7 +17,7 @@ module.exports = { get networkEventsListenerSaga() { return require('./sagas').default; }, - get checkInternetConnectionOnStartup() { - return require('./checkInternetConnectionOnStartup').default; + get checkInternetConnection() { + return require('./checkInternetConnection').default; }, };