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

fix!: cancel _checkInternetReachability request on timeout & cancel via AbortController #678

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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
node-version: '16'
cache: 'yarn'
- run: yarn install --frozen-lockfile
- run: yarn test
Expand All @@ -25,7 +25,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
node-version: '16'
cache: 'yarn'
- run: yarn install --frozen-lockfile
- run: yarn ci:publish
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ but no support will be provided.
The web implementation heavily depends on the [Network Information API](https://developer.mozilla.org/en-US/docs/Web/API/Network_Information_API) which is still an is an experimental technology and thus it's not supported in every browser.
If this API is not available the library will safely fallback to the old [onLine](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine) property and return basic connection information.

AbortController is used to cancel network requests, and may not be available on Internet Explorer, though it is available on Edge https://caniuse.com/abortcontroller

## Node Compatibility

Node v16 is the minimum required node version - `AbortController` is only present in stable versions of node from v16 on

## Migrating from the core `react-native` module
This module was created when the NetInfo was split out from the core of React Native. To migrate to this module you need to follow the installation instructions above and then change you imports from:

Expand Down
16 changes: 11 additions & 5 deletions src/internal/internetReachability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,22 @@ export default class InternetReachability {
};

private _checkInternetReachability = (): InternetReachabilityCheckHandler => {
const controller = new AbortController();

const responsePromise = fetch(this._configuration.reachabilityUrl, {
method: this._configuration.reachabilityMethod,
cache: 'no-cache',
signal: controller.signal,
});

// Create promise that will reject after the request timeout has been reached
let timeoutHandle: ReturnType<typeof setTimeout>;
const timeoutPromise = new Promise<Response>(
(_, reject): void => {
timeoutHandle = setTimeout(
(): void => reject('timedout'),
this._configuration.reachabilityRequestTimeout,
);
timeoutHandle = setTimeout((): void => {
controller.abort();
reject('timedout');
}, this._configuration.reachabilityRequestTimeout);
},
);

Expand All @@ -89,7 +92,10 @@ export default class InternetReachability {
let cancel: () => void = (): void => {};
const cancelPromise = new Promise<Response>(
(_, reject): void => {
cancel = (): void => reject('canceled');
cancel = (): void => {
controller.abort();
reject('canceled');
};
},
);

Expand Down
Loading