sleep method
Wait for duration while observing cancellation.
Implementations must reject negative durations. A cancellation request is cooperative: it ends this wait, but it cannot forcefully cancel unrelated Dart Futures.
Implementation
@override
Future<void> sleep(Duration duration, CancellationSignal cancellation) async {
if (duration.isNegative) {
throw ArgumentError.value(duration, 'duration', 'must not be negative');
}
cancellation.throwIfCancelled();
if (duration == Duration.zero) {
await Future<void>.value();
cancellation.throwIfCancelled();
return;
}
final pending = _ManualSleep(
deadline: _now.add(duration),
completer: Completer<void>.sync(),
);
_pending.add(pending);
try {
await Future.any<void>(<Future<void>>[
pending.completer.future,
cancellation.whenCancelled,
]);
cancellation.throwIfCancelled();
} finally {
_pending.remove(pending);
}
}