Truly batch actions with batch middleware #34
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #23
Also, check out the discussion around this on the original batch middleware PR: #18
Problem:
Calling
store.dispatch
sends the action through all the middlewares again and updates the store.So because the batch middleware dispatches each bundled action, the reducer receives each one and the state updates each time.
When using batch middleware, the store updates n times for a batch action with n actions, instead of 1 time.
Solution:
Whenever the batch middleware dispatches one of the bundled actions in a batch action, it marks it as
unwrappedFromBatch
. When this action comes back through the middlewares, the batch middleware looks for theunwrappedFromBatch
flag and, if present, does nothing (does not callnext
, so it does not reach later middlewares nor the reducer/store to update).I have used this approach with redux-saga successfully.
Tests Update
I added "integration tests" to test the behavior of the middleware in a real redux store.
At first glance, I thought the existing code would not send each bundled action to the reducer, because of this test:
However, this test is for the middleware in isolation, not in a redux store. So I added tests for the middleware behavior in a real redux store.