New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: "Action class" and "Reducer factory" to define them easier #1345
Comments
@stm32p103 thanks for the proposal! If this pattern works for you that's great, but I think we won't be adding this to the core API. Because the reasons I'm thinking:
|
@timdeschryver thank you for your comment. After read your comment and checked the video "Good Action Hygiene with NgRx Mike Ryan", I revised my proposal more "good action hygiene" friendly, less magical and more typesafe (maybe). If there is any chance, please re-review my revised proposal. Thank you. In revised proposal, Action definition can be written as shown below.
As for reducer definition, it can be written as shown below. And now type inference is available.
Typescript infers type of payload from specified actions (eg: CounterActions.preset -> number). When arguments of reducer are wrong: When actions are incompatible: (However "Action constant" dictionaly populated in "ngrx-utility", it is little magical though.) |
@timdeschryver Sorry, after I posted the comment above, I realized that I misunderstood what "typesafe" means. So I take back the revised proposal. |
👍 Thanks for taking the time to write a comprehensive and clear response. |
Please excuse me if this RFC is duplicate since some people would have proposed something like "automatically define actions" or "easy way to define actions".
I want to propose the way to define actions and reducers easier. Example is here.
Action class to define actions
Instead of writing action name constants, define class with static method decorated with
ToAction
.ToAction
decorator wraps original method and returnsaction
whichtype
is "method.class" andpayload
of its original return value.For example, Counter example can be written as below.
Reducer factory to define reducer
Instanciate
ReducerFactory<T>
and calladd( Class.StaticMethod, reducer )
to add reducers for respective actions, thencreate()
to create reducer.Note that there is no need to write action name constants. And auto-complete will help you find actions you need.
Dispatch actions
Define effects
Instead of
ofType
operator, usepayloadOf
operator and get payload directly.If accepted, I would be willing to submit a PR for this feature
[ x ] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No
The text was updated successfully, but these errors were encountered: