Created
October 16, 2020 02:48
-
-
Save LironHazan/a08e3fb53cc7bb12a0800587609c4289 to your computer and use it in GitHub Desktop.
Lior Levy's example for rxjs post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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