Odonno / ReduxSimple

Simple Stupid Redux Store using Reactive Extensions
http://www.nuget.org/packages/ReduxSimple/
MIT License
144 stars 20 forks source link

Lift restrictions on action to reducer mapping #71

Closed ltjax closed 4 years ago

ltjax commented 4 years ago

Right now, the mapping of reducers to actions seems to be 1:1 in ReduxSimple (Because of the FirstOrDefault in ReduxStore.Reduce). Or am I misinterpreting that? In the original redux, it is common to have multiple reducers react to the same action, especially when using slice reducers. See also: https://stackoverflow.com/questions/37111300/redux-why-not-put-actions-and-reducer-in-same-file

Maybe just replace the FirstOrDefault with a Where instead?

Odonno commented 4 years ago

@ltjax Yes, indeed. I used the simple pattern of FirstOrDefault to make it really simple. Still, I had no idea how to apply multiple reducers based on the same action.

I thought to use a Scan operator but I do not know any in .NET. What was your idea?

ltjax commented 4 years ago

Probably combine Where and Aggregate:

return _reducers
  .Where(r => r.Types.Contains(actionName))
  .Where(r => r.Reduce != null)
  .Aggregate(state, (state, reducer) => reducer.Reduce(state, action))
Odonno commented 4 years ago

That could work. We must also add some unit tests to verify that.

Odonno commented 4 years ago

Done. You should see a 3.1 version very soon.