tshelburne / redux-batched-actions

redux higher order reducer + action to reduce actions under a single subscriber notification
MIT License
1.04k stars 37 forks source link

Unreadable action history #13

Closed acontreras89 closed 7 years ago

acontreras89 commented 7 years ago

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.

tshelburne commented 7 years ago

Cool idea - what specifically would you suggest?

acontreras89 commented 7 years ago

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:

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}
}
acontreras89 commented 7 years ago

@tshelburne any thoughts?

tshelburne commented 7 years ago

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?

acontreras89 commented 7 years ago

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 :)