From 9f833ebe2694904e56a8dc074ded402bfd27ac29 Mon Sep 17 00:00:00 2001 From: Sergey Date: Tue, 29 Aug 2017 13:14:30 +0300 Subject: [PATCH] feat(typeahead): added subscription and unsubscribe on destroy (#2508) #2382 Also fixed bug, when navigating to other router before asyncActions() end. --- src/typeahead/typeahead.directive.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/typeahead/typeahead.directive.ts b/src/typeahead/typeahead.directive.ts index 97941712c9..a47ba747b6 100644 --- a/src/typeahead/typeahead.directive.ts +++ b/src/typeahead/typeahead.directive.ts @@ -7,6 +7,7 @@ import { TypeaheadContainerComponent } from './typeahead-container.component'; import { getValueFromObject, latinize, tokenize } from './typeahead-utils'; import { Observable } from 'rxjs/Observable'; +import { Subscription } from 'rxjs/Subscription'; import 'rxjs/add/observable/from'; import 'rxjs/add/operator/debounceTime'; import 'rxjs/add/operator/filter'; @@ -91,6 +92,7 @@ export class TypeaheadDirective implements OnInit, OnDestroy { protected renderer: Renderer; private _typeahead: ComponentLoader; + private _subscriptions: Subscription[] = []; @HostListener('keyup', ['$event']) public onChange(e: any): void { @@ -247,11 +249,15 @@ export class TypeaheadDirective implements OnInit, OnDestroy { } public ngOnDestroy(): any { + // clean up subscriptions + for (const sub of this._subscriptions) { + sub.unsubscribe(); + } this._typeahead.dispose(); } protected asyncActions(): void { - this.keyUpEventEmitter + this._subscriptions.push(this.keyUpEventEmitter .debounceTime(this.typeaheadWaitMs) .mergeMap(() => this.typeahead) .subscribe( @@ -261,11 +267,11 @@ export class TypeaheadDirective implements OnInit, OnDestroy { (err: any) => { console.error(err); } - ); + )); } protected syncActions(): void { - this.keyUpEventEmitter + this._subscriptions.push(this.keyUpEventEmitter .debounceTime(this.typeaheadWaitMs) .mergeMap((value: string) => { let normalizedQuery = this.normalizeQuery(value); @@ -283,7 +289,7 @@ export class TypeaheadDirective implements OnInit, OnDestroy { (err: any) => { console.error(err); } - ); + )); } protected normalizeOption(option: any): any {