persist method

OffsetIterator<T> persist({
  1. required Storage storage,
  2. Map<String, dynamic>? cache,
  3. required String key,
  4. bool equals(
    1. T prev,
    2. T next
    )?,
  5. required Object? toJson(
    1. T
    ),
  6. required T fromJson(
    1. Object?
    ),
  7. SeedCallback<T>? fallbackSeed,
  8. int retention = 0,
  9. String name = 'persist',
  10. bool? cancelOnError,
  11. bool bubbleCancellation = true,
})

Implementation

OffsetIterator<T> persist({
  required Storage storage,
  Map<String, dynamic>? cache,
  required String key,
  bool Function(T prev, T next)? equals,
  required Object? Function(T) toJson,
  required T Function(Object?) fromJson,
  SeedCallback<T>? fallbackSeed,
  int retention = 0,
  String name = 'persist',
  bool? cancelOnError,
  bool bubbleCancellation = true,
}) {
  final currentValue = _readCache(storage, cache, key, fromJson);
  final write = _writeCache(storage, cache, key, toJson);
  final seed = currentValue.p(O.fold(
    () => generateSeed(fallback: fallbackSeed),
    (v) => () => O.some(v),
  ));
  Option<T> prev = const O.None();

  return tap(
    (item) {
      Future.microtask(() {
        if (O.isSome(prev) &&
            equals != null &&
            equals((prev as O.Some).value, item)) {
          return;
        }

        write(item);
        prev = O.Some(item);
      });
    },
    seed: () {
      if (seed != null) prev = seed();
      return prev;
    },
    retention: retention,
    name: name,
    cancelOnError: cancelOnError,
    bubbleCancellation: bubbleCancellation,
  );
}