Skip to content

Instantly share code, notes, and snippets.

@tomastrajan
Last active September 16, 2019 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomastrajan/ab62f97ee2e07cef7d41614e5c3f0d2d to your computer and use it in GitHub Desktop.
Save tomastrajan/ab62f97ee2e07cef7d41614e5c3f0d2d to your computer and use it in GitHub Desktop.
Angular Model Pattern - orchestration
export class AuthApplicationService {
// inject all needed services
constructor(
private sessionService: SessionService,
private authService: AuthService
) {}
// orchestrate multi service execution
// note that subscription and cancelation are responsibility of the caller
login(username: string, password: string): Observable<boolean> {
return this.authService.login(username, password) // returns Observable<User>
.do(user => this.sessionService.setUser(user)) // .do() to perform side-effects, mutate model (sync)
.mapTo(true); // return boolean flag
.catch(err => this.sessionService.setUser(null)); // handle failure, mutate model (sync)
}
/* ... */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment