ankitjain11 ankitjain11 - 16 days ago 6
Javascript Question

Wait for a stream before invoking another stream

How do I wait for one stream (say,

StreamA
) to return non-null value and then invoke
StreamB
subscribe function. I'm not particularly interested in
StreamA
's value. In turn, I am trying to get
StreamB
's value which might have been updated before the StreamA returned non-null value, and might not have any new events.

I tried, pausable, by looking at this: RxJS: How can I do an "if" with Observables?, but unfortunately could not get it to work. This is because, there are no exported class
pausable
, rxjs v 5.0.0-beta.6.

This is how far, I've come up with, as per the answer.

export class AuthService {
userModel: FirebaseListObservable = this.af.database.list(
/users
);
constructor(private af: AngularFire) {

var user = this.currentAuthor();
var userStream = user;

this.af.auth.flatMap((d) => { console.log(d);return this.userModel.publishReplay(1); });

this.userModel
.subscribe((data) => {
var flag = false;
data.forEach((item) => {
if (item.$key && item.$key === user.uid) {
flag = true;
return;
}
});

if (flag) {
console.log('hello');
} else {
this.userModel.push({
firstName: user.auth.displayName.substr(0, user.auth.displayName.lastIndexOf(' ')),
lastName: user.auth.displayName.substr(user.auth.displayName.lastIndexOf(' '), user.auth.displayName.length),
displayPic: user.auth.photoURL,
provider: user.provider,
uid: user.uid
}
);
}
})
}
public currentAuthor():FirebaseAuthState {
return this.af.auth.getAuth();
}


Hope, I can make myself clear. Even I am getting confused now. :p.

I am new to rxjs and reactive programming. And, any help will be appreciated.

And, btw, thanks for stopping by. :)

Answer

I suppose by plausible you mean pausable? I am not sure what exactly you are trying to achieve here (control flow?). However, if you want streamB value after streamA produces a value, then you can use flatMap.

streamA.flatMapLatest(function (_){return streamB})

That should give you, anytime streamA emits, the values emitted after that time by streamB.

If you want values including the last one B emitted prior to that time, you can use streamBB = streamB.publishReplay(1) and

streamA.flatMapLatest(function (_){return streamBB})

Haven't tested it, so keep me updated if that works.

Comments