Skip to content

Commit 0cab4fc

Browse files
drumoniiyggg
authored andcommitted
refactor: unsubscribe on destroy (#2112)
1 parent 4783259 commit 0cab4fc

40 files changed

+402
-218
lines changed

docs/app/@theme/components/fragment-target/fragment-target.directive.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Directive, ElementRef, Inject, Input, OnDestroy, OnInit, PLATFORM_ID, Renderer2 } from '@angular/core';
22
import { ActivatedRoute, Router } from '@angular/router';
3-
import { timer } from 'rxjs';
4-
import { takeWhile, publish, refCount, filter, tap, debounce } from 'rxjs/operators';
3+
import { timer, Subject } from 'rxjs';
4+
import { takeUntil, publish, refCount, filter, tap, debounce } from 'rxjs/operators';
55
import { NB_WINDOW, NbLayoutScrollService } from '@nebular/theme';
66
import { NgdVisibilityService } from '../../../@theme/services';
77

@@ -15,7 +15,7 @@ export class NgdFragmentTargetDirective implements OnInit, OnDestroy {
1515
private readonly marginFromTop = 120;
1616
private isCurrentlyViewed: boolean = false;
1717
private isScrolling: boolean = false;
18-
private alive = true;
18+
private destroy$ = new Subject<void>();
1919

2020
@Input() ngdFragment: string;
2121
@Input() ngdFragmentClass: string;
@@ -37,7 +37,7 @@ export class NgdFragmentTargetDirective implements OnInit, OnDestroy {
3737
.pipe(
3838
publish(null),
3939
refCount(),
40-
takeWhile(() => this.alive),
40+
takeUntil(this.destroy$),
4141
filter(() => this.ngdFragmentSync),
4242
)
4343
.subscribe((fragment: string) => {
@@ -49,7 +49,7 @@ export class NgdFragmentTargetDirective implements OnInit, OnDestroy {
4949
});
5050

5151
this.visibilityService.isTopmostVisible(this.el.nativeElement, OBSERVER_OPTIONS)
52-
.pipe(takeWhile(() => this.alive))
52+
.pipe(takeUntil(this.destroy$))
5353
.subscribe((isTopmost: boolean) => {
5454
this.isCurrentlyViewed = isTopmost;
5555
if (isTopmost) {
@@ -59,9 +59,9 @@ export class NgdFragmentTargetDirective implements OnInit, OnDestroy {
5959

6060
this.scrollService.onScroll()
6161
.pipe(
62-
takeWhile(() => this.alive),
6362
tap(() => this.isScrolling = true),
6463
debounce(() => timer(100)),
64+
takeUntil(this.destroy$),
6565
)
6666
.subscribe(() => this.isScrolling = false);
6767
}
@@ -88,7 +88,8 @@ export class NgdFragmentTargetDirective implements OnInit, OnDestroy {
8888
}
8989

9090
ngOnDestroy() {
91-
this.alive = false;
91+
this.destroy$.next();
92+
this.destroy$.complete();
9293
this.visibilityService.unobserve(this.el.nativeElement, OBSERVER_OPTIONS);
9394
}
9495
}

docs/app/@theme/components/page-tabs/page-tabs.component.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
*/
66

77
import { ChangeDetectionStrategy, Component, Input, OnDestroy, HostBinding } from '@angular/core';
8-
import { takeWhile, map, publishReplay, refCount } from 'rxjs/operators';
8+
import { takeUntil, map, publishReplay, refCount } from 'rxjs/operators';
99
import { ActivatedRoute } from '@angular/router';
10-
import { Observable, of as observableOf, combineLatest } from 'rxjs';
10+
import { Observable, of as observableOf, combineLatest, Subject } from 'rxjs';
1111

1212
@Component({
1313
selector: 'ngd-page-tabs',
@@ -25,6 +25,8 @@ import { Observable, of as observableOf, combineLatest } from 'rxjs';
2525
})
2626
export class NgdPageTabsComponent implements OnDestroy {
2727

28+
private destroy$ = new Subject<void>();
29+
2830
items$: Observable<any[]> = observableOf([]);
2931

3032
@Input()
@@ -36,8 +38,8 @@ export class NgdPageTabsComponent implements OnDestroy {
3638
this.activatedRoute.params.pipe(publishReplay(), refCount()),
3739
)
3840
.pipe(
39-
takeWhile(() => this.alive),
4041
map(([tabs, params]) => (tabs.map((item: any) => ({ ...item, selected: item.tab === params.tab })))),
42+
takeUntil(this.destroy$),
4143
);
4244
}
4345

@@ -76,12 +78,12 @@ export class NgdPageTabsComponent implements OnDestroy {
7678
icon: 'image-outline',
7779
},
7880
];
79-
private alive = true;
8081

8182
constructor(private activatedRoute: ActivatedRoute) {
8283
}
8384

8485
ngOnDestroy() {
85-
this.alive = false;
86+
this.destroy$.next();
87+
this.destroy$.complete();
8688
}
8789
}

docs/app/@theme/components/page-toc/page-toc.component.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
*/
66

77
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnDestroy } from '@angular/core';
8-
import { takeWhile, map } from 'rxjs/operators';
8+
import { takeUntil, map } from 'rxjs/operators';
99
import { ActivatedRoute } from '@angular/router';
10-
import { of as observableOf, combineLatest } from 'rxjs';
10+
import { of as observableOf, combineLatest, Subject } from 'rxjs';
1111

1212
@Component({
1313
selector: 'ngd-page-toc',
@@ -26,6 +26,8 @@ import { of as observableOf, combineLatest } from 'rxjs';
2626
})
2727
export class NgdPageTocComponent implements OnDestroy {
2828

29+
private destroy$ = new Subject<void>();
30+
2931
items: any[];
3032

3133
@Input()
@@ -35,27 +37,26 @@ export class NgdPageTocComponent implements OnDestroy {
3537
this.activatedRoute.fragment,
3638
)
3739
.pipe(
38-
takeWhile(() => this.alive),
3940
map(([toc, fragment]) => {
4041
toc = toc.map((item: any) => ({ ...item, selected: fragment === item.fragment }));
4142
if (toc.length && !toc.find(item => item.selected)) {
4243
toc[0].selected = true;
4344
}
4445
return toc;
4546
}),
47+
takeUntil(this.destroy$),
4648
)
4749
.subscribe((toc) => {
4850
this.items = toc;
4951
this.cd.detectChanges();
5052
})
5153
}
5254

53-
private alive = true;
54-
5555
constructor(private activatedRoute: ActivatedRoute, private cd: ChangeDetectorRef) {
5656
}
5757

5858
ngOnDestroy() {
59-
this.alive = false;
59+
this.destroy$.next();
60+
this.destroy$.complete();
6061
}
6162
}

docs/app/blocks/components/live-example-block/live-example-block.component.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
AfterViewInit,
1414
} from '@angular/core';
1515
import { Location } from '@angular/common';
16-
import { takeWhile } from 'rxjs/operators';
16+
import { takeUntil } from 'rxjs/operators';
17+
import { Subject } from 'rxjs';
1718
import { NgdAnalytics, NgdIframeCommunicatorService } from '../../../@theme/services';
1819
import { NgdExampleView } from '../../enum.example-view';
1920

@@ -51,7 +52,8 @@ export class NgdLiveExampleBlockComponent implements OnInit, AfterViewInit, OnDe
5152
}
5253

5354
iframeHeight = 0;
54-
alive: boolean = true;
55+
56+
private destroy$ = new Subject<void>();
5557

5658
themes: {label: string; value: string}[] = [
5759
{ label: 'Default', value: 'default' },
@@ -79,7 +81,7 @@ export class NgdLiveExampleBlockComponent implements OnInit, AfterViewInit, OnDe
7981

8082
ngOnInit() {
8183
this.communicator.receive(this.content.id)
82-
.pipe(takeWhile(() => this.alive))
84+
.pipe(takeUntil(this.destroy$))
8385
.subscribe(it => {
8486
this.iframeHeight = it.height;
8587
this.loading = false;
@@ -97,7 +99,8 @@ export class NgdLiveExampleBlockComponent implements OnInit, AfterViewInit, OnDe
9799
}
98100

99101
ngOnDestroy() {
100-
this.alive = false;
102+
this.destroy$.next();
103+
this.destroy$.complete();
101104
}
102105

103106
switchTheme(theme: string) {

docs/app/blocks/components/theme-block/theme-block.component.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
import {Component, Input, OnInit, OnDestroy, ChangeDetectionStrategy, ChangeDetectorRef} from '@angular/core';
88
import { FormControl } from '@angular/forms';
9-
import { takeWhile, skip, distinctUntilChanged, debounceTime } from 'rxjs/operators';
10-
9+
import { takeUntil, skip, distinctUntilChanged, debounceTime } from 'rxjs/operators';
10+
import { Subject } from 'rxjs';
1111

1212
@Component({
1313
selector: 'ngd-theme-block',
@@ -18,7 +18,7 @@ import { takeWhile, skip, distinctUntilChanged, debounceTime } from 'rxjs/operat
1818
export class NgdThemeComponent implements OnInit, OnDestroy {
1919
searchControl = new FormControl();
2020

21-
private alive: boolean = true;
21+
private destroy$ = new Subject<void>();
2222

2323
properties = [];
2424
filtered = [];
@@ -47,7 +47,7 @@ export class NgdThemeComponent implements OnInit, OnDestroy {
4747

4848
ngOnInit() {
4949
this.searchControl.valueChanges
50-
.pipe(skip(1), distinctUntilChanged(), debounceTime(300), takeWhile(() => this.alive))
50+
.pipe(skip(1), distinctUntilChanged(), debounceTime(300), takeUntil(this.destroy$))
5151
.subscribe((value: string) => {
5252
this.filtered = this.properties
5353
.filter(({ name }) => name.toLowerCase().includes(value.toLowerCase()));
@@ -60,6 +60,7 @@ export class NgdThemeComponent implements OnInit, OnDestroy {
6060
}
6161

6262
ngOnDestroy() {
63-
this.alive = false;
63+
this.destroy$.next();
64+
this.destroy$.complete();
6465
}
6566
}

docs/app/documentation/documentation.component.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
import { Component, OnDestroy } from '@angular/core';
88
import { Router } from '@angular/router';
9-
import { takeWhile, withLatestFrom, map } from 'rxjs/operators';
9+
import { takeUntil, withLatestFrom, map } from 'rxjs/operators';
10+
import { Subject } from 'rxjs';
1011
import { NbThemeService, NbMenuItem, NbSidebarService, NbMenuService } from '@nebular/theme';
1112

1213
import { NgdMenuService } from '../@theme/services/menu.service';
@@ -24,7 +25,7 @@ export class NgdDocumentationComponent implements OnDestroy {
2425
collapsedBreakpoints = ['xs', 'is', 'sm', 'md', 'lg'];
2526
sidebarTag = 'menuSidebar';
2627

27-
private alive = true;
28+
private destroy$ = new Subject<void>();
2829

2930
constructor(
3031
private service: NgdMenuService,
@@ -42,7 +43,7 @@ export class NgdDocumentationComponent implements OnDestroy {
4243
this.router.events
4344
.pipe(
4445
withLatestFrom(this.themeService.onMediaQueryChange().pipe(map((changes: any[]) => changes[1]))),
45-
takeWhile(() => this.alive),
46+
takeUntil(this.destroy$),
4647
)
4748
.subscribe(([event, mediaQuery]: [any, NbMediaBreakpoint]) => {
4849
if (event.url === '/docs') {
@@ -62,6 +63,7 @@ export class NgdDocumentationComponent implements OnDestroy {
6263
}
6364

6465
ngOnDestroy() {
65-
this.alive = false;
66+
this.destroy$.next();
67+
this.destroy$.complete();
6668
}
6769
}

docs/app/documentation/page/page.component.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import { Component, Inject, NgZone, OnDestroy, OnInit, ViewChild, AfterContentChecked } from '@angular/core';
88
import { Title } from '@angular/platform-browser';
99
import { ActivatedRoute, Router } from '@angular/router';
10-
import { filter, map, publishReplay, refCount, tap, takeWhile } from 'rxjs/operators';
10+
import { filter, map, publishReplay, refCount, tap, takeUntil } from 'rxjs/operators';
11+
import { Subject } from 'rxjs';
1112
import { NB_WINDOW } from '@nebular/theme';
1213
import { NgdTabbedBlockComponent } from '../../blocks/components/tabbed-block/tabbed-block.component';
1314
import { NgdStructureService } from '../../@theme/services';
@@ -20,7 +21,7 @@ import { NgdStructureService } from '../../@theme/services';
2021
export class NgdPageComponent implements OnInit, AfterContentChecked, OnDestroy {
2122

2223
currentItem;
23-
private alive = true;
24+
private destroy$ = new Subject<void>();
2425

2526
currentTabName: string = '';
2627

@@ -52,13 +53,13 @@ export class NgdPageComponent implements OnInit, AfterContentChecked, OnDestroy
5253
}
5354

5455
ngOnDestroy() {
55-
this.alive = false;
56+
this.destroy$.next();
57+
this.destroy$.complete();
5658
}
5759

5860
handlePageNavigation() {
5961
this.activatedRoute.params
6062
.pipe(
61-
takeWhile(() => this.alive),
6263
filter((params: any) => params.subPage),
6364
map((params: any) => {
6465
const slag = `${params.page}_${params.subPage}`;
@@ -75,6 +76,7 @@ export class NgdPageComponent implements OnInit, AfterContentChecked, OnDestroy
7576
}),
7677
publishReplay(),
7778
refCount(),
79+
takeUntil(this.destroy$),
7880
)
7981
.subscribe((item) => {
8082
this.currentItem = item;

docs/app/example/example.component.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AfterViewInit, Component, Inject, OnDestroy, OnInit } from '@angular/core';
22
import { Router } from '@angular/router';
3-
import { of as observableOf } from 'rxjs';
4-
import { takeWhile, delay } from 'rxjs/operators';
3+
import { of as observableOf, Subject } from 'rxjs';
4+
import { takeUntil, delay } from 'rxjs/operators';
55
import { NB_DOCUMENT, NbThemeService } from '@nebular/theme';
66
import { NgdAnalytics, NgdIframeCommunicatorService } from '../@theme/services';
77

@@ -12,7 +12,7 @@ import { NgdAnalytics, NgdIframeCommunicatorService } from '../@theme/services';
1212
})
1313
export class NgdExampleComponent implements OnInit, AfterViewInit, OnDestroy {
1414
private id: string;
15-
private alive: boolean = true;
15+
private destroy$ = new Subject<void>();
1616

1717
constructor(private communicator: NgdIframeCommunicatorService,
1818
private themeService: NbThemeService,
@@ -34,7 +34,8 @@ export class NgdExampleComponent implements OnInit, AfterViewInit, OnDestroy {
3434
}
3535

3636
ngOnDestroy() {
37-
this.alive = false;
37+
this.destroy$.next();
38+
this.destroy$.complete();
3839
}
3940

4041
private setupId() {
@@ -43,7 +44,7 @@ export class NgdExampleComponent implements OnInit, AfterViewInit, OnDestroy {
4344

4445
private subscribeOnThemeSwitch() {
4546
this.communicator.receive(this.id)
46-
.pipe(takeWhile(() => this.alive))
47+
.pipe(takeUntil(this.destroy$))
4748
.subscribe(payload => this.changeTheme(payload))
4849
}
4950

docs/articles/auth/oauth2.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,18 @@ And finally, let's add code to our component to initiate the authentication. Fir
140140
})
141141
export class NbOAuth2LoginComponent implements OnDestroy {
142142

143-
alive = true;
143+
private destroy$ = new Subject<void>();
144144

145145
login() {
146146
this.authService.authenticate('google')
147-
.pipe(takeWhile(() => this.alive))
147+
.pipe(takeUntil(this.destroy$))
148148
.subscribe((authResult: NbAuthResult) => {
149149
});
150150
}
151151

152152
ngOnDestroy(): void {
153-
this.alive = false;
153+
this.destroy$.next();
154+
this.destroy$.complete();
154155
}
155156
}
156157
```
@@ -170,11 +171,11 @@ Now, we need to configure that "callback" URL to be able to properly handle resp
170171
})
171172
export class NbOAuth2CallbackPlaygroundComponent implements OnDestroy {
172173

173-
alive = true;
174+
private destroy$ = new Subject<void>();
174175

175176
constructor(private authService: NbAuthService, private router: Router) {
176177
this.authService.authenticate('google')
177-
.pipe(takeWhile(() => this.alive))
178+
.pipe(takeUntil(this.destroy$))
178179
.subscribe((authResult: NbAuthResult) => {
179180
if (authResult.isSuccess()) {
180181
this.router.navigateByUrl('/pages/dashboard');
@@ -183,7 +184,8 @@ export class NbOAuth2CallbackPlaygroundComponent implements OnDestroy {
183184
}
184185

185186
ngOnDestroy(): void {
186-
this.alive = false;
187+
this.destroy$.next();
188+
this.destroy$.complete();
187189
}
188190
}
189191
```

0 commit comments

Comments
 (0)