Skip to content

Instantly share code, notes, and snippets.

@seveves
Last active January 27, 2017 13:02
Show Gist options
  • Save seveves/3c7b25dfc1501aafef70e1ab00782b04 to your computer and use it in GitHub Desktop.
Save seveves/3c7b25dfc1501aafef70e1ab00782b04 to your computer and use it in GitHub Desktop.
yip
public ngOnInit() {
this.navigationResults = this.helpDataService.getMainGroups()
.concat(this.filterBox.valueChanges
.let((text: Observable<string>) => this.filterAndDebounceFor(text)(500))
.switchMap((filterTerm: string) =>
this.defaultObservableIfEmpty(
this.helpDataService.getNavigationByFilter(filterTerm),
this.helpDataService.getMainGroups())));
}
private filterAndDebounceFor = (input: Observable<string>) => {
return (delay: number) => input
.filter((text: string) => text.length > 2 || text.length === 0)
.debounceTime(delay);
}
private defaultObservableIfEmpty = (current: Observable<any>, fallback: Observable<any>) => {
return current.publish((source: Observable<any>) =>
source.merge(source.isEmpty().filter((e: boolean) => e).mergeMapTo(fallback)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment