Skip to content

Commit b45b083

Browse files
authored
Merge pull request #7497 from IgniteUI/mvenkov/call_notifychanges_correctly-9.0
Unsubscribe all subscriptions in IgxSelect, 9.0.x
2 parents 3ba4f28 + c99363b commit b45b083

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

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

Lines changed: 45 additions & 26 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,11 +366,15 @@ export class IgxSelectComponent extends IgxDropDownComponent implements IgxSelec
350366
scrollStrategy: new AbsoluteScrollStrategy(),
351367
excludePositionTarget: true
352368
};
353-
this.children.changes.subscribe(() => {
369+
const changes$ = this.children.changes.pipe(takeUntil(this.destroy$)).subscribe(() => {
354370
this.setSelection(this.items.find(x => x.value === this.value));
355371
this.cdr.detectChanges();
356372
});
357-
Promise.resolve().then(() => this.children.notifyOnChanges());
373+
Promise.resolve().then(() => {
374+
if (!changes$.closed) {
375+
this.children.notifyOnChanges();
376+
}
377+
});
358378
}
359379

360380
/** @hidden @internal */
@@ -393,7 +413,7 @@ export class IgxSelectComponent extends IgxDropDownComponent implements IgxSelec
393413
public onBlur(): void {
394414
this._onTouchedCallback();
395415
if (this.ngControl && !this.ngControl.valid) {
396-
this.input.valid = IgxInputState.INVALID;
416+
this.input.valid = IgxInputState.INVALID;
397417
} else {
398418
this.input.valid = IgxInputState.INITIAL;
399419
}
@@ -430,7 +450,7 @@ export class IgxSelectComponent extends IgxDropDownComponent implements IgxSelec
430450
*/
431451
public ngAfterViewInit() {
432452
if (this.ngControl) {
433-
this._statusChanges$ = this.ngControl.statusChanges.subscribe(this.onStatusChanged.bind(this));
453+
this.ngControl.statusChanges.pipe(takeUntil(this.destroy$)).subscribe(this.onStatusChanged.bind(this));
434454
this.manageRequiredAsterisk();
435455
}
436456
this.cdr.detectChanges();
@@ -440,17 +460,16 @@ export class IgxSelectComponent extends IgxDropDownComponent implements IgxSelec
440460
* @hidden @internal
441461
*/
442462
public ngOnDestroy() {
463+
this.destroy$.next(true);
464+
this.destroy$.complete();
443465
this.selection.clear(this.id);
444-
if (this._statusChanges$) {
445-
this._statusChanges$.unsubscribe();
446-
}
447466
}
448467

449468
/**
450469
* @hidden @internal
451470
* Prevent input blur - closing the items container on Header/Footer Template click.
452471
*/
453-
public mousedownHandler(event) {
472+
public mousedownHandler(event) {
454473
event.preventDefault();
455474
}
456475
}

0 commit comments

Comments
 (0)