take method

Uint8List take()

Take a message from the mailbox.

If mailbox is empty then take will synchronously block until message is available or mailbox is closed. If mailbox is closed then take will throw StateError.

Implementation

Uint8List take() => _mutex.runLocked(() {
      while (_mailbox.ref.state == _stateEmpty) {
        _condVar.wait(_mutex);
      }

      if (_mailbox.ref.state == _stateClosed) {
        throw StateError('Mailbox is closed');
      }

      final result = _toList(_mailbox.ref.buffer, _mailbox.ref.bufferLength);

      _mailbox.ref.state = _stateEmpty;
      _mailbox.ref.buffer = nullptr;
      _mailbox.ref.bufferLength = 0;
      return result;
    });