mapBatched<A, T> function

Future<List<T>> mapBatched<A, T>(
  1. Iterable<A> items,
  2. AsyncMapper<A, T> fn, {
  3. int batchSize = 5,
})

Batch async (e.g. 5 at a time). Roadmap #182.

Implementation

Future<List<T>> mapBatched<A, T>(
  Iterable<A> items,
  AsyncMapper<A, T> fn, {
  int batchSize = 5,
}) async {
  final List<A> list = items.toList();
  final List<T> out = <T>[];
  for (int i = 0; i < list.length; i += batchSize) {
    final batch = list.skip(i).take(batchSize).map(fn);
    final List<T> results = await Future.wait(batch, eagerError: false);
    // ignore: prefer_spread_over_addall -- accumulates across loop iterations; rebuilding via spread each pass would be O(n^2)
    out.addAll(results);
  }
  return out;
}