mapValueStream<R> method

ValueStream<R> mapValueStream<R>(
  1. R convert(
    1. T value
    ), {
  2. bool sync = false,
})

Returns a ValueStream that converts each element of this stream to a new value using the convert function, and emits the result.

For each data event, o, in this stream, the returned stream provides a data event with the value convert(o). If convert throws, the returned stream reports it as an error event instead.

Error and done events are passed through unchanged to the returned stream.

If hasValue is true for this subject, the ValueStream.value of the returned stream will synchronously return the mapped value.

Implementation

ValueStream<R> mapValueStream<R>(R Function(T value) convert,
    {bool sync = false}) {
  var mappedSubject = ValueSubject<R>(sync: sync);
  _listen(
    (value) {
      try {
        mappedSubject.add(convert(value));
      } catch (e, st) {
        mappedSubject.addError(e, st);
      }
    },
    onError: (Object error, StackTrace? st) {
      mappedSubject.addError(error, st);
    },
    onDone: () {
      mappedSubject.close();
    },
    syncNotifyInitalValue: true,
  );
  return mappedSubject;
}