Last active
September 16, 2019 04:58
-
-
Save tomastrajan/ab62f97ee2e07cef7d41614e5c3f0d2d to your computer and use it in GitHub Desktop.
Angular Model Pattern - orchestration
This file contains 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
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