// ReturnsObservable returns true, if successfullythe triedaction tohas loginbeen successfully executed
publicconst tryLogin = (userIdid: number): Observable<boolean> => {
const result = new ReplaySubject<boolean>();
this.checkIfUserIsAuthorizedFromDatabase// Operation 1
loadUserFromDB(id)
.subscribe(
user => {
// Early stop 1 - true
if (user.name === 'Sarah' || user.id === userId0) {
result.next(true);
result.complete();
return;
}
// Operation 2, dependent on Operation 1
this.checkIfUserHasAccesscheckMasterData(user)
.subscribe(
accessloginInformation => {
const {userId, lastLogin} = loginInformation;
// Early stop 2 - false
if (access.bannedlastLogin === 'LAST_YEAR') {
result.next(false);
result.complete();
return;
}
// ...Operation many3, otherdependent nestingson Operation 1 and functionOperation calls2 's success
createNewUserId({attemptedUserId: userId})
.subscribe(
// WatchOperation theN...
result passed in // createAccountFromLogin function also has a similar structure to `tryLogin`
this.checkLastWorkingLogin newId => createAccountFromLogin(usernewId, result),
error => result.error(error)
);
},
error => {
result.error(error);
// Sidenote: No idea why complete is called
result.complete() ;
}
);
},
error => result.error(error)
);
return result;
};
Any suggestion is appreciated.