startTransaction method

StreamQueueTransaction<T> startTransaction ()

Requests a transaction that can conditionally consume events.

The transaction can create copies of this queue at the current position using StreamQueueTransaction.newQueue. Each of these queues is independent of one another and of the parent queue. The transaction finishes when one of two methods is called:

Until the transaction finishes, this queue won't emit any events.

See also withTransaction and cancelable.

/// Consumes all empty lines from the beginning of [lines].
Future consumeEmptyLines(StreamQueue<String> lines) async {
  while (await lines.hasNext) {
    var transaction = lines.startTransaction();
    var queue = transaction.newQueue();
    if ((await queue.next).isNotEmpty) {
      transaction.reject();
      return;
    } else {
      transaction.commit(queue);
    }
  }
}

Implementation

StreamQueueTransaction<T> startTransaction() {
  if (_isClosed) throw _failClosed();

  var request = _TransactionRequest(this);
  _addRequest(request);
  return request.transaction;
}