manageStreamController method

  1. @mustCallSuper
  2. @override
void manageStreamController(
  1. StreamController controller
)
override

Automatically cancel a stream controller when this object is disposed.

The parameter may not be null.

Implementation

@mustCallSuper
@override
void manageStreamController(StreamController<dynamic> controller) {
  _throwOnInvalidCall('manageStreamController', 'controller', controller);
  // If a single-subscription stream has a subscription and that
  // subscription is subsequently canceled, the `done` future will
  // complete, but there is no other way for us to tell that this
  // is what has happened. If we then listen to the stream (since
  // closing a stream that was never listened to never completes) we'll
  // get an exception. This workaround allows us to "know" when a
  // subscription has been canceled so we don't bother trying to
  // listen to the stream before closing it.
  _logManageMessage(controller);

  bool isDone = false;

  var disposable = ManagedDisposer(() {
    if (!controller.hasListener && !controller.isClosed && !isDone) {
      controller.stream.listen((_) {});
    }
    return controller.close();
  });

  controller.done.then((_) {
    isDone = true;
    if (!_isDisposedOrDisposing) {
      _logUnmanageMessage(controller);
      _internalDisposables.remove(disposable);
    }
    disposable.dispose();
  });

  _internalDisposables.add(disposable);
}