applyStrategy<R, T> method

  1. @override
Future<R?> applyStrategy<R, T>(
  1. AsyncBloc<T> asyncBloc,
  2. String key,
  3. SerializerBloc<R, T> serializerBloc,
  4. int ttlValue,
  5. 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);
}