pollUntil<R> method

Future<R> pollUntil<R>({
  1. required FutureOr<bool> check(),
  2. int maxRetry = 3,
  3. Duration retryDelay = const Duration(seconds: 1),
  4. Duration timeout = const Duration(seconds: 10),
  5. RetryDelayStrategy delayStrategy = RetryDelayStrategy.fixed,
  6. double delayFactor = 0.5,
  7. List<Duration> linearDelays = defaultLinearDelays,
})

轮询检查 this Future,条件满足后执行 this maxRetry 最大重试次数 retryDelay 重试延迟(用于 RetryDelayStrategy.fixedRetryDelayStrategy.exponential 的初始值) timeout 超时时间 delayStrategy 延迟策略 delayFactor 指数衰减的倍率因子 linearDelays 线性递增策略使用的延迟数组 返回 fn 的执行结果

Implementation

Future<R> pollUntil<R>({
  required FutureOr<bool> Function() check,
  int maxRetry = 3,
  Duration retryDelay = const Duration(seconds: 1),
  Duration timeout = const Duration(seconds: 10),
  RetryDelayStrategy delayStrategy = RetryDelayStrategy.fixed,
  double delayFactor = 0.5,
  List<Duration> linearDelays = defaultLinearDelays,
}) async {
  final start = DateTime.now();
  int attempt = 0;
  while (DateTime.now().difference(start) < timeout && attempt < maxRetry) {
    if (await check()) {
      return await this;
    }
    attempt++;
    if (attempt < maxRetry) {
      await Future.delayed(
        _computeDelay(
          attempt: attempt,
          baseDelay: retryDelay,
          strategy: delayStrategy,
          delayFactor: delayFactor,
          linearDelays: linearDelays,
        ),
      );
    }
  }
  throw Exception('Poll timed out or max retries exceeded');
}