New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected withLatestFrom operator behavior #5558
Comments
Hi @stas-karmanov , This is not a bug. Have you noticed that another way of getting the behavior that you are looking for is to change the order of the subscriptions? Like this: const bh1 = new BehaviorSubject<string>(null);
const bh2 = new BehaviorSubject<string>(null);
bh2.pipe(withLatestFrom(bh1)).subscribe(console.warn);
bh1
.pipe(
filter(Boolean),
tap(console.log)
)
.subscribe(bh2);
bh1.next("BH"); If changing the order of the subscriptions is not an option, then you could also observe const bh1 = new BehaviorSubject<string>(null);
const bh2 = new BehaviorSubject<string>(null);
bh1
.pipe(
observeOn(asapScheduler),
filter(Boolean),
tap(console.log)
)
.subscribe(bh2);
bh2.pipe(withLatestFrom(bh1)).subscribe(console.warn);
bh1.next("BH"); If you think about it carefully, it does make sense that your code is behaving in the way that's behaving. Notice that this line: bh2.pipe(withLatestFrom(bh1)).subscribe(console.warn); is roughly the equivalent of: new Observable(observer => {
let latestVal: string | null;
const bh1Subscription = bh1.subscribe(x => {
latestVal = x;
});
const bh2Subscription = bh2.subscribe({
next(val) {
observer.next([val, latestVal] as const);
},
complete() {
observer.complete();
},
error(e: any) {
observer.error(e);
}
});
return () => {
bh2Subscription.unsubscribe();
bh1Subscription.unsubscribe();
};
}).subscribe(console.warn); Notice that there are 2 subscribers listening to That means that when it gets notified, it will synchronously perform a |
@josepot thank you a lot for explanation! |
Bug Report
Current Behavior
You'll see:
bh1
value was updated butwithLatestFrom
operator took previous (null
). The problem can be solved usingdebounceTime
:But I don't want to use such "workarounds" for many reasons.
Expected behavior
withLatestFrom
should pull the latest state even if it was updated synchronously.Environment
The text was updated successfully, but these errors were encountered: