Skip to content

Instantly share code, notes, and snippets.

@webcat12345
Created July 9, 2018 15:11
Show Gist options
  • Save webcat12345/126fabfc4d8b5e2eb64c3301c413801d to your computer and use it in GitHub Desktop.
Save webcat12345/126fabfc4d8b5e2eb64c3301c413801d to your computer and use it in GitHub Desktop.
Simple usage of Rxjs 6 operators and functions with Typescript
this.signUpForm.get('email').valueChanges
.pipe(
tap(() => this.isInvalidEmail = false),
debounceTime(500),
filter(value => CommonService.validateEmail(value)),
tap((x) => {
this.isCheckingEmail = true;
this.isInvalidEmail = false;
}),
switchMap(x => this.authService.checkEmail(x)),
tap((x) => {
this.isCheckingEmail = false;
this.isInvalidEmail = x;
}),
map(x => !x)
).subscribe(() => {});
async register() {
this.isLoading = true;
await this.authService.register(this.signUpForm.value)
.pipe(
tap(() => {
this.commonSerivce.openSnackbar('Successfully registered.');
this.router.navigate(['/login']);
}),
finalize(() => this.isLoading = false),
catchError((e) => {
this.commonSerivce.openSnackbar(e.error.message);
return of(e);
}))
.toPromise();
}
/*** Rxjs usage with ngrx ***/
@Effect()
loadTeamUsers$ = this.actions$.pipe(
ofType(TeamActionTypes.LoadTeamUsers),
switchMap((action: LoadTeamUsers) =>
this.teamService.getTeamMembers(action.payload.id).pipe(
map((users: TeamUser[]) => new LoadTeamUsersSuccess(users)),
catchError(error => of(new LoadTeamUsersFailure()))
)
)
);
/*** Wrapping observables together ***/
private loadTeamData(teamId) {
return combineLatest(this.teamService.getTeamGroups(teamId), this.teamService.getTeamMembers(teamId)).pipe(
map((res: [Array<Group>, Array<TeamUser>]) => {
return {groups: res[0], users: res[1]};
})
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment