listenToStream<T> method

  1. @mustCallSuper
  2. @override
StreamSubscription<T> listenToStream <T>(Stream<T> stream, void onData(T event), { Function onError, void onDone(), bool cancelOnError })
@mustCallSuper, override

Returns a StreamSubscription which handles events from the stream using the provided onData, onError and onDone handlers.

Consult documentation for Stream.listen for more info.

If the returned StreamSubscription is cancelled manually (i.e. canceled before disposal of the parent object) Disposable will clean up the internal reference allowing the subscription to be garbage collected.

Neither parameter may be null.

Implementation

@mustCallSuper
@override
StreamSubscription<T> listenToStream<T>(
    Stream<T> stream, void onData(T event),
    {Function onError, void onDone(), bool cancelOnError}) {
  _throwOnInvalidCall2('listenToStream', 'stream', 'onData', stream, onData);
  var managedStreamSubscription = new ManagedStreamSubscription(
      stream, onData,
      onError: onError, onDone: onDone, cancelOnError: cancelOnError);
  _logManageMessage(managedStreamSubscription);

  var disposable = new ManagedDisposer(() {
    _logUnmanageMessage(managedStreamSubscription);
    return managedStreamSubscription.cancel();
  });

  _internalDisposables.add(disposable);

  managedStreamSubscription.didComplete.then((_) {
    // ignore: deprecated_member_use
    if (!isDisposedOrDisposing) {
      _logUnmanageMessage(disposable);
      _internalDisposables.remove(disposable);
    }
  });

  return managedStreamSubscription;
}