run<T> method

Future<T> run<T>(
  1. Future<T> body()
)

Runs body as a leaf command. Concurrent callers are serialized.

Implementation

Future<T> run<T>(Future<T> Function() body) {
  if (_closed) {
    return Future<T>.error(
      StateError('LeafCommandQueue is closed'),
    );
  }
  final waiting = RunnerBenchmark.pending('queue', 'wait');
  final token = Object();
  final previous = _tail;
  final gate = Completer<void>();
  _tail = gate.future;

  return previous.catchError((_) {}).then((_) async {
    waiting?.finish();
    if (_closed) {
      throw StateError('LeafCommandQueue is closed');
    }
    _owner = token;
    try {
      return await RunnerBenchmark.async('queue', 'execute', body);
    } finally {
      if (identical(_owner, token)) {
        _owner = null;
      }
      if (!gate.isCompleted) {
        gate.complete();
      }
    }
  });
}