addStream method

  1. @override
Future addStream (Stream<T> source, { bool cancelOnError: true })

Receives events from source and puts them into this controller's stream.

Returns a future which completes when the source stream is done.

Events must not be added directly to this controller using add, addError, close or addStream, until the returned future is complete.

Data and error events are forwarded to this controller's stream. A done event on the source will end the addStream operation and complete the returned future.

If cancelOnError is true, only the first error on source is forwarded to the controller's stream, and the addStream ends after this. If cancelOnError is false, all errors are forwarded and only a done event will end the addStream. If cancelOnError is omitted, it defaults to false.

Implementation

@override
Future<dynamic> addStream(Stream<T> source, {bool cancelOnError = true}) {
  if (_isAddingStreamItems) {
    throw StateError(
        'You cannot add items while items are being added from addStream');
  }

  final completer = Completer<T>();
  _isAddingStreamItems = true;

  source.listen((T event) {
    _add(event);
  }, onError: (dynamic e, StackTrace s) {
    _controller.addError(e, s);

    if (cancelOnError) {
      _isAddingStreamItems = false;
      completer.completeError(e);
    }
  }, onDone: () {
    _isAddingStreamItems = false;
    completer.complete();
  }, cancelOnError: cancelOnError);

  return completer.future;
}