applyStrategy<R, T> method
Future<R?>
applyStrategy<R, T>(
- AsyncBloc<
T> asyncBloc, - String key,
- SerializerBloc<
R, T> serializerBloc, - int ttlValue,
- Storage storage,
override
Implementation
@override
Future<R?> applyStrategy<R, T>(
AsyncBloc<T> asyncBloc,
String key,
SerializerBloc<R, T> serializerBloc,
int ttlValue,
Storage storage,
) async {
final cache = await read<T>(key, storage);
if (cache == null) {
return serializerBloc(
await invokeAsync<T>(asyncBloc, key, storage).onError(
(RestException<T> restError, stackTrace) {
if (restError.code == 403 || restError.code == 404) {
storage.clear(prefix: key);
return Future.error(restError);
}
return Future.value();
},
),
);
}
logCacheData(key, cache, storage: storage, ttlValue: ttlValue);
if (cache.expired(ttlValue)) {
storage.log("Fetch cache data will try and update key $key");
invokeAsync<dynamic>(asyncBloc, key, storage).catchError((error) {
storage.log(
"Update cache data failed for key $key error:${error.toString()}",
);
return null;
});
}
if (cache.data == null) {
return null;
}
return serializerBloc(cache.data as T);
}