Long story short, I have an async concurrency issue with an Authorization system.
I’m using rxjs to do these operations with observables.
In my application should be handled only one at a time that’s want to singIn, signUp or signOut. Any of these operations must wait there’s no currently one of the already running and if that the case they should wait in an ordered queue.
It all started with my signUp form, the function that handles this should register the account data in the credentials database and then write account data in the accounts database. These are two requests done one after another.
The library that handles authentication has an event that’s triggered after credentials are confirmed, which may happen before account data is written in the database.
Everything sum-up: I have a place that write data and the event that reads it but it does when it “feels like it”, I cannot change this. How do I make a variable to flag data being written and wait for it to finish before continuing?
I know exactly what I need here! A queue! Each request should be stacked and wait the previous one to complete before it continues. How do I make this in rxjs? How do I make an observable take place in a queue and wait another observable to finish?
My situation could be visualized like this with the following pseudo-code:
// this calls after signUpWithEmail and before writeDbData.
function onSignInEvent(userId): void {
readDbData(userId).subscribe(); // access to db but signUp hasn't finished yet so gets nothing.
}
function signUp(email, password): Observable<Account> {
return from(signUpWithEmail(email, password)).pipe(
// here credentials registered and event should trigger.
map(userId => createAccountData(userId)),
switchMap(account => writeDbData(account)) // here is saved in db.
);
}

