I have an Observable in my root (bootstrapped) Angular (6.x) component, AppComponent
.
Normally, I would unsubscribe any open Subscription upon a destroy() call using the lifecycle hook, ngOnDestroy
.
Since the AppComponent is the root of the application and therefore is never destroyed (except when the entire application is destroyed), do I still need to implement ngOnDestroy and do I need to bother with unsubscribing from my Subscriptions?
I haven't been able to find an answer to this, seemingly common, exact scenario.
Example:
export class AppComponent implements OnInit, OnDestroy {
private tokenSubscription: Subscription;
constructor(private dataSvc: DataService) { }
ngOnInit() {
this.tokenSubscription = this.dataSvc.myObservable.subscribe((val) => {
// do stuff
});
}
ngOnDestroy() {
this.tokenSubscription.unsubscribe(); // Do I need this in root component?
}
}
Thanks!
No need to unsubscribe from internal observables of an application scoped service since this service never get's destroyed, unless your entire application get's destroyed, there is no real reason to unsubscribe from it and there is no chance of memory leaks.
Yes you are correct. After a stream is terminated ( onComplete / onError has been called ), subscriber unsubscribes automatically. You should be able to test these behaviors using isUnsubscribed() method on the Subscription object.
As you probably know when you subscribe to an observable or event in JavaScript, you usually need to unsubscribe at a certain point to release memory in the system. Otherwise, you will have a memory leak. A memory leak occurs when a section of memory that is no longer being…
Generally you do not need to unsubscribe from HTTP calls. The ActivatedRoute and its observables are insulated from the Router itself so the Angular Router destroys a routed component when it is no longer needed and the injected ActivatedRoute dies with it.
RXJS subscriptions in the Angular AppComponent
do not need to be unsubscribed, as long as the new Subscriptions are not being repeatedly created. It is fine, for example, if subscriptions are created in ngOnInit
of AppComponent
as long as they are one-time actions.
For Angular services created in the root component, it is ideal to use singleton services to ensure that only one instance exists in the app.
Although it is generally okay to have subscriptions in the root component that are not unsubscribed, it is recommended to following best practices for managing subscriptions.
take(1)
- for Subscriptions that only happen once during application startup, use RXJS take(1)
operator, which has the benefit of automatically unsubscribing.async
Pipe - handles RXJS subscriptions behind the scenes and automatically unsubscribes.take(1)
constructor(private dataSvc: DataService) { }
ngOnInit() {
this.dataSvc.myObservable.pipe(take(1)).subscribe((val) => {
// do stuff
});
}
See The Best Way To Unsubscribe RxJS Observables In The Angular Applications!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With