alt icon indicating copy to clipboard operation
alt copied to clipboard

Data Sources: give Source methods access to isLoading()

Open jfsiii opened this issue 10 years ago • 3 comments

One common pattern for Data Sources is to only fetch if a request is not already in progress. shouldFetch seems like an appropriate place for this logic, but it doesn't have access to isLoading.

If it did, the behavior could be implemented with

shouldFetch: (state, args) {
  return !this.isLoading();
}

jfsiii avatar Sep 11 '15 20:09 jfsiii

:+1:

goatslacker avatar Sep 11 '15 21:09 goatslacker

We keep this state in our stores.

this.state = new Immutable.Map({
  entities: new Immutabe.Map(),
  isLoading: false,
  hasLoaded: false,
  hasFailed: false,

While it's a bit more to write our shouldFetches look something like this:

shouldFetch(state) {
  if (state.get('isLoading')) {
    return false;
  } else if (state.get('hasLoaded')) {
    return state.get('entities').any(e => e._expired);
  } else {
    return state.get('hasLoaded') === false;
  }
}

It doesn't look super pretty. For me I want to know whether or not a resource is loading in my components so I keep it in my store, which fits nicely with hasLoaded & hasFailed as well.

mull avatar Sep 14 '15 08:09 mull

+1

3plusalpha avatar Oct 05 '15 13:10 3plusalpha