flatMap<R> method

OffsetIterator<R> flatMap<R>(
  1. OffsetIterator<R> pred(
    1. T item
    ), {
  2. String name = 'flatMap',
  3. int retention = 0,
  4. SeedCallback<R>? seed,
  5. bool? cancelOnError,
  6. bool bubbleCancellation = true,
})

Implementation

OffsetIterator<R> flatMap<R>(
  OffsetIterator<R> Function(T item) pred, {
  String name = 'flatMap',
  int retention = 0,
  SeedCallback<R>? seed,
  bool? cancelOnError,
  bool bubbleCancellation = true,
}) {
  final parent = this;

  return OffsetIterator(
    name: toStringWithChild(name),
    process: (acc) async {
      var child = acc as OffsetIterator<R>?;

      if (child == null) {
        final itemFuture = parent.pull();
        final item = itemFuture is Future ? await itemFuture : itemFuture;
        child = item.p(O.fold(() => null, pred));
      }

      if (child != null) {
        final itemFuture = child.pull();
        final item = itemFuture is Future ? await itemFuture : itemFuture;
        final childHasMore = child.hasMore();

        return OffsetIteratorState(
          acc: childHasMore ? child : null,
          chunk: item.p(O.fold(() => null, (v) => [v])),
          hasMore: childHasMore || parent.hasMore(),
        );
      }

      return OffsetIteratorState(
        acc: offset,
        hasMore: parent.hasMore(),
      );
    },
    cleanup: parent.generateCleanup(bubbleCancellation: bubbleCancellation),
    cancelOnError: cancelOnError ?? parent.cancelOnError,
    seed: seed,
    retention: retention,
  );
}