From 0a36296d1cafdd1572503e937dbb63e393854ac6 Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 29 Nov 2023 19:31:04 +0700 Subject: [PATCH 1/2] fix: internetReachability aborts handle cancel correctly (#700) InternetReachability aborts pending request by calling abort() of AbortController. According to AbortController API when abort() is called, the fetch() promise rejects with an Error of type DOMException, with name AbortError. So reject('canceled') will be ignored and checking error !== 'canceled' is useless. The right way is to use abort('canceled') to set reject reason to 'canceled' instead of DOMException. Previously because of that InternetReachability aborts previous request and calls _setIsInternetReachable(false). --- src/internal/internetReachability.ts | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/internal/internetReachability.ts b/src/internal/internetReachability.ts index 2cd4dee4..0f49f06a 100644 --- a/src/internal/internetReachability.ts +++ b/src/internal/internetReachability.ts @@ -79,26 +79,19 @@ export default class InternetReachability { // Create promise that will reject after the request timeout has been reached let timeoutHandle: ReturnType; - const timeoutPromise = new Promise( - (_, reject): void => { - timeoutHandle = setTimeout((): void => { - controller.abort(); - reject('timedout'); - }, this._configuration.reachabilityRequestTimeout); - }, - ); + const timeoutPromise = new Promise((): void => { + timeoutHandle = setTimeout( + (): void => controller.abort('timedout'), + this._configuration.reachabilityRequestTimeout, + ); + }); // Create promise that makes it possible to cancel a pending request through a reject // eslint-disable-next-line @typescript-eslint/no-empty-function let cancel: () => void = (): void => {}; - const cancelPromise = new Promise( - (_, reject): void => { - cancel = (): void => { - controller.abort(); - reject('canceled'); - }; - }, - ); + const cancelPromise = new Promise((): void => { + cancel = (): void => controller.abort('canceled'); + }); const promise = Promise.race([ responsePromise, From 1cd754de6c1fb102a491af418e3b6e831f58855a Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Wed, 29 Nov 2023 12:33:46 +0000 Subject: [PATCH 2/2] chore(release): 11.1.1 [skip ci] ## [11.1.1](https://github.com/react-native-netinfo/react-native-netinfo/compare/v11.1.0...v11.1.1) (2023-11-29) ### Bug Fixes * internetReachability aborts handle cancel correctly ([#700](https://github.com/react-native-netinfo/react-native-netinfo/issues/700)) ([0a36296](https://github.com/react-native-netinfo/react-native-netinfo/commit/0a36296)) --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa61029c..43507fbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [11.1.1](https://github.com/react-native-netinfo/react-native-netinfo/compare/v11.1.0...v11.1.1) (2023-11-29) + + +### Bug Fixes + +* internetReachability aborts handle cancel correctly ([#700](https://github.com/react-native-netinfo/react-native-netinfo/issues/700)) ([0a36296](https://github.com/react-native-netinfo/react-native-netinfo/commit/0a36296)) + # [11.1.0](https://github.com/react-native-netinfo/react-native-netinfo/compare/v11.0.1...v11.1.0) (2023-11-08) diff --git a/package.json b/package.json index 294e520f..c6fabca4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@react-native-community/netinfo", - "version": "11.1.0", + "version": "11.1.1", "description": "React Native Network Info API for iOS & Android", "react-native": "src/index.ts", "types": "lib/typescript/src/index.d.ts",