Skip to content

Commit

Permalink
checkInternetConnectionOnStartup utility function (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
dickerpulli authored and rgommezz committed May 30, 2018
1 parent 0366c66 commit a8deeb7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
28 changes: 3 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ const Root = () => (
```

##### 2b.- Fork `networkEventsListenerSaga` from your root saga.
If you are using redux-saga, I highly encourage you this option since it's a very elegant way to deal with global connectivity changes, without having to wrap your components with extra functionality. It receives the same [config](#config) options as `withNetworkConnectivity` HOC, with the exception of `withRedux`, which is not needed in this case.
If you are using redux-saga, I highly encourage you this option since it's a very elegant way to deal with global connectivity changes, without having to wrap your components with extra functionality. It receives the same [config](#config) options as `withNetworkConnectivity` HOC, with the exception of `withRedux`, which is not needed in this case.

```js
// rootSaga.js
Expand Down Expand Up @@ -424,33 +424,11 @@ 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 } from 'react-native-offline';
import { createNetworkMiddleware, offlineActionTypes, checkInternetConnectionOnStartup } from 'react-native-offline';
import rootReducer from '../reducers';

const networkMiddleware = createNetworkMiddleware();

// 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
function isNetworkConnected(): Promise<boolean> {
if (Platform.OS === 'ios') {
return new Promise(resolve => {
const handleFirstConnectivityChangeIOS = isConnected => {
NetInfo.isConnected.removeEventListener( // Cleaning up after initial detection
'connectionChange',
handleFirstConnectivityChangeIOS,
);
resolve(isConnected);
};
NetInfo.isConnected.addEventListener(
'connectionChange',
handleFirstConnectivityChangeIOS,
);
});
}

return NetInfo.isConnected.fetch();
}

export default function configureStore(callback) {
const store = createStore(
rootReducer,
Expand All @@ -469,7 +447,7 @@ export default function configureStore(callback) {
},
() => {
// After rehydration completes, we detect initial connection
isNetworkConnected().then(isConnected => {
checkInternetConnectionOnStartup().then(isConnected => {
store.dispatch({
type: offlineActionTypes.CONNECTION_CHANGE,
payload: isConnected,
Expand Down
37 changes: 37 additions & 0 deletions src/checkInternetConnectionOnStartup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* @flow */

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(
timeout: number = 3000,
url: string = 'https://google.com',
): 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 Promise.resolve(false);
});
}
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ module.exports = {
get networkEventsListenerSaga() {
return require('./sagas').default;
},
get checkInternetConnectionOnStartup() {
return require('./checkInternetConnectionOnStartup').default;
},
};

0 comments on commit a8deeb7

Please sign in to comment.