Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Caching / Aborting actions based on state #50

Open
jaredpalmer opened this issue Apr 13, 2017 · 2 comments
Open

[Question] Caching / Aborting actions based on state #50

jaredpalmer opened this issue Apr 13, 2017 · 2 comments

Comments

@jaredpalmer
Copy link

So a common pattern in redux-thunk land is to do something like:

// pseudo code

const loadListingsWithFilter = (filter) => (dispatch, getState) => {
   // bailout early if the data is fresh
   if (getTimeLastFetchedByFilter(getState(), filter) < 15 minutes) {
       return;
   }
   // normal thunk stuff
   ...
}

How does this work with redux-pack?

@Mesoptier
Copy link

Using redux-pack should not prohibit you from using redux-thunk. (Hell, the readme seems to suggest adding both.) So you should feel free to use redux-pack for handling async actions, and redux-thunk for conditional actions.

For example:

const loadListings = (filter) => {
    return {
        type: 'LOAD_LISTINGS',
        promise: Api.loadListings(filter),
    };
};

const loadListingsIfNeeded = (filter) => (dispatch, getState) => {
    // Bailout early if the data is fresh
    if (!isLoadNeeded(getState(), filter)) {
        return;
    }

    dispatch(loadListings(filter));
};

@pbomb
Copy link

pbomb commented Jun 27, 2017

Dropping down to redux-thunk is less than ideal, though. It's such a general API and allows devs/teams too much power to make bad decisions. It would be awesome to see something like this added to the API to handle this common scenario.

const loadListings = (filter) => {
  return {
    type: 'LOAD_LISTINGS',
    promise: Api.loadListings(filter),
    conditional: (state) => isLoadNeeded(state, filter)

Would this library be open to adding a feature like this? I could submit a pull request to add it if there's openness. I've used a strategy like this before and found it very useful - it was used in most of our data-fetching scenarios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants