Closed acontreras89 closed 7 years ago
Cool idea - what specifically would you suggest?
Something like
export const BATCH = 'BATCHING_REDUCER.BATCH';
export function batchActions(type = BATCH, actions) {
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);
} else {
return reduce(state, action);
}
}
}
Other things me and my colleagues do are:
BATCH
as type
{[object]|...object}
For instance, in order to make the change backwards compatible, you could use
export function batchActions(type = BATCH, actions) {
if (Array.isArray(type)) {
actions = type
type = BATCH
}
return {type, meta: { batch: true }, payload: actions}
}
@tshelburne any thoughts?
Hey, sorry for the delay. I like this a lot - my only comment, I would prefer to have the type
argument be second, both because of BC and because args with defaults just make sense to me at the end. Is there a precedent here you're following?
Nope. It simply made more sense to me to have type
before actions
, regardless of whether it is optional or not. It still does, even though the code is straight uglier this way.
In any case, the action creator's signature it is up to you as the maintainer of the package :)
All batches of actions look the same in the history (
BATCHING_REDUCER.BATCH
). Using a meta property to identify batches would allow for action type customization.I'd be happy to help with the change if this is something you'd like to do.