Skip to content

Commit

Permalink
feat: add parameter useNativeReachability to optionally choose non-na…
Browse files Browse the repository at this point in the history
…tive reachability test (#609)

* Add ability to override native reachability checks
* Update README.md
* Fix lint
  • Loading branch information
andrewssun1 committed Jun 24, 2022
1 parent a6fb54f commit 9b02cac
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
17 changes: 10 additions & 7 deletions README.md
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
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
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
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
Expand Up @@ -115,4 +115,5 @@ export interface NetInfoConfiguration {
reachabilityRequestTimeout: number;
reachabilityShouldRun: () => boolean;
shouldFetchWiFiSSID: boolean;
useNativeReachability: boolean;
}

0 comments on commit 9b02cac

Please sign in to comment.