2024-12-03 21:47:12 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
import 'dart:ui';
|
|
|
|
|
|
2025-01-09 18:38:52 +08:00
|
|
|
import 'package:fl_clash/common/common.dart';
|
|
|
|
|
|
2025-07-31 17:09:18 +08:00
|
|
|
extension FutureExt<T> on Future<T> {
|
|
|
|
|
Future<T> withTimeout({
|
2024-12-03 21:47:12 +08:00
|
|
|
Duration? timeout,
|
2025-07-31 17:09:18 +08:00
|
|
|
String? tag,
|
2024-12-03 21:47:12 +08:00
|
|
|
VoidCallback? onLast,
|
|
|
|
|
FutureOr<T> Function()? onTimeout,
|
|
|
|
|
}) {
|
2025-07-31 17:09:18 +08:00
|
|
|
final realTimout = timeout ?? const Duration(minutes: 3);
|
|
|
|
|
Timer(realTimout + commonDuration, () {
|
2024-12-03 21:47:12 +08:00
|
|
|
if (onLast != null) {
|
|
|
|
|
onLast();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return this.timeout(
|
2025-07-31 17:09:18 +08:00
|
|
|
realTimout,
|
2024-12-03 21:47:12 +08:00
|
|
|
onTimeout: () async {
|
|
|
|
|
if (onTimeout != null) {
|
|
|
|
|
return onTimeout();
|
|
|
|
|
} else {
|
2025-07-31 17:09:18 +08:00
|
|
|
throw TimeoutException('${tag ?? runtimeType} timeout');
|
2024-12-03 21:47:12 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-31 17:09:18 +08:00
|
|
|
|
|
|
|
|
extension CompleterExt<T> on Completer<T> {
|
|
|
|
|
void safeCompleter(T value) {
|
|
|
|
|
if (isCompleted) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
complete(value);
|
|
|
|
|
}
|
|
|
|
|
}
|