Skip to content

Commit ed42c22

Browse files
kentosrgommezz
authored andcommitted
Adds ability to check for connectivity in background, defaults to false (#97)
1 parent 2acaf65 commit ed42c22

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type Config = {
7676
pingServerUrl?: string = 'https://google.com',
7777
withExtraHeadRequest?: boolean = true,
7878
checkConnectionInterval?: number = 0,
79+
checkInBackground?: boolean = false,
7980
}
8081
```
8182

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

9192
`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.
9293

94+
`checkInBackground`: whether or not to check connectivity when app isn't active. Default is `false`.
95+
9396
##### Usage
9497
```js
9598
import React from 'react';

src/withNetworkConnectivity.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import React, { PureComponent } from 'react';
44
import PropTypes from 'prop-types';
5-
import { NetInfo, Platform } from 'react-native';
5+
import { NetInfo, Platform, AppState } from 'react-native';
66
import hoistStatics from 'hoist-non-react-statics';
77
import { connectionChange } from './actionCreators';
88
import reactConnectionStore from './reactConnectionStore';
@@ -18,6 +18,7 @@ type Arguments = {
1818
pingServerUrl?: string,
1919
withExtraHeadRequest?: boolean,
2020
checkConnectionInterval?: number,
21+
checkInBackground?: boolean,
2122
};
2223

2324
type State = {
@@ -31,6 +32,7 @@ const withNetworkConnectivity = (
3132
pingServerUrl = 'https://google.com',
3233
withExtraHeadRequest = true,
3334
checkConnectionInterval = 0,
35+
checkInBackground = false,
3436
}: Arguments = {},
3537
) => (WrappedComponent: ReactClass<*>) => {
3638
if (typeof withRedux !== 'boolean') {
@@ -100,6 +102,9 @@ const withNetworkConnectivity = (
100102
};
101103

102104
checkInternet = () => {
105+
if (checkInBackground === false && AppState.currentState !== 'active') {
106+
return; // <-- Return early as we dont care about connectivity if apps' not in foreground.
107+
}
103108
checkInternetAccess(
104109
timeout,
105110
pingServerUrl,

0 commit comments

Comments
 (0)