prefetch method

OffsetIterator<T> prefetch({
  1. String name = 'prefetch',
  2. bool? cancelOnError,
  3. bool bubbleCancellation = true,
})

Eagerly load the next item in the OffsetIterator.

This ensures the the parent OffsetIterator is processing the next item before the child needs it.

Implementation

OffsetIterator<T> prefetch({
  String name = 'prefetch',
  bool? cancelOnError,
  bool bubbleCancellation = true,
}) {
  final parent = this;

  return OffsetIterator(
    name: toStringWithChild(name),
    seed: parent.generateSeed(),
    init: () => parent.offset,
    process: (offset) async {
      final futureOr = parent.pull(offset);
      final item = futureOr is Future ? await futureOr : futureOr;

      final newOffset = offset + 1;
      final hasMore = parent.hasMore(newOffset);
      if (hasMore) parent.pull(newOffset);

      return OffsetIteratorState(
        acc: newOffset,
        chunk: item is Some ? [(item as Some).value] : null,
        hasMore: hasMore,
      );
    },
    cleanup: parent.generateCleanup(bubbleCancellation: bubbleCancellation),
    cancelOnError: cancelOnError ?? parent.cancelOnError,
  );
}