CachedCall<T>.scripted constructor
CachedCall<T>.scripted ({})
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),
);
});