1- # When should I unsubscribe from RxJS observables in Angular?
2- There's a lot of confusion in the community about the question if and when you have to unsubscribe
3- from RxJS observables in Angular.
1+ # When should I unsubscribe from RxJS observables in Angular components?
2+ ## Intro
3+ There's a lot of confusion in the community about the question if and when you have to unsubscribe manually
4+ from RxJS observables in Angular components.
45
56Although there are some quite nice StackOverflow posts about this question, there is no clear guide
67that contains all relevant information explained by examples.
78
8- This repository provides a guide for common situations and an Angular project to examine the problems by yourself.
9+ ### Angular components and observables
10+ Our guide focuses on issues with components using observables.
911
10- ## Angular components and observables
11- The guide focuses on issues with components using observables.
12-
13- A typical component lifecycle in an Angular CRUD application contains the following steps (in a simplified version):
12+ A typical component lifecycle in an Angular CRUD application contains the following steps (simplified version):
14131 . User navigates to component `` A ``
15142 . Component `` A `` gets initialized (constructor and `` ngOnInit `` are invoked)
16153 . Some logic is called to load data (e.g. using `` HttpClient `` ).
@@ -19,18 +18,37 @@ A typical component lifecycle in an Angular CRUD application contains the follow
19185 . Component `` A `` gets destroyed (`` ngOnDestroy `` is invoked)
20196 . ...
2120
21+ This repository provides a guide explaining when you have to unsubscribe manually from subscriptions
22+ (when component gets destroyed) made in step 3 together with the examples.
23+
24+ ## A guide for common situations
25+ Whether you have to unsubscribe or not depends on the callback logic you are using in the observables subscription.
26+
27+ ### Side effects
28+ If the callback code from the subscription executes code with side effects (e.g. affecting global application state)
29+ you should always consider to manually unsubscribe when the component gets destroyed.
30+ Otherwise the current and correct application state can be falsely overwritten by callback execution from a destroyed component.
31+
32+ ### Observables that don't complete
33+ If the subscription callback from an observables that does not eventually complete uses member variables from the component,
34+ the destroyed component cannot be garbage collected thus resulting in a memory leak.
35+ Therefore you should always unsubscribe in that case.
36+
37+ ### Observables that eventually complete
38+ Observables that don't eventually complete (for example an observable emitting a value each second) should be cancelled always,
39+ since the callback logic from the destroyed component still runs (infinitely) in the background otherwise.
2240
23- ## Conclusion
24- Whether you have to unsubscribe or not heavily depends on the callback logic you are using in the observables subscription .
41+ ### Angular HttpClient
42+ The Angular HttpClient creates an observable that eventually completes. Therefore the same applies .
2543
26- If the callback executes code with side effects (affecting global application state) you should always unsubscribe
27- when the component gets destroyed .
44+ ### Angular ActivatedRoute
45+ For observables from the `` ActivatedRoute `` you do not have to unsubscribe manually .
2846
29- If the callback uses member variables from the component class, there can be a memory leak when using observables
30- that don't complete, therefore you should always unsubscribe in that case .
47+ ### Angular Router events
48+ For Angular Router events ( `` NavigationStart `` , `` NavigationEnd `` , ...) you have to manually unsubscribe .
3149
32- Observables that don't complete should be cancelled always,
33- since the callback logic still runs (infinitely) in the background otherwise .
50+ ### Overview
51+ When you should unsubscribe when the component gets destroyed .
3452
3553| | Side effects | Memory leaks | Should unsubscribe |
3654| ----------------------------------------| -----------------| --------------| --------------------|
@@ -40,8 +58,8 @@ since the callback logic still runs (infinitely) in the background otherwise.
4058| _ Angular ActivatedRoute_ | No | No | No (4) |
4159| _ Angular Router events_ | Possible (1) | Possible (2) | Yes |
4260
43- (1): If you execute methods with side effects in the callback.
44- (2): If you use member variables from the component in the callback.
61+ (1): If you execute methods with side effects in the subscription callback.
62+ (2): If you use member variables from the component in the subscription callback.
4563(3): Assuming the observable completes.
4664(4): You don't have to, but are free to unsubscribe anyway.
4765
0 commit comments