Skip to content

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

Merged
merged 3 commits into from
Sep 20, 2024

Conversation

OlimpiaZurek
Copy link
Contributor

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

  1. Run the example app
  2. Tap the "Tap to trigger multiple refreshes" button
  3. Observe that subsequent refresh calls were prevented due to an ongoing requests
test.mov

Copy link
Contributor

@mikehardy mikehardy left a 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
Copy link
Contributor

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 ?

Copy link
Contributor Author

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.

Copy link
Contributor

@mikehardy mikehardy left a 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

@mikehardy mikehardy added the pending merge A PR that will be merged shortly, waiting for CI or final comment label Sep 20, 2024
@mikehardy mikehardy changed the title fix: track ongoing requests during refresh fix: ensure one reachability check in-flight at once / proper useEffect listener cleanup Sep 20, 2024
@mikehardy mikehardy merged commit eaed080 into react-native-netinfo:master Sep 20, 2024
2 checks passed
@mikehardy mikehardy removed the pending merge A PR that will be merged shortly, waiting for CI or final comment label Sep 20, 2024
github-actions bot pushed a commit that referenced this pull request Sep 20, 2024
## [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))
@matt-oakes
Copy link
Collaborator

🎉 This PR is included in version 11.4.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy