wait method

Future<void> wait({
  1. CancellationToken? token,
})

Paused execution of the calling code while the token is in the paused state. If the token is not in the paused state, then execution of the calling code continues immediately.

Parameters:

  • token: A cancellation token. If a token value is specified, the method will throw a CancellationException exception if cancellation was requested.

Implementation

Future<void> wait({CancellationToken? token}) async {
  if (token != null) {
    token.throwIfCanceled();
  }

  if (!_isPaused) {
    return;
  }

  if (token == null) {
    return _event.wait();
  }

  final completer = Completer<void>();
  final handler = token.addHandler(completer.tryComplete);
  await Future.any([_event.wait(), completer.future]);
  completer.tryComplete();
  token.removerHandler(handler);
  token.throwIfCanceled();
}