parallelMap<V> method

Stream<V> parallelMap<V>(
  1. Future<V> mapper(
    1. T t
    )
)

Unlike asyncMap and semaphoreMap: the order of the output is not guaranteed. As soon as a Future is completed, it is yielded. Once all Futures are completed, the Stream is completed.

Implementation

Stream<V> parallelMap<V>(Future<V> Function(T t) mapper) async* {
  List<Future<V>> work = [];
  BehaviorSubject<V> emit = BehaviorSubject();

  await for (T i in this) {
    Future<V> job = mapper(i);
    work.add(job);
    job.then(emit.add);
  }

  Future closer = emit.close();
  yield* emit.stream;
  await closer;
  await Future.wait(work);
}