-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Store: improve action creators #1634
Comments
Further discussion lead to the advantage of the single The action creator could be further simplified to export const myAction = createAction('type1', props<{x: number}>()); and the usage would be // component.ts
this.store.dispatch(myAction({x: 5})); // uses function symbol
// effect.ts
ofType(myAction.type) // uses the same symbol with static type prop
map(() => myOtherAction()) // same symbol, without `new`
// reducer.ts
case myAction.type: // uses function symbol with static type prop The implementation would be heavily inspired ({read: copied with minor adjustments}) from the |
👍👍 I would also like the following to be possible - https://github.com/cartant/ts-action#creator const foo = action("FOO", (name: string) => ({ name }));
const fooAction = foo("alice");
console.log(fooAction); // { type: "FOO", name: "alice" } I think it would also be possible to allow an ofType(myAction) |
Yes, I plan to leave the options of
|
Have ended up with an example app which shows ngrx using classes, action creators & ts-action. |
We also discussed to drop Why? Payload was serving as a container object to be passed constructor, so that property names had to be explicitly specified, e.g. // Unclear which props these arguments would be assigned to.
new MyAction('blah', 'blah blah', foo, bar);
// much better and explicit
new MyAction({
prop1: 'blah',
prop2: 'blah blah',
prop3: foo,
bar,
}); However, accessing properties through payload is a bit cumbersome.
Note, that if one still wishes to continue using I'll send a PR for this proposal soon. |
What about adding factory for async flow that will create success, falied (and pending) actions? I think it would reduce the boilerplate of create async flow, and will help to create standard conventions for async actions. Suggestions of how the api will be: const loadFeature =
createAsyncAction<REQ, // the request payload type
RES, // the response success payload type
ERR // the response failure payload type
>('[Feature] Load Feature');
console.log(loadFeature);
// {load: Action<REQ>, success: Action<RES>, fail: Action<ERR>, pending: Action} Hope you like the idea, will be happy to help with PR. 😃 |
@itayod that would violate Good Action Hygiene™️ - actions types should include the source in their type. |
@alex-okrushko so just to make things clear, what you mean is that it will violate the Good Action Hygiene™️ because the action types for If so, I got your point, It could also effect the However, I think the value here in terms of naming standards and boilerplate reduce are two strong advantages here. |
Closed via #1654 |
Problem
There has been a number of takes on this already (#584, current function-base action creator #1480).
MY_ACTION_TYPE
(instead of type enum) to accompany the Class itself is also something that is frequently done.However, recently new linter rule was added that requires any exported symbols to have the comment. This ruled turned any Action into the following:
Describe any alternatives/workarounds you're currently using
None, however there were proposals to duplicate
readonly type
withstatic type
on the same class.Proposal
To address both of the issues I suggest to create function returning Class that would "enhance" the action creator.
typedAction
here would create a class and add bothreadonly type
andstatic type
to the class.Suggestions about the naming of that function are very welcomed. I heard
withAction
, however not sure if that provides enough context about what it does.New usage would be
Implementation
Implementation requires 3 parts:
PLAYGROUND LINK
Limitation
Ideally I'd want
readonly type
to beabstract
, and TS is supporting itHowever TS does not seem to like when I use this anonymous Class as a return type of and requires that Actions themselves reimplement it.
PLAYGROUND LINK
Alternative implementation
Alternative would be to help TS with that limitation and extract
readonly type
into another interface:PLAYGROUND LINK
In this cases static class doesn't have to be exported, so there would still be three exported symbols.
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: