Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding option of disabling extra HEAD request #46

Merged
merged 5 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ withNetworkConnectivity(config: Config): (WrappedComponent) => EnhancedComponent
type Config = {
withRedux?: boolean = false,
timeout?: number = 3000,
pingServerUrl?: string = 'https://google.com'
pingServerUrl?: string = 'https://google.com',
withExtraHeadRequest?: boolean = true,
}
```

Expand All @@ -75,6 +76,8 @@ type Config = {

`pingServerUrl`: remote server to ping to. It defaults to https://google.com since it's probably one the most stable servers out there, but you can provide your own if needed.

`withExtraHeadRequest`: flag that denotes whether the extra ping check will be performed or not. Defaults to `true`.

##### Usage
```js
import React from 'react';
Expand All @@ -89,14 +92,15 @@ export default withNetworkConnectivity()(YourComponent);
```

#### `ConnectivityRenderer`
React component that accepts a function as children. It allows you to decouple your parent component and your child component, managing connectivity state on behalf of the components it is composed with, without making demands on how that state is leveraged by its children. Useful for conditionally render different children based on connectivity status. `timeout` and `pingServerUrl` can be provided through props in this case.
React component that accepts a function as children. It allows you to decouple your parent component and your child component, managing connectivity state on behalf of the components it is composed with, without making demands on how that state is leveraged by its children. Useful for conditionally render different children based on connectivity status. `timeout`, `pingServerUrl` and `withExtraHeadRequest` can be provided through props in this case.

##### Props
```js
type Props = {
children: (isConnected: boolean) => React$Element<any>
timeout?: number,
pingServerUrl?: string
timeout?: number = 3000,
pingServerUrl?: string = 'https://google.com',
withExtraHeadRequest?: boolean = true,
}
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-offline",
"version": "3.1.1",
"version": "3.2.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 <raulgdeveloper@gmail.com> (https://github.com/rauliyohmc)",
Expand Down
27 changes: 22 additions & 5 deletions src/ConnectivityRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import reactConnectionStore from './reactConnectionStore';
type DefaultProps = {
timeout?: number,
pingServerUrl?: string,
withExtraHeadRequest?: boolean,
};

type Props = DefaultProps & {
Expand All @@ -24,11 +25,13 @@ class ConnectivityRenderer extends Component<DefaultProps, Props, State> {
children: PropTypes.func.isRequired,
timeout: PropTypes.number,
pingServerUrl: PropTypes.string,
withExtraHeadRequest: PropTypes.bool,
};

static defaultProps: DefaultProps = {
timeout: 3000,
pingServerUrl: 'https://google.com',
withExtraHeadRequest: true,
};

state = {
Expand All @@ -48,17 +51,31 @@ class ConnectivityRenderer extends Component<DefaultProps, Props, State> {
}

componentDidMount() {
NetInfo.isConnected.addEventListener('change', this.checkInternet);
NetInfo.isConnected.addEventListener(
'change',
this.props.withExtraHeadRequest
? this.checkInternet
: this.handleConnectivityChange,
);
// On Android the listener does not fire on startup
if (Platform.OS === 'android') {
NetInfo.isConnected
.fetch()
.then((isConnected: boolean) => this.checkInternet(isConnected));
NetInfo.isConnected.fetch().then((isConnected: boolean) => {
if (this.props.withExtraHeadRequest) {
this.checkInternet(isConnected);
} else {
this.handleConnectivityChange(isConnected);
}
});
}
}

componentWillUnmount() {
NetInfo.isConnected.removeEventListener('change', this.checkInternet);
NetInfo.isConnected.removeEventListener(
'change',
this.props.withExtraHeadRequest
? this.checkInternet
: this.handleConnectivityChange,
);
}

checkInternet = (isConnected: boolean) => {
Expand Down
26 changes: 21 additions & 5 deletions src/withNetworkConnectivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Arguments = {
withRedux?: boolean,
timeout?: number,
pingServerUrl?: string,
withExtraHeadRequest?: boolean,
};

type State = {
Expand All @@ -23,6 +24,7 @@ const withNetworkConnectivity = (
withRedux = false,
timeout = 3000,
pingServerUrl = 'https://google.com',
withExtraHeadRequest = true,
}: Arguments = {},
) => (WrappedComponent: ReactClass<*>) => {
if (typeof withRedux !== 'boolean') {
Expand All @@ -49,17 +51,31 @@ const withNetworkConnectivity = (
};

componentDidMount() {
NetInfo.isConnected.addEventListener('change', this.checkInternet);
NetInfo.isConnected.addEventListener(
'change',
withExtraHeadRequest
? this.checkInternet
: this.handleConnectivityChange,
);
// On Android the listener does not fire on startup
if (Platform.OS === 'android') {
NetInfo.isConnected
.fetch()
.then((isConnected: boolean) => this.checkInternet(isConnected));
NetInfo.isConnected.fetch().then((isConnected: boolean) => {
if (withExtraHeadRequest) {
this.checkInternet(isConnected);
} else {
this.handleConnectivityChange(isConnected);
}
});
}
}

componentWillUnmount() {
NetInfo.isConnected.removeEventListener('change', this.checkInternet);
NetInfo.isConnected.removeEventListener(
'change',
withExtraHeadRequest
? this.checkInternet
: this.handleConnectivityChange,
);
}

checkInternet = (isConnected: boolean) => {
Expand Down