-
Notifications
You must be signed in to change notification settings - Fork 25.9k
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
fix: don't instantiate providers with ngOnDestroy eagerly. #15070
Conversation
Wouldn't this be considered a breaking change? |
14d0f86
to
13982cb
Compare
Marked in the commit message as breaking change with small impact. |
ed796b7
to
9403853
Compare
BREAKING CHANGE: Perviously, any provider that had an ngOnDestroy lifecycle hook would be created eagerly. Now, only classes that are annotated with @component, @directive, @pipe, @NgModule are eager. Providers only become eager if they are either directly or transitively injected into one of the above. This also makes all `useValue` providers eager, which should have no observable impact other than code size. EXPECTED IMPACT: Making providers eager was an incorrect behavior and never documented. Also, providers that are used by a directive / pipe / ngModule stay eager. So the impact should be rather small. Fixes angular#14552
…5070) BREAKING CHANGE: Perviously, any provider that had an ngOnDestroy lifecycle hook would be created eagerly. Now, only classes that are annotated with @component, @directive, @pipe, @NgModule are eager. Providers only become eager if they are either directly or transitively injected into one of the above. This also makes all `useValue` providers eager, which should have no observable impact other than code size. EXPECTED IMPACT: Making providers eager was an incorrect behavior and never documented. Also, providers that are used by a directive / pipe / ngModule stay eager. So the impact should be rather small. Fixes angular#14552
This was a breaking change for us. Is there a way to make an @Injectable() instantiate eagerly? We have @Injectable() services that listens for events then performs some logic whenever such events are fired, hence they never had to be injected anywhere. Now it looks like to instantiate these services we would have to inject them through the constructor even though we never call these services directly. Which can be annoying since we now have constructors with 20+ services that are never actually referenced. This is an example of such a service:
Could we make @Injectable() take in an option to instantiate eagerly? or perhaps something like this:
I'd be happy to submit a PR if I can get some directions on where to look. |
If you have an NgModule, then that module could inject them.
To prevent having to list all of them, change these providers into multi
providers (set "multi: true") for the same token, and then inject just this
token into your NgModule constructor.
You can also make each of your providers also provide an APP_ INITIALIZER,
which already is a multi provider following the above pattern...
Please let me know if this helps...
Regarding eager: true flag: we had some bad experience with this in
angularJS and would need to investigate it more thoroughly, if it actually
helps, or rather makes complex apps harder to maintain...
…On Fri, Mar 24, 2017, 6:04 PM Casper Chia ***@***.***> wrote:
This was a breaking change for us. Is there a way to make an @Injectable
<https://github.com/Injectable>() instantiate eagerly? We have @Injectable
<https://github.com/Injectable>() services that listens for events then
performs some logic whenever such events are fired, hence they never had to
be injected anywhere. Now it looks like to instantiate these services we
would have to inject them through the constructor even though we never call
these services directly. Which can be annoying since we now have
constructors with 20+ services that are never actually referenced.
This is an example of such a service:
`
@Injectable <https://github.com/Injectable>()
export class SubmitEventListener {
constructor(private eventService: EventService) {
this.eventService.events.subscribe(event => doSomething());
}
doSomething() {
console.log("hello world");
}
}
`
Could we make @Injectable <https://github.com/Injectable>() take in an
option to instantiate eagerly?
@Injectable({eager: true})
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#15070 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAqKfw8EOEebAY-SXLSX_I8Tv5YgOynwks5rpGgvgaJpZM4MZxoQ>
.
|
Thanks for the quick response! Using multi providers as suggested worked just fine. Not too sure about using APP_INITIALIZER though, the official docs doesn't say much about it at the moment. For reference, here is what I did (following the example above):
Thanks @tbosch ! |
@casperchia , there is an example here. |
@casperchia there's an issue for discussion #13960 |
…5070) BREAKING CHANGE: Perviously, any provider that had an ngOnDestroy lifecycle hook would be created eagerly. Now, only classes that are annotated with @component, @directive, @pipe, @NgModule are eager. Providers only become eager if they are either directly or transitively injected into one of the above. This also makes all `useValue` providers eager, which should have no observable impact other than code size. EXPECTED IMPACT: Making providers eager was an incorrect behavior and never documented. Also, providers that are used by a directive / pipe / ngModule stay eager. So the impact should be rather small. Fixes angular#14552
…5070) BREAKING CHANGE: Perviously, any provider that had an ngOnDestroy lifecycle hook would be created eagerly. Now, only classes that are annotated with @component, @directive, @pipe, @NgModule are eager. Providers only become eager if they are either directly or transitively injected into one of the above. This also makes all `useValue` providers eager, which should have no observable impact other than code size. EXPECTED IMPACT: Making providers eager was an incorrect behavior and never documented. Also, providers that are used by a directive / pipe / ngModule stay eager. So the impact should be rather small. Fixes angular#14552
…5070) BREAKING CHANGE: Perviously, any provider that had an ngOnDestroy lifecycle hook would be created eagerly. Now, only classes that are annotated with @component, @directive, @pipe, @NgModule are eager. Providers only become eager if they are either directly or transitively injected into one of the above. This also makes all `useValue` providers eager, which should have no observable impact other than code size. EXPECTED IMPACT: Making providers eager was an incorrect behavior and never documented. Also, providers that are used by a directive / pipe / ngModule stay eager. So the impact should be rather small. Fixes angular#14552
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Perviously, any provider that had an ngOnDestroy lifecycle hook would be created eagerly. Now, only classes that are annotated with @component, @directive, @pipe, @NgModule are eager. Providers only become eager if they are either directly or transitively injected into one of the above.
Fixes #14552