Skip to content

Commit 5b42f17

Browse files
committed
fix(select): clear all subscriptions, #7006
1 parent ddee12e commit 5b42f17

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

projects/igniteui-angular/src/lib/select/select.component.ts

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,44 @@
1-
import { IgxInputDirective, IgxInputState } from './../directives/input/input.directive';
21
import {
3-
Component, ContentChildren, forwardRef, QueryList, ViewChild, Input, ContentChild,
4-
AfterContentInit, HostBinding, Directive, TemplateRef, ElementRef, ChangeDetectorRef, Optional,
5-
Injector, OnInit, AfterViewInit, OnDestroy, Inject, Type
6-
2+
AfterContentInit,
3+
AfterViewInit,
4+
ChangeDetectorRef,
5+
Component,
6+
ContentChild,
7+
ContentChildren,
8+
Directive,
9+
ElementRef,
10+
forwardRef,
11+
HostBinding,
12+
Inject,
13+
Injector,
14+
Input,
15+
OnDestroy,
16+
OnInit,
17+
Optional,
18+
QueryList,
19+
TemplateRef,
20+
ViewChild
721
} from '@angular/core';
8-
import { ControlValueAccessor, NG_VALUE_ACCESSOR, NgControl, AbstractControl } from '@angular/forms';
9-
import { Subscription } from 'rxjs';
10-
22+
import { AbstractControl, ControlValueAccessor, NgControl, NG_VALUE_ACCESSOR } from '@angular/forms';
23+
import { Subscription, Subject } from 'rxjs';
24+
import { takeUntil } from 'rxjs/operators';
25+
import { DisplayDensityToken, IDisplayDensityOptions } from '../core/density';
26+
import { EditorProvider } from '../core/edit-provider';
27+
import { IgxSelectionAPIService } from '../core/selection';
28+
import { CancelableEventArgs } from '../core/utils';
29+
import { IgxLabelDirective } from '../directives/label/label.directive';
30+
import { IGX_DROPDOWN_BASE, ISelectionEventArgs, Navigate } from '../drop-down/drop-down.common';
1131
import { IgxDropDownItemBaseDirective } from '../drop-down/index';
1232
import { IgxInputGroupComponent } from '../input-group/input-group.component';
13-
33+
import { AbsoluteScrollStrategy, OverlaySettings } from '../services/index';
34+
import { IgxInputDirective, IgxInputState } from './../directives/input/input.directive';
1435
import { IgxDropDownComponent } from './../drop-down/drop-down.component';
1536
import { IgxSelectItemComponent } from './select-item.component';
1637
import { SelectPositioningStrategy } from './select-positioning-strategy';
17-
18-
import { OverlaySettings, AbsoluteScrollStrategy } from '../services/index';
19-
import { IGX_DROPDOWN_BASE, ISelectionEventArgs, Navigate } from '../drop-down/drop-down.common';
20-
import { CancelableEventArgs } from '../core/utils';
21-
import { IgxLabelDirective } from '../directives/label/label.directive';
2238
import { IgxSelectBase } from './select.common';
23-
import { EditorProvider } from '../core/edit-provider';
24-
import { IgxSelectionAPIService } from '../core/selection';
25-
import { DisplayDensityToken, IDisplayDensityOptions } from '../core/density';
39+
40+
41+
2642

2743
/** @hidden @internal */
2844
@Directive({
@@ -79,9 +95,9 @@ export class IgxSelectComponent extends IgxDropDownComponent implements IgxSelec
7995
AfterContentInit, OnInit, AfterViewInit, OnDestroy, EditorProvider {
8096

8197
private ngControl: NgControl = null;
82-
private _statusChanges$: Subscription;
8398
private _overlayDefaults: OverlaySettings;
8499
private _value: any;
100+
protected destroy$ = new Subject<boolean>();
85101

86102
/** @hidden @internal do not use the drop-down container class */
87103
public cssClass = false;
@@ -350,7 +366,7 @@ export class IgxSelectComponent extends IgxDropDownComponent implements IgxSelec
350366
scrollStrategy: new AbsoluteScrollStrategy(),
351367
excludePositionTarget: true
352368
};
353-
this.children.changes.subscribe(() => {
369+
this.children.changes.pipe(takeUntil(this.destroy$)).subscribe(() => {
354370
this.setSelection(this.items.find(x => x.value === this.value));
355371
this.cdr.detectChanges();
356372
});
@@ -393,7 +409,7 @@ export class IgxSelectComponent extends IgxDropDownComponent implements IgxSelec
393409
public onBlur(): void {
394410
this._onTouchedCallback();
395411
if (this.ngControl && !this.ngControl.valid) {
396-
this.input.valid = IgxInputState.INVALID;
412+
this.input.valid = IgxInputState.INVALID;
397413
} else {
398414
this.input.valid = IgxInputState.INITIAL;
399415
}
@@ -430,7 +446,7 @@ export class IgxSelectComponent extends IgxDropDownComponent implements IgxSelec
430446
*/
431447
public ngAfterViewInit() {
432448
if (this.ngControl) {
433-
this._statusChanges$ = this.ngControl.statusChanges.subscribe(this.onStatusChanged.bind(this));
449+
this.ngControl.statusChanges.pipe(takeUntil(this.destroy$)).subscribe(this.onStatusChanged.bind(this));
434450
this.manageRequiredAsterisk();
435451
}
436452
this.cdr.detectChanges();
@@ -440,17 +456,16 @@ export class IgxSelectComponent extends IgxDropDownComponent implements IgxSelec
440456
* @hidden @internal
441457
*/
442458
public ngOnDestroy() {
459+
this.destroy$.next(true);
460+
this.destroy$.complete();
443461
this.selection.clear(this.id);
444-
if (this._statusChanges$) {
445-
this._statusChanges$.unsubscribe();
446-
}
447462
}
448463

449464
/**
450465
* @hidden @internal
451466
* Prevent input blur - closing the items container on Header/Footer Template click.
452467
*/
453-
public mousedownHandler(event) {
468+
public mousedownHandler(event) {
454469
event.preventDefault();
455470
}
456471
}

0 commit comments

Comments
 (0)