hydrate method

  1. @visibleForTesting
  2. @protected
Future<Data?> hydrate({
  1. bool refreshAfter = true,
})

Gets the data from the cache, if it exists, and emits it to the stream.

Implementation

@visibleForTesting
@protected
Future<Data?> hydrate({bool refreshAfter = true}) async {
  return hydratationFiber.run(() async {
    final stopwatch = Stopwatch()..start();
    try {
      final cachedDataString = await storage.read(key: key);

      if (cachedDataString != null) {
        return await _emitRawData(cachedDataString);
      }
    } on FormatException catch (e) {
      logger.call(
        'Repository($name): Error while hydrating repository $key: $e.'
        ' The cache will be cleared.',
      );

      await clearCache();
    } finally {
      stopwatch.stop();
      logger.call(
        'Repository($name): '
        'hydrated in ${stopwatch.elapsedMilliseconds}ms',
      );

      if (refreshAfter) {
        await refresh();
      }
    }
    return null;
  });
}