dphilipson / typescript-fsa-reducers

Fluent syntax for defining typesafe reducers on top of typescript-fsa.
MIT License
220 stars 16 forks source link

Multiple handlers for a single action type #17

Closed zernie closed 7 years ago

zernie commented 7 years ago

Currently there is no way to specify a reducer for a single action type but with multiple handlers. It would look like this:

.caseWithHandlers(action, [handlerA, handlerB])
dphilipson commented 7 years ago

Hi Zernie,

To make sure I understand what you mean, what is the effect of doing this? Do you want it to apply the two handlers in order, i.e.

const reducer = reducerWithInitialState(INITIAL_STATE)
    .caseWithHandlers(actionCreator, [handlerA, handlerB])

is equivalent to

function reducer(state = INITIAL_STATE, action) {
     if (isType(action, actionCreator) {
          return handlerB(handlerA(state, action.payload), action.payload);
    }
}

Can you describe a situation where this would be useful, i.e. why would defining a handler in two parts increase clarity?

Supposing there's a good use case for this, you can already achieve this functionality by using reduce-reducers, i.e.

.case(action, reduceReducers(handlerA, handlerB))

Because this is already possible through composition, I'm inclined to say it is unnecessary to include this functionality into the API of this library. Let me know if you disagree.

dphilipson commented 7 years ago

Closing for inactivity. Feel free to re-open if you have more comments.