asyncExpand method - Stream class - dart:async library (original) (raw)

Stream<E> asyncExpand<E>(

  1. Stream<E>? convert(
    1. T event
      ) )

Transforms each element into a sequence of asynchronous events.

Returns a new stream and for each event of this stream, do the following:

The returned stream is a broadcast stream if this stream is.

Implementation

Stream<E> asyncExpand<E>(Stream<E>? convert(T event)) {
  _StreamControllerBase<E> controller;
  if (isBroadcast) {
    controller = _SyncBroadcastStreamController<E>(null, null);
  } else {
    controller = _SyncStreamController<E>(null, null, null, null);
  }

  controller.onListen = () {
    StreamSubscription<T> subscription = this.listen(
      null,
      onError: controller._addError, // Avoid Zone error replacement.
      onDone: controller.close,
    );
    subscription.onData((T event) {
      Stream<E>? newStream;
      try {
        newStream = convert(event);
      } catch (e, s) {
        controller.addError(e, s);
        return;
      }
      if (newStream != null) {
        subscription.pause();
        controller.addStream(newStream).whenComplete(subscription.resume);
      }
    });
    controller.onCancel = subscription.cancel;
    if (!isBroadcast) {
      controller
        ..onPause = subscription.pause
        ..onResume = subscription.resume;
    }
  };
  return controller.stream;
}