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

Pass through dispatch action to batch function #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Pass through dispatch action to batch function #13

wants to merge 1 commit into from

Conversation

ryardley
Copy link

Having the action available at the batching function level is important for calculating batch exclusion based on action type. This is important for things like transactional batching triggered by specific actions which is a use case we came across.

Having the action available at the batching function level is
important for calculating batch exclusion based on action type.
@ryardley
Copy link
Author

ryardley commented Oct 22, 2016

I just saw a second PR from January un-merged that does exactly the same thing. Are there any issues with this?

@tappleby
Copy link
Owner

Looking into getting this PR merged soon. Just evaluating the impact future redux store enhancer api changes might have on this:

@sagar-sm
Copy link

sagar-sm commented May 4, 2017

I was just about to implement this exact same functionality when I came across this PR. @tappleby
Is there an ETA on merging this? reduxjs/redux#1702 and reduxjs/redux#2214 (comment) are still in review but seem to be stale since January.

@paynecodes
Copy link

paynecodes commented Nov 14, 2017

@tappleby Any updates here?

I understand that this is something that the Redux team isn't offering positive feedback in this direction based on the linked issues, but I'm struggling to see how this lib can be used without receiving the action (controlled components). Am I missing something?

@staab
Copy link

staab commented Dec 8, 2017

I also started implementing this same functionality, then I forked @sagar-sm's version - but then I realized that this stuff is pretty easy to compose to get whatever behavior you need (in my case, at the expense of some global mutable variables - but not terribly hacky, you could easily encapsulate them if you wanted). Here's what I did:

batching/state.js

export default {notify: null, paused: false}

batching/enhancer.js

import {batchedSubscribe} from 'redux-batched-subscribe'
import State from 'modules/batch-actions/state'

export default batchedSubscribe(freshNotify => {
  State.notify = freshNotify
})

batching/middleware.js

import State from 'modules/batch-actions/state'

export default store => next => action => {
  if (action.type === 'PAUSE_SUBS') {
    State.paused = true
  }

  if (action.type === 'RESUME_SUBS') {
    State.paused = false
  }

  next(action)

  // Wait all actions has been applied to the store to notify
  if (!State.paused) {
    State.notify()
  }
}

Once you stick the middleware and the enhancer in the right place, you can imperatively persist store changes to your components, e.g.:

dispatch({type: 'PAUSE_SUBS'})
dispatch({type: 'LEAVES_STORE_IN_INCONSISTENT_STATE'})
dispatch({type: 'FIXES_INCONSISTENT_STATE'})
dispatch({type: 'RESUME_SUBS'})

This suited my needs just fine, but isn't really ideal since the store's state should probably be in a consistent state at all times, but unfortunately we didn't develop the right abstractions to do that for our app until it was too late and a refactor had become expensive.

Hopefully this helps someone else out (I noticed sagar-sm's fork has 22 forks of its own) - and of course, my implementation is only like 20 lines, so you could do something very different in the middleware to suit your needs.

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

Successfully merging this pull request may close these issues.

5 participants