-
Notifications
You must be signed in to change notification settings - Fork 396
fix: ensure one reachability check in-flight at once / proper useEffect listener cleanup #732
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: ensure one reachability check in-flight at once / proper useEffect listener cleanup #732
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very interesting - I'm concerned about returning stale state in response to API requests to specifically fetch it and left a comment in that area
src/index.ts
Outdated
return _state._fetchCurrentState(); | ||
|
||
if (isRequestInProgress) { | ||
return _state.latest(); // Return the latest state if a request is already in progress |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔
If I understand this correctly, when multiple simultaneous requests are requested, only the first request will wait and get the correct / current state, all the subsequent ones (including the last one, which I presume will update the UI) will get incorrect / stale state. Is that correct?
Is there a way to have all the requests queue and wait on the first request to finish, then return the fresh information to all of them? May be possible with the creation of some Promises in an array or similar ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, this solution may return an invalid state for subsequent requests. I modified it by adding a request queue, so all requests are queued while the first one is in progress. Once the first request completes, all queued requests are resolved with the updated state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a really nifty solution to your stated problem now, and should prevent parallel external requests. Very cool, thank you
## [11.4.1](v11.4.0...v11.4.1) (2024-09-20) ### Bug Fixes * ensure one reachability check in-flight at once / proper useEffect listener cleanup ([#732](#732)) ([eaed080](eaed080))
🎉 This PR is included in version 11.4.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Overview
We use react-native-netinfo in our app and encountered an issue with the library sending multiple reachability requests in parallel.
This PR adds isRequestInProgress to track whether a reachability request is currently in progress, preventing multiple parallel requests.
Additionally, it implements proper cleanup of listeners in useEffect to prevent multiple listeners from being active simultaneously.
Test Plan
test.mov