Skip to content

Instantly share code, notes, and snippets.

@LironHazan
Created October 16, 2020 02:48
Show Gist options
  • Save LironHazan/a08e3fb53cc7bb12a0800587609c4289 to your computer and use it in GitHub Desktop.
Save LironHazan/a08e3fb53cc7bb12a0800587609c4289 to your computer and use it in GitHub Desktop.
Lior Levy's example for rxjs post
private searchQuery$ = new BehaviorSubject('');
result$: Observable<string>;
ngOnInit(): void {
this.result$ = this.searchQuery$.pipe(
debounceTime(300), // will drop some values if they occur too frequently
switchMap(() =>this.service.fetchSomething(
this.searchQuery$.getValue()) // will switch to the new inner observable when source emits and cancel the previus one
)
);
}
// The input component will use onChangeSearchText as handler for input changes
onChangeSearchText(freeText: string) {
this.searchQuery$.next(freeText);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment