flush method

Future<void> flush()

Flush all existing messages from the queue.

Implementation

Future<void> flush() async {
  while (true) {
    // Don't call queue._next directly as it will
    // eat the next available message even if it
    // timed out. Use hasNext instead.
    var f = _queue.hasNext.timeout(Duration(microseconds: 1));
    try {
      bool hasNext = await f;
      if (!hasNext) {
        // The stream has closed, bail out!
        return;
      }
      await _queue.next;
      // consume the data and throw it away.
    } on TimeoutException {
      // Timeout occured, we are done, no more data
      // available.
      return;
    }
  }
}