diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f8053db2..af924349 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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 diff --git a/README.md b/README.md index f77150af..99692ec7 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/internal/internetReachability.ts b/src/internal/internetReachability.ts index 5edff0a0..9dd495cb 100644 --- a/src/internal/internetReachability.ts +++ b/src/internal/internetReachability.ts @@ -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; const timeoutPromise = new Promise( (_, reject): void => { - timeoutHandle = setTimeout( - (): void => reject('timedout'), - this._configuration.reachabilityRequestTimeout, - ); + timeoutHandle = setTimeout((): void => { + controller.abort(); + reject('timedout'); + }, this._configuration.reachabilityRequestTimeout); }, ); @@ -89,7 +92,10 @@ export default class InternetReachability { let cancel: () => void = (): void => {}; const cancelPromise = new Promise( (_, reject): void => { - cancel = (): void => reject('canceled'); + cancel = (): void => { + controller.abort(); + reject('canceled'); + }; }, );