Skip to content

Commit c437805

Browse files
authored
Merge pull request #7495 from IgniteUI/mvenkov/call_notifychanges_correctly
Unsubscribe all subscriptions in IgxSelect
2 parents 113f9da + b4ee40b commit c437805

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

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

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { IgxInputDirective, IgxInputState } from './../directives/input/input.directive';
21
import {
32
AfterContentInit,
43
AfterViewInit,
@@ -20,24 +19,27 @@ import {
2019
TemplateRef,
2120
ViewChild
2221
} from '@angular/core';
23-
import { ControlValueAccessor, NG_VALUE_ACCESSOR, NgControl, AbstractControl } from '@angular/forms';
24-
import { Subscription } from 'rxjs';
25-
26-
import { IgxDropDownItemBaseDirective } from '../drop-down/public_api';
22+
import { AbstractControl, ControlValueAccessor, NgControl, NG_VALUE_ACCESSOR } from '@angular/forms';
23+
import { 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 { IgxDropDownItemBaseDirective } from '../drop-down/drop-down-item.base';
31+
import { IGX_DROPDOWN_BASE, ISelectionEventArgs, Navigate } from '../drop-down/drop-down.common';
2732
import { IgxInputGroupComponent } from '../input-group/input-group.component';
28-
33+
import { AbsoluteScrollStrategy } from '../services/overlay/scroll/absolute-scroll-strategy';
34+
import { OverlaySettings } from '../services/overlay/utilities';
35+
import { IgxInputDirective, IgxInputState } from './../directives/input/input.directive';
2936
import { IgxDropDownComponent } from './../drop-down/drop-down.component';
3037
import { IgxSelectItemComponent } from './select-item.component';
3138
import { SelectPositioningStrategy } from './select-positioning-strategy';
32-
33-
import { OverlaySettings, AbsoluteScrollStrategy } from '../services/public_api';
34-
import { IGX_DROPDOWN_BASE, ISelectionEventArgs, Navigate } from '../drop-down/drop-down.common';
35-
import { CancelableEventArgs } from '../core/utils';
36-
import { IgxLabelDirective } from '../directives/label/label.directive';
3739
import { IgxSelectBase } from './select.common';
38-
import { EditorProvider } from '../core/edit-provider';
39-
import { IgxSelectionAPIService } from '../core/selection';
40-
import { DisplayDensityToken, IDisplayDensityOptions } from '../core/density';
40+
41+
42+
4143

4244
/** @hidden @internal */
4345
@Directive({
@@ -94,9 +96,9 @@ export class IgxSelectComponent extends IgxDropDownComponent implements IgxSelec
9496
AfterContentInit, OnInit, AfterViewInit, OnDestroy, EditorProvider {
9597

9698
private ngControl: NgControl = null;
97-
private _statusChanges$: Subscription;
9899
private _overlayDefaults: OverlaySettings;
99100
private _value: any;
101+
protected destroy$ = new Subject<boolean>();
100102

101103
/** @hidden @internal do not use the drop-down container class */
102104
public cssClass = false;
@@ -365,11 +367,15 @@ export class IgxSelectComponent extends IgxDropDownComponent implements IgxSelec
365367
scrollStrategy: new AbsoluteScrollStrategy(),
366368
excludePositionTarget: true
367369
};
368-
this.children.changes.subscribe(() => {
370+
const changes$ = this.children.changes.pipe(takeUntil(this.destroy$)).subscribe(() => {
369371
this.setSelection(this.items.find(x => x.value === this.value));
370372
this.cdr.detectChanges();
371373
});
372-
Promise.resolve().then(() => this.children.notifyOnChanges());
374+
Promise.resolve().then(() => {
375+
if (!changes$.closed) {
376+
this.children.notifyOnChanges();
377+
}
378+
});
373379
}
374380

375381
/** @hidden @internal */
@@ -408,7 +414,7 @@ export class IgxSelectComponent extends IgxDropDownComponent implements IgxSelec
408414
public onBlur(): void {
409415
this._onTouchedCallback();
410416
if (this.ngControl && !this.ngControl.valid) {
411-
this.input.valid = IgxInputState.INVALID;
417+
this.input.valid = IgxInputState.INVALID;
412418
} else {
413419
this.input.valid = IgxInputState.INITIAL;
414420
}
@@ -445,7 +451,7 @@ export class IgxSelectComponent extends IgxDropDownComponent implements IgxSelec
445451
*/
446452
public ngAfterViewInit() {
447453
if (this.ngControl) {
448-
this._statusChanges$ = this.ngControl.statusChanges.subscribe(this.onStatusChanged.bind(this));
454+
this.ngControl.statusChanges.pipe(takeUntil(this.destroy$)).subscribe(this.onStatusChanged.bind(this));
449455
this.manageRequiredAsterisk();
450456
}
451457
this.cdr.detectChanges();
@@ -455,17 +461,16 @@ export class IgxSelectComponent extends IgxDropDownComponent implements IgxSelec
455461
* @hidden @internal
456462
*/
457463
public ngOnDestroy() {
464+
this.destroy$.next(true);
465+
this.destroy$.complete();
458466
this.selection.clear(this.id);
459-
if (this._statusChanges$) {
460-
this._statusChanges$.unsubscribe();
461-
}
462467
}
463468

464469
/**
465470
* @hidden @internal
466471
* Prevent input blur - closing the items container on Header/Footer Template click.
467472
*/
468-
public mousedownHandler(event) {
473+
public mousedownHandler(event) {
469474
event.preventDefault();
470475
}
471476
}

0 commit comments

Comments
 (0)