async_util.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import 'dart:async';
  2. import 'package:flutter/widgets.dart';
  3. import 'async_typeof.dart';
  4. import 'cancel_future.dart';
  5. class AsyncUtil {
  6. AsyncUtil._();
  7. static CancelableFuture<T> retryWithExponentialBackoff<T>(
  8. FutureCallback<T> callback, int maxRetry, Predicate<dynamic>? predicate) {
  9. const Duration initialInterval = Duration(seconds: 1);
  10. int retryCount = 0;
  11. Timer? timer;
  12. void attempt(Completer<T> completer) {
  13. callback().then((value) {
  14. if (!completer.isCompleted) {
  15. completer.complete(value);
  16. }
  17. }).catchError((error) {
  18. if (retryCount < maxRetry && (predicate == null || predicate(error))) {
  19. retryCount++;
  20. Duration nextInterval = initialInterval * (1 << (retryCount - 1));
  21. timer = Timer(nextInterval, () => attempt(completer));
  22. } else {
  23. if (!completer.isCompleted) {
  24. completer.completeError(error);
  25. }
  26. }
  27. });
  28. }
  29. return CancelableFuture<T>((completer) {
  30. attempt(completer);
  31. }, () {
  32. timer?.cancel();
  33. });
  34. }
  35. static CancelableFuture<T> retryWhen<T>(FutureCallback<T> callback,
  36. int maxRetry, Duration interval, Predicate<dynamic> predicate,
  37. [Duration? timeout]) {
  38. int retryCount = 0;
  39. Timer? timer;
  40. Timer? timeoutTimer;
  41. void attempt(Completer<T> completer) {
  42. callback().then((value) {
  43. if (!completer.isCompleted) {
  44. completer.complete(value);
  45. }
  46. }).catchError((error) {
  47. if ((maxRetry <= 0 || retryCount < maxRetry) && predicate(error)) {
  48. retryCount++;
  49. timer = Timer(interval, () => attempt(completer));
  50. } else {
  51. if (!completer.isCompleted) {
  52. completer.completeError(error);
  53. }
  54. }
  55. });
  56. }
  57. return CancelableFuture<T>((completer) {
  58. if (timeout != null) {
  59. timeoutTimer = Timer(timeout, () {
  60. if (!completer.isCompleted) {
  61. completer.completeError(TimeoutException('Operation timed out'));
  62. }
  63. });
  64. }
  65. attempt(completer);
  66. }, () {
  67. timer?.cancel();
  68. timeoutTimer?.cancel();
  69. });
  70. }
  71. static CancelableFuture<T> retry<T>(
  72. FutureCallback<T> callback, int maxRetry, Duration interval,
  73. [Duration? timeout]) {
  74. return retryWhen(callback, maxRetry, interval, (error) => true, timeout);
  75. }
  76. static CancelableFuture<T> delay<T>(
  77. FutureCallback<T> callback, Duration interval) {
  78. Timer? timer;
  79. return CancelableFuture<T>((completer) {
  80. timer = Timer(interval, () {
  81. callback().then((value) {
  82. if (!completer.isCompleted) {
  83. completer.complete(value);
  84. }
  85. }).catchError((error) {
  86. if (!completer.isCompleted) {
  87. completer.completeError(error);
  88. }
  89. });
  90. });
  91. }, () {
  92. timer?.cancel();
  93. });
  94. }
  95. ///实际delay第一次执行的时间为delay+interval
  96. static CancelableFuture<T> interval<T>(FutureCallback<T> callback,
  97. Duration delay, Duration interval, int times) {
  98. Timer? timer;
  99. Timer? delayTimer;
  100. int counter = 0;
  101. void tick(Completer<T> completer) {
  102. if (counter < times) {
  103. callback().then((value) {
  104. if (!completer.isCompleted) {
  105. completer.complete(value);
  106. }
  107. }).catchError((error) {
  108. if (!completer.isCompleted) {
  109. completer.completeError(error);
  110. }
  111. });
  112. counter++;
  113. } else {
  114. timer?.cancel();
  115. }
  116. }
  117. return CancelableFuture<T>((completer) {
  118. delayTimer = Timer(delay, () {
  119. timer = Timer.periodic(interval, (Timer t) => tick(completer));
  120. });
  121. }, () {
  122. delayTimer?.cancel();
  123. timer?.cancel();
  124. });
  125. }
  126. }