-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
35 lines (31 loc) · 870 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export const BATCH = 'BATCHING_REDUCER.BATCH';
export function batchActions(actions, type = BATCH) {
return {type, meta: { batch: true }, payload: actions}
}
export function enableBatching(reduce) {
return function batchingReducer(state, action) {
if (action && action.meta && action.meta.batch) {
return action.payload.reduce(batchingReducer, state);
}
return reduce(state, action);
}
}
export function batchDispatchMiddleware(store) {
function dispatchChildActions(store, action) {
if(action.meta && action.meta.batch) {
action.payload.forEach(function(childAction) {
dispatchChildActions(store, childAction)
})
} else {
store.dispatch(action)
}
}
return function(next) {
return function(action) {
if (action && action.meta && action.meta.batch) {
dispatchChildActions(store, action)
}
return next(action)
}
}
}