Skip to content

Commit fa20977

Browse files
committed
New structure
1 parent 1b5d5e4 commit fa20977

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

README.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,34 @@ that contains all relevant information explained by examples.
77

88
This repository provides the necessary Angular project to examine the problems by yourself and a guide for common situations.
99

10-
### Some resources:
11-
##### Angular/RxJs When should I unsubscribe from Subscription:
12-
https://stackoverflow.com/questions/38008334/angular-rxjs-when-should-i-unsubscribe-from-subscription
10+
## Conclusion
11+
Whether you have to unsubscribe or not heavily depends on the callback logic you are using.
1312

14-
##### Is it necessary to unsubscribe from observables created by Http methods?
15-
https://stackoverflow.com/questions/35042929/is-it-necessary-to-unsubscribe-from-observables-created-by-http-methods
13+
If the callback executes code with side effects you should always unsubscribe.
14+
15+
If the callback uses member variables from the component class there can be a memory leak when using observables that don't complete,
16+
therefore you should unsubscribe in that case.
1617

18+
Observables that don't complete should be cancelled (almost) always,
19+
since the callback logic still runs (infinitely) in the background otherwise.
1720

21+
| | Side effects | Memory leaks | Should unsubscribe |
22+
|----------------------------------------|-----------------|--------------|--------------------|
23+
| _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
1838
## How to run the examples by yourself
1939
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
2040
``
@@ -260,7 +280,7 @@ As expected, only the component with the timer does not get garbage collected.
260280
This is good news, as this means the third party components (as used as in this example)
261281
cannot affect memory leaks on your components.
262282

263-
## Recommended ways to unsubscribe
283+
# Recommended ways to unsubscribe
264284
The obvious way of unsubscribing is how it is done in our examples: Assign the subscription to a class
265285
property and unsubscribe in the ``ngOnDestroy()`` method.
266286

@@ -292,7 +312,7 @@ ngOnDestroy() {
292312
```
293313
However, same drawbacks here: Cumbersome and obfuscating.
294314

295-
### Unsubscribe with ``takeUntil``
315+
## Unsubscribe with ``takeUntil``
296316
A cleaner way of unsubscribing is using ``takeUntil``.
297317

298318
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.
331351

332352
NOTE: takeUntil operator should always come last (https://blog.angularindepth.com/rxjs-avoiding-takeuntil-leaks-fb5182d047ef)
333353

334-
### Using ``untilDestroyed``
354+
## Using ``untilDestroyed``
335355
There is an npm package called ``@ngneat/until-destroy`` (https://github.com/ngneat/until-destroy).
336356

337357
You can install it (for Angular versions using Ivy) with
@@ -372,29 +392,9 @@ ngOnDestroy() {
372392
Only drawback: We have to implement ``ngOnDestroy`` everywhere we want to use ``untilDestroyed``.
373393
However it should be easy to write a linting rule to enforce this.
374394

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 |
387-
|----------------------------------------|-----------------|--------------|--------------------|
388-
| _Observables that don't complete_ | Possible (1) | Possible (2) | Yes |
389-
| _Observables that eventually complete_ | Possible (1) | No (3) | Depends (1) |
390-
| _Angular HttpClient_ | Possible (1) | No (3) | Depends (1) |
391-
| _Angular ActivatedRoute_ | No | No | No (4) |
392-
| _Angular Router events_ | Possible (1) | Possible (2) | Yes |
393-
394-
(1): If you execute methods with side effects in the callback.
395-
396-
(2): If you use member variables from the component in the callback.
397-
398-
(3): Assuming the observable completes.
395+
# Other resources
396+
### Angular/RxJs When should I unsubscribe from Subscription:
397+
https://stackoverflow.com/questions/38008334/angular-rxjs-when-should-i-unsubscribe-from-subscription
399398

400-
(4): You don't have to, but are free to unsubscribe anyway.
399+
### Is it necessary to unsubscribe from observables created by Http methods?
400+
https://stackoverflow.com/questions/35042929/is-it-necessary-to-unsubscribe-from-observables-created-by-http-methods

0 commit comments

Comments
 (0)