Skip to content

Commit

Permalink
Adds ability to check for connectivity in background, defaults to fal…
Browse files Browse the repository at this point in the history
…se (#97)
  • Loading branch information
kentos authored and rgommezz committed Jun 11, 2018
1 parent 2acaf65 commit ed42c22
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type Config = {
pingServerUrl?: string = 'https://google.com',
withExtraHeadRequest?: boolean = true,
checkConnectionInterval?: number = 0,
checkInBackground?: boolean = false,
}
```

Expand All @@ -90,6 +91,8 @@ type Config = {

`checkConnectionInterval`: the interval (in ms) you want to ping the server at. The default is 0, and that means it is not going to regularly check connectivity.

`checkInBackground`: whether or not to check connectivity when app isn't active. Default is `false`.

##### Usage
```js
import React from 'react';
Expand Down
7 changes: 6 additions & 1 deletion src/withNetworkConnectivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { NetInfo, Platform } from 'react-native';
import { NetInfo, Platform, AppState } from 'react-native';
import hoistStatics from 'hoist-non-react-statics';
import { connectionChange } from './actionCreators';
import reactConnectionStore from './reactConnectionStore';
Expand All @@ -18,6 +18,7 @@ type Arguments = {
pingServerUrl?: string,
withExtraHeadRequest?: boolean,
checkConnectionInterval?: number,
checkInBackground?: boolean,
};

type State = {
Expand All @@ -31,6 +32,7 @@ const withNetworkConnectivity = (
pingServerUrl = 'https://google.com',
withExtraHeadRequest = true,
checkConnectionInterval = 0,
checkInBackground = false,
}: Arguments = {},
) => (WrappedComponent: ReactClass<*>) => {
if (typeof withRedux !== 'boolean') {
Expand Down Expand Up @@ -100,6 +102,9 @@ const withNetworkConnectivity = (
};

checkInternet = () => {
if (checkInBackground === false && AppState.currentState !== 'active') {
return; // <-- Return early as we dont care about connectivity if apps' not in foreground.
}
checkInternetAccess(
timeout,
pingServerUrl,
Expand Down

0 comments on commit ed42c22

Please sign in to comment.