Redux Batch Middleware
Batch Redux actions.
Installation
% npm install redux-batch-actions
Usage
;;; const store = ; store; store; // on next tick:// State is: {foo: 'bar', fuz: 'bus'}
Merge
By default, batch-actions merges your data objects into a single action.
If you want to customize this, include it as a configuration with your middleware.
;;; const store = ; store; store; // on next tick:// Action is: {type: 'GRAPH', myProp: {foo: 'bar', fuz: 'bus'}}
If you pass in a function as your type config, we'll assume that it's the merge function
;; const store = ; // same as above
Batch
If you don't include the action type when you configure your middleware, just include batch as an argument with your action type
;;; const store = ; store; store; // on next tick:// State is: {foo: 'bar', fuz: 'bus'}
You can also pass in batch config this way.
;; const store = ; store; store; // on next tick:// Action is: {type: 'GRAPH', myProp: {foo: 'bar', fuz: 'bus'}}
Batch Complete
Want to ensure the batch fires synchronously? You'll need to dispatch the batched action with one additional property: batchComplete
.
;;; const store = ; store; store; store; // Action is: {type: 'GRAPH', data: {foo: 'bar', fuz: 'bus'}}
Batch Purge
Want to stop a batch from going out? Add batchPurge
to an action and we'll clear it.
;;; const store = ; store; store; store; // No Action fired on current tick or next
Configuration
Tick
This batching relies on a tick function, that is - any function that we know will fire next time there is a javascript event loop.
By default, we use setTimeout([fn], 1)
on the browser and process.nextTick([fn])
on the server. You can change this, however, by passing it in as an option when you declare your middleware.
const store = ;
Finalize
Once a batch is going to be committed, we'll run it through a finalize function. This defaults to _.identity
.
const store = ; store; // on next tick:// Action is: {type: 'GRAPH', data: {fuz: 'bus'}, hi: 'mom'}
If you pass a function to batchComplete
, it will be used to finalize.
const store = ; store; store; // Action is: {type: 'GRAPH', data: {fuz: 'bus'}, hi: 'mom'}
Credits
Redux Batch Middleware is free software under the MIT license. It was created in sunny Santa Monica by Matthew Drake.