load method

Stream<Resource<V>> load({
  1. bool forceReload = false,
  2. void doOnStore(
    1. V
    )?,
  3. bool allowEmptyLoading = false,
  4. ResourceFetchArguments? fetchArguments,
})

Trigger resource loading from cache or with _fetch. forceReload reload even if cache is valid. allowEmptyLoading put empty loading to prevent previous SUCCESS to return. doOnStore callback to modify data before putting it in the storage. fetchArguments additional arguments passed to _fetch if provided.

Implementation

Stream<Resource<V>> load({
  bool forceReload = false,
  void Function(V)? doOnStore,
  bool allowEmptyLoading = false,
  final ResourceFetchArguments? fetchArguments,
}) {
  if (!_isLoading) {
    _isLoading = true;
    _lock.synchronized(() async {
      _shouldReload = false;
      // try always starting with loading value
      if (allowEmptyLoading || _subject.hasValue) {
        // prevent previous SUCCESS to return
        _subject.add(Resource.loading(_subject.valueOrNull?.data));
      }
      await _loadProcess(forceReload, doOnStore, fetchArguments);
    }).then((_) {
      _isLoading = false;
      if (_shouldReload) {
        load(
          forceReload: forceReload,
          doOnStore: doOnStore,
          allowEmptyLoading: allowEmptyLoading,
        );
      }
    });
  } else if (forceReload) {
    // don't need to call load many times
    // perform another load only once
    _shouldReload = true;
  }
  return _subject;
}