Is it bad practice to do calculations and data tuning in the saga in return?

I have a saga that runs every 10 seconds in a POLL action to update the current state of my GUI.

when a POLL happens, I need to make a few calls to go through the rest of the interface to find the components I need. There will be 1-5 components in total, for each of them I need to make a separate rest call for the Foo and Bar components.

Then at some point I need to do some sums by concatenating the Foo and Bar data together so that the structure my table expects to list the components will calculate some totals across all the components in my toolbox, etc. None of the work is processor intensive, but it does add a decent bit of code as I have so many things to change.

I'm currently doing all of this in the saga, but I'm not sure if this counts as bad practice? I feel like reducers are a common "place" to tweak data, but it seems odd to throw an action with such a large payload, all responses from every call in the saga, since most of the rest of the response is data, Don't worry. I also like to do all the processing in the saga, so I can decide at the end of everything to pass the error to show the error to the user, or to pass a success action that removes any previous errors, some of the solutions for me wanting to clear the action requires more data processing.

My only concern is that the generator is getting pretty big, with a lot of helper methods that feel a bit out of place in the saga class to handle (they need to be moved into the utils class no matter what I think). The processing is not too expensive and I use generators, so I don't think the processing will have a noticeable impact on the saga threads. However, if there is a recommended best practice, I want to stick with it. Am I breaking standard practice by doing all of my data customization in my saga and sending a reducer for each formatted object to store in a state without any other processing?

+3


source to share


1 answer


This is really a specific case with a common question that is addressed by the Redux FAQ: "Where should my business logic go?" Quoting this answer:

Now the problem is what to add the action creator and what in the reducer is the choice between bold and thin action objects. If you put all the logic in the action creator, you end up with live action objects that basically declare state updates. Reducers get clean, dumb, add, remove this, update these features. They will be easy to compose. But not much of your business logic will be there. If you add more logic to the reducer, you end up with beautiful, subtle action objects, most of your data logic in one place, but your reducers are more difficult to compose as you may need information from other branches. You end up with large reducers or reducers that take additional arguments from the state above.



There is nothing wrong with the logic on the creation side of the action (whether in components, thunks, sagas, or middleware) does a great job of preparing and formatting the data, and the reducer just keeps what was included in the action. On the other hand, having more logic on the reducer side can mean that travel debugging will run more of your actual code again, giving you a better chance of editing and re-behaving.

Overall, it looks like you are doing perfectly intelligently.

+2


source







All Articles