pollUntil<R> method
轮询检查 this Future,条件满足后执行 this
maxRetry 最大重试次数
retryDelay 重试延迟(用于 RetryDelayStrategy.fixed 和 RetryDelayStrategy.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');
}