stop method

Future<void> stop()

Stops the consumer, tears the live connection down and writes out the pending cursor, after which start returns.

A healthy firehose stream never ends, so stopping cannot mean "wait for the stream to finish": this cancels the in-flight subscription, closes the socket and wakes any pending reconnect sleep. Safe to call more than once, and safe to call before or during start — a connection that is still being established is closed as soon as it opens.

Implementation

Future<void> stop() async {
  if (!_stopSignal.isCompleted) _stopSignal.complete();

  final subscription = _subscription;
  _subscription = null;
  await subscription?.cancel();

  // Cancelling a subscription never delivers a done event, so release
  // `_consume` explicitly or `start` would wait on it forever.
  final consumed = _consumed;
  _consumed = null;
  if (consumed != null && !consumed.isCompleted) consumed.complete();

  final connection = _connection;
  _connection = null;
  await _closeQuietly(connection);

  await _flushCursor(force: true);
}