close method

  1. @override
Future<void> close()
override

Tells the stream sink that no further streams will be added.

This allows the stream sink to complete any remaining work and release resources that are no longer needed

Returns a future which is completed when the stream sink has shut down. If cleaning up can fail, the error may be reported in the returned future, otherwise it completes with null.

Returns the same future as done.

The stream sink may close before the close method is called, either due to an error or because it is itself providing events to someone who has stopped listening. In that case, the done future is completed first, and the close method will return the done future when called.

Unifies StreamConsumer.close and EventSink.close which both mark their object as not expecting any further events.

Implementation

@override
Future<void> close() {
  if (_doneCompleter.isCompleted) {
    return _doneCompleter.future;
  }

  Future<void> doClose() async {
    add(Dispose());

    _sendController.close();
    // _rawReceiveController and _transformedReceiveController are closed by the piped
    // receive port stream being closed.

    // Give the isolate time to shut down cleanly, but kill it if it takes too long.
    try {
      // Wait for disconnection confirmation.
      await firstWhere((message) => message is Disconnecting).then(drain).timeout(const Duration(seconds: 5));
    } on TimeoutException {
      logger.warning('Isolate took too long to shut down, killing it');
      isolate.kill(priority: Isolate.immediate);
    }
  }

  _doneCompleter.complete(doClose());
  return _doneCompleter.future;
}