CachedCall<T>.scripted constructor

  1. @visibleForTesting
CachedCall<T>.scripted({
  1. T local()?,
  2. required FutureOr<T> server(),
  3. bool same(
    1. T local,
    2. T server
    )?,
})

A call that plays a fixed script, for tests: local (if given) as the saved copy under CacheStrategy.localThenServer, then server. same decides Cached.sameAsLocal; it defaults to ==.

Implementation

@visibleForTesting
factory CachedCall.scripted({
  T Function()? local,
  required FutureOr<T> Function() server,
  bool Function(T local, T server)? same,
}) =>
    CachedCall((strategy) async* {
      T? saved;
      var hadLocal = false;
      if (local != null && strategy == CacheStrategy.localThenServer) {
        saved = local();
        hadLocal = true;
        yield Cached(saved as T, source: CacheSource.local);
      }
      final fresh = await server();
      yield Cached(
        fresh,
        source: CacheSource.server,
        sameAsLocal:
            hadLocal && (same ?? (a, b) => a == b)(saved as T, fresh),
      );
    });