-
Notifications
You must be signed in to change notification settings - Fork 26.2k
ngOnDestroy not fired when service is provided using useFactory #14818
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
Comments
it's by design. there's no way to know if returned by the factory object has |
As a user |
A few other related examples showing that even https://plnkr.co/edit/eIs7pjvldhcFrMmaJ4IV?p=preview In that version I'm providing an abstract class and using https://plnkr.co/edit/G7vLIvA1nz1dBoJ4sPcW?p=preview Similar to the above, but using an OpaqueToken. As far as I can tell the only time
|
Previously, ngOnDestroy was only called on services which were statically determined to have ngOnDestroy methods. In some cases, such as with services instantiated via factory functions, it's not statically known that the service has an ngOnDestroy method. This commit changes the runtime to look for ngOnDestroy when instantiating all DI tokens, and to call the method if it's present. Fixes angular#22466 Fixes angular#22240 Fixes angular#14818
Previously, ngOnDestroy was only called on services which were statically determined to have ngOnDestroy methods. In some cases, such as with services instantiated via factory functions, it's not statically known that the service has an ngOnDestroy method. This commit changes the runtime to look for ngOnDestroy when instantiating all DI tokens, and to call the method if it's present. Fixes #22466 Fixes #22240 Fixes #14818 PR Close #23755
I have simuler issue with Factory providers from this gide: https://angular.io/guide/dependency-injection#factory-providers Minimal reproduction of the problem with instructions
|
I can't believe it doesn't work with an I need to unsubscribe to some observables in my service - I ended up having to inject a 'SubscriptionManager' service to my component and had the dependent service use that. I suppose in future maybe it's better to have the component call |
@simeyla do you have any gists to demonstrate using a subscription manager? |
@lloydaf I'm not particularly happy with the way mine works - the boiler plate to just use it is equal to the effort to just manually create a Subscription. The goal was to just make an Injectable that a component would provide to itself and you would 'add' subscriptions to it. First problem is that you can't disable inheritance for DI objects - so if you forget to provide it then you'll get the parent manager. So I added a mechanism to make sure it was initialized only once - but then you have to call init() in the constructor which is just ugly. It also put all the subscriptions in a global manager which in the end didn't really bring me the other benefits I was hoping for (such as counting subscriptions and reporting if you weren't closing them properly). So tbh my 'subscription manager' ended up kind of a mess and I'm phasing it out where I did use it. A simple 'Subscription' object on the class or using something like takeUntilDestroy works better. Also there are more issues with it that are even more complicated and 'providing' it to you would be more of a foot gun than anything else. If you're only using ngOnDestroy to unsubscribe to observables you can do one of two things:
Then a factory function to create it:
In every component you must provide it - and be sure to use @self to prevent inheriting a parent subscription (big mess if you don't):
You must also put this in the component (only)
and in services you use this:
The service will then get your component's And none of this is any help if you need to dispose of anything other than RxJS subscriptions! |
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. |
I'm submitting a ... (check one with "x")
Current behavior
When you provide a service using
useFactory
,ngOnDestroy
will never be called.Expected behavior
Regardless of how you provide the service,
ngOnDestroy
should get called if it is a method on the service.Minimal reproduction of the problem with instructions
https://plnkr.co/edit/aUaz6ewyM3m5JorsAoCO?p=preview
useFactory
withuseClass
the problem goes away and you'll see output.What is the motivation / use case for changing the behavior?
There are many cases, main motivator is that this isn't intuitive.
Please tell us about your environment:
Language: TypeScript 2.0
Node (for AoT issues):
node --version
=The text was updated successfully, but these errors were encountered: