Skip to content

Commit

Permalink
Exporting checkInternetConnection for queries on demand + docs (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgommezz authored Jun 11, 2018
1 parent aab6455 commit 81fedc6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<boolean>
```

##### 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
Expand All @@ -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();
Expand All @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]> (https://github.com/rauliyohmc)",
"author": "Raul Gomez Acuña <[email protected]> (https://github.com/rgommezz)",
"license": "MIT",
"scripts": {
"lint": "eslint src",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>}
*/
export default function checkInternetConnection(
timeout: number = 3000,
url: string = 'http://www.google.com/',
): Promise<boolean> {
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
get networkEventsListenerSaga() {
return require('./sagas').default;
},
get checkInternetConnectionOnStartup() {
return require('./checkInternetConnectionOnStartup').default;
get checkInternetConnection() {
return require('./checkInternetConnection').default;
},
};

0 comments on commit 81fedc6

Please sign in to comment.