-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
destructor option for service providers #11228
Comments
Services already have |
I have never seen it mentioned that |
I guess the docs on the interface should be updated then |
@DzmitryShylovich, there is no longer logging in the console in the plunker you referenced when a component is destroyed. Has anything changed? |
@MaximusK there was a change in 4.0.0-rc.4 that made providers instantiate lazily. Updated plunker: http://plnkr.co/edit/FA9bIc3dEiHundiKx95M?p=preview |
@willshowell , thanks! So as I understand the |
@MaximusK From what I understand, yes to both questions. For our case, we wished for a service to be destroyed once navigating away from any of the module's routes. This required providing (and injecting) the service in a wrapper |
@willshowell @MaximusK I have the same observations. Service gets its ngOnDestroy() called only when provided in a component. So the Service will never get destroyed if provided in module. And I ask - does that matter - if you "destroy" the ng app - that means you have closed that page so effectively the garbage collector will clean up all eventually. |
@willshowell , thanks. Interesting approach with a @gparlakov , thanks for the confirmation |
@MaximusK it works like this: @Component({
...
providers: [MyService],
template: '<router-outlet></router-outlet>'
})
export class MyNavWrapperComponent {
// Must inject to instantiate service for all child routes.
// When this component is destroyed, the service will be too.
constructor(service: MyService) { }
} routes: Routes = [
{
path: '',
component: MyNavWrapperComponent,
children: [ ... ],
}
] It's important to note that using empty routes like this may introduce confusing behavior with relative routes since that empty route is still considered a UrlSegment. See #17957. |
@willshowell , thanks for sharing |
@gparlakov - I don't think we should assume that destroying an app only happens when you close the window or navigate away. Let's say your app is loaded into a portal with potentially other apps. IMHO - removing the dom node that the app is bootstrapped on should destroy everything including services provided via modules. |
Is there any manual way to destroy the services provided in a module? |
What's the use case?
1. Suppose we did destroy it (kill off its reference for example). Is it
expected to get renew-ed upon next it is needed?
2. Or maybe the service itself can be a creator of services (I'd say
factory but with the existence of *provide... useFactory *it would be
confusing and misleading).
3. Some other use case?
…On 14 April 2018 at 15:53, aks1994 ***@***.***> wrote:
Is there any manual way to destroy the services provided in a module?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#11228 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ADUiVyVnnNioZAnlwIk5LCUJNnj3nYe4ks5tofEygaJpZM4Jycxn>
.
--
Георги Парлъков / Georgi Parlakov
|
@gparlakov My use case is that I have a service that I want to be associated with a component (or only inside a lazy module that opens post logging into my app). However, if I provide it in the component (the main wrapper component post login), I cannot access it in route resolver guards. So I have to provide it at the lazy module level but then it becomes a global service to some extent. Even upon logout from my app (routing to a component outside the lazy module), the service is not destroyed so I have user data in that service which is still in memory. Sure, I can't access it directly from outside the lazy module but it is still not deleted permanently and is reaccessed upon logging in (even with a different log in) |
@aks1994 This is my understanding of what happens:
Right? Provided that you already have guards: Maybe inside of a route guard that is executed on the way in you can clean up/initialize the service i.e. every time user wants to go back to this component the service-s state gets zeroed out. |
Okay, yes I don’t think there is a way either but just wanted to check.
For the clean up/initialise step that you mention, is there a way to do that besides keeping track of all the variables and setting them to null?
Do you mind elaborating a little bit more on how the service could be a provider of services and how that would help here? I’m not too familiar with this.
Thanks for your help!
… On 15-Apr-2018, at 2:48 AM, Georgi Parlakov ***@***.***> wrote:
@aks1994 <https://github.com/aks1994> This is my understanding of what happens:
Whenever the lazy module is first loaded the service gets instantiated and placed in the module's injector
Every subsequent request to that module (to its main component) would use that same instance
Right?
(I am not aware of a way of instructing angular to kill off any module or its providers.)
Provided that you already have guards: Maybe inside of a route guard that is executed on the way in you can clean up/initialize the service i.e. every time user wants to go back to this component the service-s state gets zeroed out.
Alternatively the service could become a provider of a service-s instead of doing the work itself.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub <#11228 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AcygdMZwhBD-MQ2w5PDGRyf82pPeM_zgks5tou1VgaJpZM4Jycxn>.
|
For the clean up - I would do it just like you said - set everything back to initial value. And do it /initialize it/ every time you want to use it. The risk is that there might be a confusion about whether or not the state was initialized - so I'd do it every time. Elaborating on the provider of services: here. Notice the 'the-service.service.ts' and how it is provided in the module and used in |
Okay, makes sense. Thanks for the help and sharing the info on provider of services! |
This seems like a bit of a hack, imo. It would be nice to simply specify my provider in the module and allow the module to destroy it when the user exits the module. The current design can be left in place for backwards compatibility, but why not allow me to specify that the provider gets destroyed every time the use exits the module? |
@jscharett ... what is the term |
@mlc-mlapis When my route changes to leave a lazy loaded module. I understand that lazy loading only delays the loading of the module and does not "unload" it when navigating away from it. It just seems like there should be a clean way to "reset/destroy" a provider used in a lazy loaded module. For example, in my app, we have a lazy loaded module that when the user enters, create a provider for storing various data. The provider is used across multiple components, so its created in the ng module. When the user navigates away from the module back to the home page, I am required to implement a guard so that I can "cleanup" the data stored in the provider. Not a huge issue, but just seems like it would be beneficial if you could specify a provider that could be destroyed upon exiting the module/route. It was not intuitive to me that my provider would continue after navigating away from the module. |
Hi ! |
@Sahil624 any solution you found out for your use case? |
@Sahil624 @shikharseth One approach would be providing the provider in the component that needs it. Then It will get instantiated and destroyed along with the component. I am assuming this component would get destroyed when user logs out - because app navigates to logout screen for example |
@gparlakov the use case is quite different here. Since, I require service data to be shared across components so I need to mention my providers inside my app.module.ts. Your's approach wont let me do the same. |
@shikharseth It appears that you need to use the onDestroy lifecycle hook to clean up your service. |
Hi ! |
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. |
Current behavior
See example below:
The problem I'm having in similar code is that the moment this component is destroyed, there is no way for me to unsubscribe ServiceProvidedInElement from GlobalServiceProvidedInBootstrap unless I use ngOnDestroy in the component.
Expected/desired behavior
The expected behavior is that when I declare my providers, I should be able to set a callback to destroy my service provided. So instead of using ngOnDestroy I could do it like this:
The text was updated successfully, but these errors were encountered: