-
Notifications
You must be signed in to change notification settings - Fork 85
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
Added retryIfValue and onRetryIfValue to support retry on returned value condition #55
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -44,6 +44,7 @@ final _rand = math.Random(); | |||||
/// () => http.get('https://google.com').timeout(Duration(seconds: 5)), | ||||||
/// // Retry on SocketException or TimeoutException | ||||||
/// retryIf: (e) => e is SocketException || e is TimeoutException, | ||||||
/// retryIfValue: (v) => v == 500, | ||||||
/// ); | ||||||
/// print(response.body); | ||||||
/// ``` | ||||||
|
@@ -110,9 +111,9 @@ class RetryOptions { | |||||
} | ||||||
|
||||||
/// Call [fn] retrying so long as [retryIf] return `true` for the exception | ||||||
/// thrown. | ||||||
/// thrown, or if [retryIfValue] returns `true` for the value returned from [fn]. | ||||||
/// | ||||||
/// At every retry the [onRetry] function will be called (if given). The | ||||||
/// At every retry the [onRetry] and [onRetryIfValue] functions will be called (if given). The | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't be an OR, either And don't we need to explain which is called when... |
||||||
/// function [fn] will be invoked at-most [this.attempts] times. | ||||||
/// | ||||||
/// If no [retryIf] function is given this will retry any for any [Exception] | ||||||
|
@@ -122,13 +123,22 @@ class RetryOptions { | |||||
FutureOr<T> Function() fn, { | ||||||
FutureOr<bool> Function(Exception) retryIf, | ||||||
FutureOr<void> Function(Exception) onRetry, | ||||||
FutureOr<bool> Function(dynamic) retryIfValue, | ||||||
FutureOr<void> Function(dynamic) onRetryIfValue, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Why the if here? Also why are these |
||||||
}) async { | ||||||
int attempt = 0; | ||||||
// ignore: literal_only_boolean_expressions | ||||||
while (true) { | ||||||
attempt++; // first invocation is the first attempt | ||||||
try { | ||||||
return await fn(); | ||||||
var value = await fn(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if (retryIfValue == null || !(await retryIfValue(value))) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Shouldn't there also be a |
||||||
return value; | ||||||
} else { | ||||||
if (onRetryIfValue != null) { | ||||||
await onRetryIfValue(value); | ||||||
} | ||||||
} | ||||||
} on Exception catch (e) { | ||||||
if (attempt >= maxAttempts || | ||||||
(retryIf != null && !(await retryIf(e)))) { | ||||||
|
@@ -179,10 +189,18 @@ Future<T> retry<T>( | |||||
int maxAttempts = 8, | ||||||
FutureOr<bool> Function(Exception) retryIf, | ||||||
FutureOr<void> Function(Exception) onRetry, | ||||||
FutureOr<bool> Function(dynamic) retryIfValue, | ||||||
FutureOr<void> Function(dynamic) onRetryIfValue, | ||||||
}) => | ||||||
RetryOptions( | ||||||
delayFactor: delayFactor, | ||||||
randomizationFactor: randomizationFactor, | ||||||
maxDelay: maxDelay, | ||||||
maxAttempts: maxAttempts, | ||||||
).retry(fn, retryIf: retryIf, onRetry: onRetry); | ||||||
).retry( | ||||||
fn, | ||||||
retryIf: retryIf, | ||||||
onRetry: onRetry, | ||||||
retryIfValue: retryIfValue, | ||||||
onRetryIfValue: onRetryIfValue, | ||||||
); |
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 example won't work