Created
May 30, 2023 03:10
-
-
Save cdmunoz/697f3ceaaf89eb17524b3a22231e0e7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef FunctionOnRetry = Function( | |
dynamic ex, | |
int retryCount, | |
int retryInMilliseconds, | |
); | |
Future<void> onErrorRetry({ | |
required Future Function() doIt, | |
required FunctionOnRetry onRetry, | |
required int maxRetries, | |
required int delayMilliseconds, | |
int retryCount = 0, | |
}) async { | |
try { | |
await doIt(); | |
} catch (ex) { | |
if (retryCount < maxRetries) { | |
final nextCount = retryCount + 1; | |
final retryInMilliseconds = delayMilliseconds * nextCount; | |
await Future.delayed(Duration(milliseconds: retryInMilliseconds)); | |
onRetry(ex, nextCount, retryInMilliseconds); | |
await onErrorRetry( | |
doIt: doIt, | |
onRetry: onRetry, | |
maxRetries: maxRetries, | |
retryCount: nextCount, | |
delayMilliseconds: delayMilliseconds, | |
); | |
} else { | |
rethrow; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment