When should I store the Subscription
instances and invoke unsubscribe()
during the NgOnDestroy life cycle and when can I simply ignore them?
Some of the best practices regarding observables unsubscriptions inside Angular components:
A quote from Routing & Navigation
When subscribing to an observable in a component, you almost always arrange to unsubscribe when the component is destroyed.
There are a few exceptional observables where this is not necessary. The ActivatedRoute observables are among the exceptions.
The ActivatedRoute and its observables are insulated from the Router itself. The Router destroys a routed component when it is no longer needed and the injected ActivatedRoute dies with it.
Feel free to unsubscribe anyway. It is harmless and never a bad practice.
And in responding to the following links:
I collected some of the best practices regarding observables unsubscriptions inside Angular components to share with you:
http
observable unsubscription is conditional and we should consider the effects of the 'subscribe callback' being run after the component is destroyed on a case by case basis. We know that angular unsubscribes and cleans the http
observable itself (1), (2). While this is true from the perspective of resources it only tells half the story. Let's say we're talking about directly calling http
from within a component, and the http
response took longer than needed so the user closed the component. The subscribe()
handler will still be called even if the component is closed and destroyed. This can have unwanted side effects and in the worse scenarios leave the application state broken. It can also cause exceptions if the code in the callback tries to call something that has just been disposed of. However at the same time occasionally they are desired. Like, let's say you're creating an email client and you trigger a sound when the email is done sending - well you'd still want that to occur even if the component is closed (8).AsyncPipe
as much as possible because it automatically unsubscribes from the observable on component destruction.ActivatedRoute
observables like route.params
if they are subscribed inside a nested (Added inside tpl with the component selector) or dynamic component as they may be subscribed many times as long as the parent/host component exists. No need to unsubscribe from them in other scenarios as mentioned in the quote above from Routing & Navigation docs.takeUntil
(3) or you can use this npm
package mentioned at (4) The easiest way to unsubscribe from Observables in Angular.FormGroup
observables like form.valueChanges
and form.statusChanges
Renderer2
service like renderer2.listen
HostListener
as angular cares well about removing the event listeners if needed and prevents any potential memory leak due to event bindings.A nice final tip: If you don't know if an observable is being automatically unsubscribed/completed or not, add a complete
callback to subscribe(...)
and check if it gets called when the component is destroyed.