You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|_Observables that don't complete_| Possible (1) | Possible (2) | Yes |
24
+
|_Observables that eventually complete_| Possible (1) | No (3) | Depends (1) |
25
+
|_Angular HttpClient_| Possible (1) | No (3) | Depends (1) |
26
+
|_Angular ActivatedRoute_| No | No | No (4) |
27
+
|_Angular Router events_| Possible (1) | Possible (2) | Yes |
28
+
29
+
(1): If you execute methods with side effects in the callback.
30
+
31
+
(2): If you use member variables from the component in the callback.
32
+
33
+
(3): Assuming the observable completes.
34
+
35
+
(4): You don't have to, but are free to unsubscribe anyway.
36
+
37
+
# Further explanation and examples
18
38
## How to run the examples by yourself
19
39
The project was set up using the latest [Angular CLI](https://github.com/angular/angular-cli) (version 8.3.6). Therefore you can just clone the repository and run
20
40
``
@@ -260,7 +280,7 @@ As expected, only the component with the timer does not get garbage collected.
260
280
This is good news, as this means the third party components (as used as in this example)
261
281
cannot affect memory leaks on your components.
262
282
263
-
##Recommended ways to unsubscribe
283
+
# Recommended ways to unsubscribe
264
284
The obvious way of unsubscribing is how it is done in our examples: Assign the subscription to a class
265
285
property and unsubscribe in the ``ngOnDestroy()`` method.
266
286
@@ -292,7 +312,7 @@ ngOnDestroy() {
292
312
```
293
313
However, same drawbacks here: Cumbersome and obfuscating.
294
314
295
-
###Unsubscribe with ``takeUntil``
315
+
## Unsubscribe with ``takeUntil``
296
316
A cleaner way of unsubscribing is using ``takeUntil``.
297
317
298
318
Official docs for ``takeUntil``: ``takeUntil(notifier: Observable<any>)`` — Emits the values emitted by the source Observable until a notifier Observable emits a value.
@@ -331,7 +351,7 @@ Drawbacks: As with the other methods, it's still quite verbose and error-prone.
331
351
332
352
NOTE: takeUntil operator should always come last (https://blog.angularindepth.com/rxjs-avoiding-takeuntil-leaks-fb5182d047ef)
333
353
334
-
###Using ``untilDestroyed``
354
+
## Using ``untilDestroyed``
335
355
There is an npm package called ``@ngneat/until-destroy`` (https://github.com/ngneat/until-destroy).
336
356
337
357
You can install it (for Angular versions using Ivy) with
@@ -372,29 +392,9 @@ ngOnDestroy() {
372
392
Only drawback: We have to implement ``ngOnDestroy`` everywhere we want to use ``untilDestroyed``.
373
393
However it should be easy to write a linting rule to enforce this.
374
394
375
-
## Conclusion
376
-
Whether you have to unsubscribe or not heavily depends on the callback logic you are using.
377
-
378
-
If the callback executes code with side effects you should always unsubscribe.
379
-
380
-
If the callback uses member variables from the component class there can be a memory leak when using observables that don't complete,
381
-
therefore you should unsubscribe in that case.
382
-
383
-
Observables that don't complete should be cancelled (almost) always,
384
-
since the callback logic still runs (infinitely) in the background otherwise.
385
-
386
-
|| Side effects | Memory leaks | Should unsubscribe |
0 commit comments