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

feat: add parameter useNativeReachability to optionally choose non-native reachability test #609

Merged
merged 4 commits into from
Jun 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,14 @@ The configuration options for the library.

| Property | Type | Default | Description
| ---------------------------- | --------------------------------- | ----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `reachabilityUrl` | `string` | `https://clients3.google.com/generate_204` | The URL to call to test if the internet is reachable. Only used on platforms which do not supply internet reachability natively. |
| `reachabilityTest` | `(response: Response) => boolean` | `Promise.resolve(response.status === 204)` | A function which is passed the `Response` from calling the reachability URL. It should return `true` if the response indicates that the internet is reachable. Only used on platforms which do not supply internet reachability natively. |
| `reachabilityShortTimeout` | `number` | 5 seconds | The number of milliseconds between internet reachability checks when the internet was not previously detected. Only used on platforms which do not supply internet reachability natively. |
| `reachabilityLongTimeout` | `number` | 60 seconds | The number of milliseconds between internet reachability checks when the internet was previously detected. Only used on platforms which do not supply internet reachability natively. |
| `reachabilityRequestTimeout` | `number` | 15 seconds | The number of milliseconds that a reachability check is allowed to take before failing. Only used on platforms which do not supply internet reachability natively. |
| `reachabilityUrl` | `string` | `https://clients3.google.com/generate_204` | The URL to call to test if the internet is reachable. Only used on platforms which do not supply internet reachability natively or if `useNativeReachability` is `false`. |
| `reachabilityTest` | `(response: Response) => boolean` | `Promise.resolve(response.status === 204)` | A function which is passed the `Response` from calling the reachability URL. It should return `true` if the response indicates that the internet is reachable. Only used on platforms which do not supply internet reachability natively or if `useNativeReachability` is `false`. |
| `reachabilityShortTimeout` | `number` | 5 seconds | The number of milliseconds between internet reachability checks when the internet was not previously detected. Only used on platforms which do not supply internet reachability natively or if `useNativeReachability` is `false`. |
| `reachabilityLongTimeout` | `number` | 60 seconds | The number of milliseconds between internet reachability checks when the internet was previously detected. Only used on platforms which do not supply internet reachability natively or if `useNativeReachability` is `false`. |
| `reachabilityRequestTimeout` | `number` | 15 seconds | The number of milliseconds that a reachability check is allowed to take before failing. Only used on platforms which do not supply internet reachability natively or if `useNativeReachability` is `false`. |
| `reachabilityShouldRun` | `() => boolean` | `() => true` | A function which returns a boolean to determine if checkInternetReachability should be run. |
| `shouldFetchWiFiSSID` | `boolean` | `false` | A flag indicating one of the requirements on iOS has been met to retrieve the network (B)SSID, and the native SSID retrieval APIs should be called. This has no effect on Android.
| `useNativeReachability` | `boolean` | `true` | A flag indicating whether or not Netinfo should use native reachability checks, if available.


### Methods
Expand All @@ -246,7 +247,8 @@ NetInfo.configure({
reachabilityShortTimeout: 5 * 1000, // 5s
reachabilityRequestTimeout: 15 * 1000, // 15s
reachabilityShouldRun: () => true,
shouldFetchWiFiSSID: true // met iOS requirements to get SSID. Will leak memory if set to true without meeting requirements.
shouldFetchWiFiSSID: true, // met iOS requirements to get SSID. Will leak memory if set to true without meeting requirements.
useNativeReachability: false
});
```

Expand Down Expand Up @@ -301,7 +303,8 @@ const YourComponent = () => {
reachabilityShortTimeout: 5 * 1000, // 5s
reachabilityRequestTimeout: 15 * 1000, // 15s
reachabilityShouldRun: () => true,
shouldFetchWiFiSSID: true // met iOS requirements to get SSID
shouldFetchWiFiSSID: true, // met iOS requirements to get SSID
useNativeReachability: false
});

// ...
Expand Down
3 changes: 2 additions & 1 deletion src/internal/defaultConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const DEFAULT_CONFIGURATION: Types.NetInfoConfiguration = {
reachabilityLongTimeout: 60 * 1000, // 60s
reachabilityRequestTimeout: 15 * 1000, // 15s
reachabilityShouldRun: (): boolean => true,
shouldFetchWiFiSSID: false
shouldFetchWiFiSSID: false,
useNativeReachability: true
};

export default DEFAULT_CONFIGURATION;
3 changes: 2 additions & 1 deletion src/internal/defaultConfiguration.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const DEFAULT_CONFIGURATION: Types.NetInfoConfiguration = {
reachabilityLongTimeout: 60 * 1000, // 60s
reachabilityRequestTimeout: 15 * 1000, // 15s
reachabilityShouldRun: (): boolean => true,
shouldFetchWiFiSSID: true
shouldFetchWiFiSSID: true,
useNativeReachability: true
};

export default DEFAULT_CONFIGURATION
5 changes: 4 additions & 1 deletion src/internal/internetReachability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ export default class InternetReachability {
};

public update = (state: PrivateTypes.NetInfoNativeModuleState): void => {
if (typeof state.isInternetReachable === 'boolean') {
if (
typeof state.isInternetReachable === 'boolean' &&
this._configuration.useNativeReachability
) {
this._setIsInternetReachable(state.isInternetReachable);
} else {
this._setExpectsConnection(state.isConnected);
Expand Down
1 change: 1 addition & 0 deletions src/internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,5 @@ export interface NetInfoConfiguration {
reachabilityRequestTimeout: number;
reachabilityShouldRun: () => boolean;
shouldFetchWiFiSSID: boolean;
useNativeReachability: boolean;
}