Skip to content

Instantly share code, notes, and snippets.

@ardydedase
Last active September 30, 2021 04:39
Show Gist options
  • Save ardydedase/f01ffabf8b3dae6b73277128ec5eecbd to your computer and use it in GitHub Desktop.
Save ardydedase/f01ffabf8b3dae6b73277128ec5eecbd to your computer and use it in GitHub Desktop.
takeUntiNgOnInit.ts
ngOnInit() {
this.results$ = this.searchSubject$.pipe(
debounceTime(200),
distinctUntilChanged(),
switchMap((searchString) => {
if ((searchString as any).replace(/\s/g, '')) {
switch (this.searchType) {
case SearchType.REPOS:
return this.githubService.searchRepos(searchString);
case SearchType.USERS:
return this.githubService.searchUsers(searchString);
default:
break;
}
}
})
);
this.searchReposFormControl.valueChanges
.pipe(takeUntil(this.destroyed$))
.subscribe((searchString) => {
if (searchString) {
this.searchType = SearchType.REPOS;
this.searchSubject$.next(searchString);
}
});
this.searchUsersFormControl.valueChanges
.pipe(takeUntil(this.destroyed$))
.subscribe((searchString) => {
if (searchString) {
this.searchType = SearchType.USERS;
this.searchSubject$.next(searchString);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment