alt
alt copied to clipboard
Data Sources: give Source methods access to isLoading()
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();
}
:+1:
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.
+1