run method

Future<Current> run({
  1. CryptoCancellationToken? cancellationToken,
})

Executes this flow exactly once and returns its final typed value.

Cancellation is cooperative and is checked before and after the source and every step. An active operation is allowed to finish; its result is then discarded when cancellation is observed.

Source and step failures are wrapped in CryptoFlowException with the original cause and stack trace. Reuse throws CryptoFlowStateException, while observed cancellation throws CryptoFlowCancelledException.

Implementation

Future<Current> run({CryptoCancellationToken? cancellationToken}) async {
  if (_hasRun) {
    throw CryptoFlowStateException('A crypto flow can only be run once');
  }
  _hasRun = true;

  final token = cancellationToken ?? CryptoCancellationToken();
  final cleanup = _CleanupStack();
  try {
    token._throwIfCancelled();
    var current = await _invokeFlowStage(_source.name, 0, _source.operation);
    token._throwIfCancelled();
    for (var index = 0; index < _stages.length; index++) {
      final stage = _stages[index];
      token._throwIfCancelled();
      current = await _invokeFlowStage(
        stage.name,
        index + 1,
        () => stage.execute(current),
      );
      token._throwIfCancelled();
    }
    return current as Current;
  } finally {
    await cleanup.releaseAll();
  }
}