Skip to content

Commit

Permalink
feat(typeahead): added subscription and unsubscribe on destroy (#2508)
Browse files Browse the repository at this point in the history
#2382

Also fixed bug, when navigating to other router before asyncActions() end.
  • Loading branch information
krooshua authored and valorkin committed Aug 29, 2017
1 parent 96a72c3 commit 9f833eb
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/typeahead/typeahead.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -91,6 +92,7 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
protected renderer: Renderer;

private _typeahead: ComponentLoader<TypeaheadContainerComponent>;
private _subscriptions: Subscription[] = [];

@HostListener('keyup', ['$event'])
public onChange(e: any): void {
Expand Down Expand Up @@ -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(
Expand All @@ -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);
Expand All @@ -283,7 +289,7 @@ export class TypeaheadDirective implements OnInit, OnDestroy {
(err: any) => {
console.error(err);
}
);
));
}

protected normalizeOption(option: any): any {
Expand Down

0 comments on commit 9f833eb

Please sign in to comment.