Skip to content

Commit 1eefc44

Browse files
committed
Add takeUntilDestroyed (https://angular.io/api/core/rxjs-interop/takeUntilDestroyed) and cleanup other unsubscription examples
1 parent 666ee0b commit 1eefc44

File tree

13 files changed

+106
-47
lines changed

13 files changed

+106
-47
lines changed

angular-app/src/app/app-routing.module.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,24 @@ import {
99
PATH_ROUTER_EVENTS,
1010
PATH_ROUTER_PARAM_MAP,
1111
PATH_RXJS_TIMER,
12-
PATH_RXJS_TIMER_COMPLETE
12+
PATH_RXJS_TIMER_COMPLETE,
13+
PATH_UNSUBSCRIPTION_GATHER,
14+
PATH_UNSUBSCRIPTION_TAKE_UNTIL, PATH_UNSUBSCRIPTION_TAKE_UNTIL_DESTROYED,
15+
PATH_UNSUBSCRIPTION_UNTIL_DESTROYED
1316
} from './routing-links';
1417
import {RxjsTimerCompleteComponent} from './rxjs-timer-complete/rxjs-timer-complete.component';
1518
import {HttpclientComponent} from './httpclient/httpclient.component';
1619
import {RouterParamMapComponent} from './router-param-map/router-param-map.component';
1720
import {RouterEventsComponent} from './router-events/router-events.component';
1821
import {ComponentTreeComponent} from './component-tree/component-tree.component';
22+
import {
23+
GatherSubscriptionsComponent
24+
} from "./unsubscription-methods/gather-subscriptions/gather-subscriptions.component";
25+
import {TakeUntilComponent} from "./unsubscription-methods/take-until/take-until.component";
26+
import {UntilDestroyedComponent} from "./unsubscription-methods/until-destroyed/until-destroyed.component";
27+
import {
28+
TakeUntilDestroyedComponent
29+
} from "./unsubscription-methods/take-until-destroyed/take-until-destroyed.component";
1930

2031

2132
const routes: Routes = [
@@ -26,7 +37,11 @@ const routes: Routes = [
2637
{path: PATH_ROUTER_PARAM_MAP, component: RouterParamMapComponent},
2738
{path: PATH_ROUTER_PARAM_MAP + '/:param', component: RouterParamMapComponent},
2839
{path: PATH_ROUTER_EVENTS, component: RouterEventsComponent},
29-
{path: PATH_COMPONENT_TREE, component: ComponentTreeComponent}
40+
{path: PATH_COMPONENT_TREE, component: ComponentTreeComponent},
41+
{path: PATH_UNSUBSCRIPTION_GATHER, component: GatherSubscriptionsComponent},
42+
{path: PATH_UNSUBSCRIPTION_TAKE_UNTIL, component: TakeUntilComponent},
43+
{path: PATH_UNSUBSCRIPTION_UNTIL_DESTROYED, component: UntilDestroyedComponent},
44+
{path: PATH_UNSUBSCRIPTION_TAKE_UNTIL_DESTROYED, component: TakeUntilDestroyedComponent},
3045
];
3146

3247
@NgModule({

angular-app/src/app/app.component.html

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<h1>When to unsubscribe in Angular - Examples</h1>
1+
<h1>When to unsubscribe in Angular</h1>
2+
<h2>Examples</h2>
23
<ul>
34
<li>
45
<a [routerLink]="RoutingLinks.emptyComponent">Empty component</a>
@@ -29,5 +30,20 @@ <h1>When to unsubscribe in Angular - Examples</h1>
2930
<a [routerLink]="RoutingLinks.componentTree">Component tree</a>
3031
</li>
3132
</ul>
33+
<h2>Unsubscription methods</h2>
34+
<ul>
35+
<li>
36+
<a [routerLink]="RoutingLinks.unsubscriptionUntilDestroyed">Unsubscribe with untilDestroyed</a>
37+
</li>
38+
<li>
39+
<a [routerLink]="RoutingLinks.unsubscriptionTakeUntilDestroyed">Unsubscribe with takeUntilDestroyed</a>
40+
</li>
41+
<li>
42+
<a [routerLink]="RoutingLinks.unsubscriptionTakeUntil">Unsubscribe with takeUntil</a>
43+
</li>
44+
<li>
45+
<a [routerLink]="RoutingLinks.unsubscriptionGather">Unsubscribe with subscription gathering</a>
46+
</li>
47+
</ul>
3248
<hr>
3349
<router-outlet></router-outlet>

angular-app/src/app/app.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import { RouterEventsComponent } from './router-events/router-events.component';
1616
import { ComponentTreeComponent } from './component-tree/component-tree.component';
1717
import { SubComponentOneComponent } from './component-tree/sub-component-one/sub-component-one.component';
1818
import { SubComponentTwoComponent } from './component-tree/sub-component-one/sub-component-two/sub-component-two.component';
19+
import {
20+
TakeUntilDestroyedComponent
21+
} from "./unsubscription-methods/take-until-destroyed/take-until-destroyed.component";
1922

2023
@NgModule({
2124
declarations: [
@@ -31,7 +34,8 @@ import { SubComponentTwoComponent } from './component-tree/sub-component-one/sub
3134
RouterEventsComponent,
3235
ComponentTreeComponent,
3336
SubComponentOneComponent,
34-
SubComponentTwoComponent
37+
SubComponentTwoComponent,
38+
TakeUntilDestroyedComponent
3539
],
3640
imports: [
3741
BrowserModule,
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import {Component} from '@angular/core';
22

33
@Component({
44
selector: 'app-component-tree',
55
templateUrl: './component-tree.component.html',
66
styleUrls: ['./component-tree.component.scss']
77
})
8-
export class ComponentTreeComponent implements OnInit {
8+
export class ComponentTreeComponent {
99

10-
constructor() { }
11-
12-
ngOnInit() {
10+
constructor() {
1311
}
14-
1512
}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import {Component} from '@angular/core';
22

33
@Component({
44
selector: 'app-sub-component-one',
55
templateUrl: './sub-component-one.component.html',
66
styleUrls: ['./sub-component-one.component.scss']
77
})
8-
export class SubComponentOneComponent implements OnInit {
8+
export class SubComponentOneComponent {
99

1010
constructor() { }
1111

12-
ngOnInit() {
13-
}
14-
1512
}

angular-app/src/app/router-param-map/router-param-map.component.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, OnDestroy, OnInit} from '@angular/core';
1+
import {Component, OnInit} from '@angular/core';
22
import {ActivatedRoute, ParamMap} from '@angular/router';
33
import {Title} from '@angular/platform-browser';
44

@@ -7,7 +7,7 @@ import {Title} from '@angular/platform-browser';
77
templateUrl: './router-param-map.component.html',
88
styleUrls: ['./router-param-map.component.scss']
99
})
10-
export class RouterParamMapComponent implements OnInit, OnDestroy {
10+
export class RouterParamMapComponent implements OnInit {
1111

1212
paramMap?: ParamMap;
1313
queryParamMap?: ParamMap;
@@ -27,7 +27,4 @@ export class RouterParamMapComponent implements OnInit, OnDestroy {
2727
});
2828
}
2929

30-
ngOnDestroy() {
31-
}
32-
3330
}

angular-app/src/app/routing-links.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ export const PATH_HTTP_CLIENT = 'http-client';
55
export const PATH_ROUTER_PARAM_MAP = 'param-map';
66
export const PATH_ROUTER_EVENTS = 'router-events';
77
export const PATH_COMPONENT_TREE = 'component-tree';
8+
export const PATH_UNSUBSCRIPTION_GATHER = 'unsubscription-gather';
9+
export const PATH_UNSUBSCRIPTION_TAKE_UNTIL = 'unsubscription-take-until';
10+
export const PATH_UNSUBSCRIPTION_UNTIL_DESTROYED = 'unsubscription-until-destroyed';
11+
export const PATH_UNSUBSCRIPTION_TAKE_UNTIL_DESTROYED = 'unsubscription-take-until-destroyed';
812

913
export class RoutingLinks {
1014
static emptyComponent = PATH_EMPTY;
@@ -14,4 +18,8 @@ export class RoutingLinks {
1418
static paramMap = PATH_ROUTER_PARAM_MAP;
1519
static routerEvents = PATH_ROUTER_EVENTS;
1620
static componentTree = PATH_COMPONENT_TREE;
21+
static unsubscriptionGather = PATH_UNSUBSCRIPTION_GATHER;
22+
static unsubscriptionTakeUntil = PATH_UNSUBSCRIPTION_TAKE_UNTIL;
23+
static unsubscriptionUntilDestroyed = PATH_UNSUBSCRIPTION_UNTIL_DESTROYED;
24+
static unsubscriptionTakeUntilDestroyed = PATH_UNSUBSCRIPTION_TAKE_UNTIL_DESTROYED;
1725
}

angular-app/src/app/unsubscription-methods/gather-subscriptions/gather-subscriptions.component.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
import {Component, OnDestroy, OnInit} from '@angular/core';
1+
import {Component, OnDestroy} from '@angular/core';
22
import {Subscription, timer} from 'rxjs';
33

44
@Component({
55
selector: 'app-gather-subscriptions',
66
templateUrl: './gather-subscriptions.component.html',
77
styleUrls: ['./gather-subscriptions.component.scss']
88
})
9-
export class GatherSubscriptionsComponent implements OnInit, OnDestroy {
9+
export class GatherSubscriptionsComponent implements OnDestroy {
1010

1111
private readonly subscription = new Subscription();
1212

13-
private everySecond = timer(0, 1000);
14-
private everyThirdSecond = timer(0, 3000);
13+
private everySecond$ = timer(0, 1000);
14+
private everyThirdSecond$ = timer(0, 3000);
1515

1616
constructor() {
17-
}
18-
19-
ngOnInit() {
20-
this.subscription.add(this.everySecond.subscribe(() => {
17+
this.subscription.add(this.everySecond$.subscribe(() => {
18+
console.log("GatherSubscriptionsComponent everySecond$ emitted");
2119
// some logic here
2220
}));
23-
this.subscription.add(this.everyThirdSecond.subscribe(() => {
21+
this.subscription.add(this.everyThirdSecond$.subscribe(() => {
22+
console.log("GatherSubscriptionsComponent everyThirdSecond$ emitted");
2423
// some logic here
2524
}));
2625
}

angular-app/src/app/unsubscription-methods/take-until-destroyed/take-until-destroyed.component.html

Whitespace-only changes.

angular-app/src/app/unsubscription-methods/take-until-destroyed/take-until-destroyed.component.scss

Whitespace-only changes.

0 commit comments

Comments
 (0)