async_util.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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(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>(FutureCallback<T> callback, int maxRetry,
  72. Duration interval, [Duration? timeout]) {
  73. return retryWhen(callback, maxRetry, interval, (error) => true, timeout);
  74. }
  75. static CancelableFuture<T> delay<T>(
  76. FutureCallback<T> callback, Duration interval) {
  77. Timer? timer;
  78. return CancelableFuture<T>((completer) {
  79. timer = Timer(interval, () {
  80. callback().then((value) {
  81. if (!completer.isCompleted) {
  82. completer.complete(value);
  83. }
  84. }).catchError((error) {
  85. if (!completer.isCompleted) {
  86. completer.completeError(error);
  87. }
  88. });
  89. });
  90. }, () {
  91. timer?.cancel();
  92. });
  93. }
  94. ///实际delay第一次执行的时间为delay+interval
  95. static CancelableFuture<T> interval<T>(FutureCallback<T> callback,
  96. Duration delay, Duration interval, int times) {
  97. Timer? timer;
  98. Timer? delayTimer;
  99. int counter = 0;
  100. void tick(Completer<T> completer) {
  101. if (counter < times) {
  102. callback().then((value) {
  103. if (!completer.isCompleted) {
  104. completer.complete(value);
  105. }
  106. }).catchError((error) {
  107. if (!completer.isCompleted) {
  108. completer.completeError(error);
  109. }
  110. });
  111. counter++;
  112. } else {
  113. timer?.cancel();
  114. }
  115. }
  116. return CancelableFuture<T>((completer) {
  117. delayTimer = Timer(delay, () {
  118. timer = Timer.periodic(interval, (Timer t) => tick(completer));
  119. });
  120. }, () {
  121. delayTimer?.cancel();
  122. timer?.cancel();
  123. });
  124. }
  125. }