polling function
通用轮询逻辑
Implementation
@Deprecated('使用retry代替')
Future<void> polling({
required Future<void> Function() task,
required Duration interval,
required int maxTryCount,
Future<void> Function()? whenErrorTry,
}) async {
int tryCount = 0;
while (true) {
if (tryCount < maxTryCount) {
tryCount++;
L.d('执行第$tryCount次轮询');
try {
await task();
L.d('第$tryCount次轮询执行成功');
// 成功就马上break
break;
} catch (e) {
String message = '第$tryCount次轮询失败, 错误信息: $e';
if (whenErrorTry != null) {
L.d('$message 开始执行错误重试');
await whenErrorTry();
} else {
L.d('$message 未配置错误重试, 抛出异常');
rethrow;
}
}
await Future.delayed(interval);
} else {
L.d('超出轮询尝试次数');
throw '超出轮询尝试次数';
}
}
L.d('轮询执行结束');
}