/** * batchActions should be applied before calling dispatch: * * dispatch(batchActions([action1, action2])) */ export function batchActions(actions: any[], type: string = "BATCH_ACTION") { return { type, payload: actions, meta: { batch: true }, }; } /** * enableBatching should be applied to the top-level reducer when creating the store: * * const store = createStore(enableBatching(reducer)); */ export function enableBatching(reducer) { return (state, action) => { if (action && action.meta && action.meta.batch) { // `reduce` here refers to the array reduce method. The end result of this line will be the // state after all actions in action.payload have been processed return action.payload.reduce(reducer, state); } return reducer(state, action); }; }