Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b173545

Browse files
committedMay 1, 2023
refactor(tabs): safe tabServiceSubscription?.unsubscribe()
1 parent a7673aa commit b173545

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed
 

‎projects/coreui-angular/src/lib/tabs/tab-content-ref.directive.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,25 @@ import {
66
Input,
77
OnChanges,
88
OnDestroy,
9-
OnInit,
109
SimpleChanges
1110
} from '@angular/core';
12-
import { Subscription } from 'rxjs';
1311
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
12+
import { Subscription } from 'rxjs';
1413

1514
import { TabService } from './tab.service';
1615

1716
@Directive({
1817
selector: '[cTabContent]',
1918
standalone: true
2019
})
21-
export class TabContentRefDirective implements OnChanges, OnDestroy, OnInit {
20+
export class TabContentRefDirective implements OnChanges, OnDestroy {
2221

2322
constructor(
2423
private changeDetectorRef: ChangeDetectorRef,
2524
private tabService: TabService
26-
) { }
25+
) {
26+
this.subscribeTabService();
27+
}
2728

2829
static ngAcceptInputType_disabled: BooleanInput;
2930
private tabServiceSubscription!: Subscription;
@@ -46,9 +47,11 @@ export class TabContentRefDirective implements OnChanges, OnDestroy, OnInit {
4647
this.changeDetectorRef.detectChanges();
4748
}
4849
}
50+
4951
get active() {
5052
return this._active;
5153
}
54+
5255
private _active = false;
5356

5457
/**
@@ -59,9 +62,11 @@ export class TabContentRefDirective implements OnChanges, OnDestroy, OnInit {
5962
set disabled(value: boolean) {
6063
this._disabled = coerceBooleanProperty(value);
6164
}
65+
6266
get disabled(): boolean {
6367
return this._disabled || this.tabPaneIdx >= this.tabContentRef?.panes?.length;
6468
}
69+
6570
private _disabled = false;
6671

6772
/**
@@ -116,10 +121,6 @@ export class TabContentRefDirective implements OnChanges, OnDestroy, OnInit {
116121
});
117122
}
118123

119-
ngOnInit(): void {
120-
this.subscribeTabService();
121-
}
122-
123124
ngOnDestroy(): void {
124125
this.subscribeTabService(false);
125126
}
@@ -132,7 +133,7 @@ export class TabContentRefDirective implements OnChanges, OnDestroy, OnInit {
132133
}
133134
});
134135
} else {
135-
this.tabServiceSubscription.unsubscribe();
136+
this.tabServiceSubscription?.unsubscribe();
136137
}
137138
}
138139
}

‎projects/coreui-angular/src/lib/tabs/tab-content/tab-content.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class TabContentComponent implements AfterContentChecked, AfterContentIni
100100
}
101101
});
102102
} else {
103-
this.tabServiceSubscription.unsubscribe();
103+
this.tabServiceSubscription?.unsubscribe();
104104
}
105105
}
106106

‎projects/coreui-angular/src/lib/tabs/tab-pane/tab-pane.component.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
import { ChangeDetectorRef, Component, HostBinding, OnDestroy, OnInit } from '@angular/core';
1+
import { ChangeDetectorRef, Component, HostBinding, OnDestroy } from '@angular/core';
2+
import { coerceBooleanProperty } from '@angular/cdk/coercion';
23
import { Subscription } from 'rxjs';
34

45
import { TabContentComponent } from '../tab-content/tab-content.component';
56
import { ITabContentState, TabService } from '../tab.service';
6-
import { coerceBooleanProperty } from '@angular/cdk/coercion';
77

88
@Component({
99
selector: 'c-tab-pane',
10-
template: `<ng-content></ng-content>`,
10+
template: `
11+
<ng-content></ng-content>`,
1112
styleUrls: ['./tab-pane.component.scss'],
1213
exportAs: 'cTabPane',
1314
standalone: true
1415
})
15-
export class TabPaneComponent implements OnDestroy, OnInit {
16+
export class TabPaneComponent implements OnDestroy {
1617

1718
constructor(
1819
private changeDetectorRef: ChangeDetectorRef,
1920
private tabService: TabService
20-
) { }
21+
) {
22+
this.subscribeTabService();
23+
}
2124

2225
public tabPaneIdx!: number;
2326
public tabContent!: TabContentComponent;
@@ -30,9 +33,11 @@ export class TabPaneComponent implements OnDestroy, OnInit {
3033
this.changeDetectorRef.markForCheck();
3134
}
3235
}
36+
3337
get active(): boolean {
3438
return this._active;
3539
}
40+
3641
private _active: boolean = false;
3742

3843
@HostBinding('class')
@@ -45,10 +50,6 @@ export class TabPaneComponent implements OnDestroy, OnInit {
4550
};
4651
}
4752

48-
ngOnInit(): void {
49-
this.subscribeTabService();
50-
}
51-
5253
ngOnDestroy(): void {
5354
this.subscribeTabService(false);
5455
}
@@ -61,7 +62,7 @@ export class TabPaneComponent implements OnDestroy, OnInit {
6162
}
6263
});
6364
} else {
64-
this.tabServiceSubscription.unsubscribe();
65+
this.tabServiceSubscription?.unsubscribe();
6566
}
6667
}
6768
}

0 commit comments

Comments
 (0)
Please sign in to comment.