subscribe until destroyed (#3776)

This commit is contained in:
Cilibiu Bogdan 2018-09-12 15:03:12 +03:00 committed by Eugenio Romano
parent 609a6c154b
commit 3857364b39

View File

@ -33,7 +33,7 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { DOCUMENT } from '@angular/common'; import { DOCUMENT } from '@angular/common';
import { Observable, Subject, Subscription, merge, of, fromEvent } from 'rxjs'; import { Observable, Subject, Subscription, merge, of, fromEvent } from 'rxjs';
import { SearchComponent } from './search.component'; import { SearchComponent } from './search.component';
import { filter, switchMap } from 'rxjs/operators'; import { filter, switchMap, takeUntil } from 'rxjs/operators';
export const SEARCH_AUTOCOMPLETE_VALUE_ACCESSOR: any = { export const SEARCH_AUTOCOMPLETE_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR, provide: NG_VALUE_ACCESSOR,
@ -55,6 +55,7 @@ export const SEARCH_AUTOCOMPLETE_VALUE_ACCESSOR: any = {
providers: [SEARCH_AUTOCOMPLETE_VALUE_ACCESSOR] providers: [SEARCH_AUTOCOMPLETE_VALUE_ACCESSOR]
}) })
export class SearchTriggerDirective implements ControlValueAccessor, OnDestroy { export class SearchTriggerDirective implements ControlValueAccessor, OnDestroy {
private onDestroy$: Subject<boolean> = new Subject<boolean>();
@Input('searchAutocomplete') @Input('searchAutocomplete')
searchPanel: SearchComponent; searchPanel: SearchComponent;
@ -76,6 +77,9 @@ export class SearchTriggerDirective implements ControlValueAccessor, OnDestroy {
@Optional() @Inject(DOCUMENT) private document: any) { } @Optional() @Inject(DOCUMENT) private document: any) { }
ngOnDestroy() { ngOnDestroy() {
this.onDestroy$.next(true);
this.onDestroy$.complete();
if (this.escapeEventStream) { if (this.escapeEventStream) {
this.escapeEventStream.unsubscribe(); this.escapeEventStream.unsubscribe();
this.escapeEventStream = null; this.escapeEventStream = null;
@ -187,6 +191,7 @@ export class SearchTriggerDirective implements ControlValueAccessor, OnDestroy {
return merge(firstStable, optionChanges) return merge(firstStable, optionChanges)
.pipe( .pipe(
takeUntil(this.onDestroy$),
switchMap(() => { switchMap(() => {
this.searchPanel.setVisibility(); this.searchPanel.setVisibility();
return this.panelClosingActions; return this.panelClosingActions;