-
-
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
feat(effects): allow ofType to handle ActionCreator #1676
Conversation
Preview docs changes for 8549f84 at https://previews.ngrx.io/pr1676-8549f84/ |
modules/effects/src/actions.ts
Outdated
allowedTypes.some(type => type === action.type) | ||
); | ||
return filter((action: Action) => { | ||
for (let typeOrActionCreator of allowedTypes) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about checking on action first and next on the action creator type?
return allowedTypes.some(type => {
// Comparing the string to type
if (typeof type === 'string') {
return type === action.type;
}
// We are filtering by ActionCreator
return type && type.type === action.type;
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, reads much better. Also I think I reimplemented some
in my previous solution 🤦♂️
Preview docs changes for 531d115 at https://previews.ngrx.io/pr1676-531d115/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 LGTM
Right now this offers type safety for 5 action types.
This was due to the fact that we had to provide the typings.
If we want, we could add new typings as in the ts-action-operators
library of Nicholas.
See https://github.com/cartant/ts-action-operators#oftype for ref.
ofType({ foo, bar }),
tap(action => {
// Here, the action has been narrowed to `typeof union({ foo, bar })`.
// Common properties will be accessible, other will require further narrowing.
if (isType(action, foo)) {
// Here, the action has been narrowed to a FOO action.
} else if (isType(action, bar)) {
// Here, the action has been narrowed to a BAR action.
}
})
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Closes #
What is the new behavior?
Allows to filter Actions with
ofType(actionCreator)
as well as maintains previous behaviorofType(actionCreator.type)
.Does this PR introduce a breaking change?
Other information