take method

  1. @override
void take(
  1. TakeCallback callback, [
  2. PatternMatcher? matcher
])
inherited

Used to register a taker. The take is resolved using the following rules

  • If the channel has buffered messages, then callback will be invoked with the next message from the underlying buffer (using Buffer.take)
  • If the channel is closed and there are no buffered messages, then callback is invoked with End
  • Otherwise callback will be queued until a message is put into the channel
  • If an optional pattern is provided then take will be processed only if the message matches the matcher.

Implementation

@override
void take(TakeCallback<T> callback, [PatternMatcher<T>? matcher]) {
  if (_isDebugMode) {
    _checkForbiddenStates();
  }

  if (_closed && _buffer.isEmpty) {
    callback._onData(End as T);
  } else if (!_buffer.isEmpty) {
    callback._onData(_buffer.take());
  } else {
    _takers.add(callback);
    callback._cancel = () => _takers.remove(callback);
  }
}