Skip to content
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): add OnInitEffects interface to dispatch an action on initialization #1467

Merged
merged 2 commits into from
Dec 20, 2018

Conversation

timdeschryver
Copy link
Member

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

As mentioned in the issue, the solution we have now to dispatch an INIT effect action doesn't work in prod builds because the constructor name is mangled.

Closes #1447

What is the new behavior?

This PR adds a new lifecycle hook OnInitEffects that dispatches a user-defined action on effect registration. I couldn't find another way to access the effect's name.

Personally, I also find this solution to be cleaner and more flexible than the previous one.

Right now I've made it so that only one action can be dispatched, but this can also be an array of actions if it's needed. Imho one action is sufficient and also follows our best practices to treat an action as a unique event.

Does this PR introduce a breaking change?

[ ] Yes
[ ] No

Other information

The first commit reverts #1305
The second commit adds the OnInitEffects hook. In this commit, I also changed the OnIdentifyEffects signature to be consistent with the other signatures.

During a prod build the contructor name of an effect gets magnled,
therefore this implementation can't be used.
* ```
*/
export interface OnIdentifyEffects {
/**
* @description
* String identifier to differentiate effect instances.
*/
ngrxOnIdentifyEffects: () => string;
ngrxOnIdentifyEffects(): string;
Copy link
Member Author

@timdeschryver timdeschryver Dec 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this signature to a "normal" function instead of an arrow function.
If we would use an arrow function, the function isn't being picked up at

.

@timdeschryver
Copy link
Member Author

Fyi, it seems like the failing test command is "because" of yarn - yarnpkg/yarn#6746 and will be fixed in the next release.

@timdeschryver timdeschryver changed the title Pr/ngrx on init effects feat(Effects): add OnInitEffects interface to dispatch an action on initialization Dec 13, 2018
@brandonroberts
Copy link
Member

@timdeschryver instead of introducing a new hook, how about adding a configuration when registering effects to give the set a name? It could be the same as the class name, but we would use the array of names with the UpdateEffects action.

@timdeschryver
Copy link
Member Author

I think the downsides of using that approach are:

  • With only a name, a user couldn't define a payload, if this is needed
  • It won't be possible to trigger an init while manually adding effects, but is this even needed?

The better part would be, that it would be consistent with the config from ngrx/store.

@brandonroberts
Copy link
Member

Yea, I thought about manually adding effects but I think that's an advanced use case. I'd rather provide the name and use a consistent Update action then have to come up with a different one for each effect.

@brandonroberts
Copy link
Member

Scratch that last comment. I wasn't thinking about how the effects are registered with more than one class. I'm good with the init function.

@brandonroberts brandonroberts merged commit e921cd9 into master Dec 20, 2018
@brandonroberts brandonroberts deleted the pr/ngrxOnInitEffects branch December 20, 2018 12:31
@timdeschryver timdeschryver restored the pr/ngrxOnInitEffects branch March 27, 2019 21:35
@timdeschryver timdeschryver deleted the pr/ngrxOnInitEffects branch March 27, 2019 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UpdateEffects during prod build not behaving as intended
2 participants