Skip to content

Immediately return if any of the requests succeeds #24

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
hasConnection returns immediately if any of the requests succeeds
  • Loading branch information
Daplex committed Oct 6, 2021
commit 633f3254d2c407c4af95035997cca5b2c93dbbc2
13 changes: 7 additions & 6 deletions lib/data_connection_checker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ class DataConnectionChecker {
}
}

/// Returns the results from the last check.
/// Returns the result from the last check.
/// It contains the result of the first successfully completed future
///
/// The list is populated only when [hasConnection]
/// The variable is set only when [hasConnection]
/// (or [connectionStatus]) is called.
List<AddressCheckResult> get lastTryResults => _lastTryResults;
List<AddressCheckResult> _lastTryResults = <AddressCheckResult>[];
AddressCheckResult get lastTryResult => _lastTryResult;
AddressCheckResult _lastTryResult;

/// Initiates a request to each address in [addresses].
/// If at least one of the addresses is reachable
Expand All @@ -145,9 +146,9 @@ class DataConnectionChecker {
for (var addressOptions in addresses) {
requests.add(isHostReachable(addressOptions));
}
_lastTryResults = List.unmodifiable(await Future.wait(requests));
_lastTryResult = await Future.any<AddressCheckResult>(requests);

return _lastTryResults.map((result) => result.isSuccess).contains(true);
return _lastTryResult.isSuccess;
}

/// Initiates a request to each address in [addresses].
Expand Down