-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Should race have a special case for synchronous observables? #4806
Comments
No. In short, it is not possible to determine whether or not an observable is synchronous or not without subscribing to it. And |
After diving into the code for race, I can now change this into an actual bug report. Race is intended to stop subscribing as soon as the first Observable emits (see https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/race.ts#L124). However, completion is not setting the I can show this with the same code example, except switching out import { race, of } from 'rxjs';
import { create } from 'rxjs-spy';
import { tag } from 'rxjs-spy/operators';
import { delay, map } from 'rxjs/operators';
create().log(/.*/);
const source = race(
of(1).pipe(delay(100), tag("e0")),
of(1).pipe(tag("e1")),
of(1).pipe(delay(100), tag("e2")),
of(1).pipe(delay(100), tag("e3")),
).pipe(
tag("output"),
)
source.subscribe();
|
I think fixing should be something along the lines of adding this: notifyComplete(innerSub: InnerSubscriber<T, T>): void {
if (!this.hasFirst) {
this.hasFirst = true;
for (let i = 0; i < this.subscriptions.length; i++) {
let subscription = this.subscriptions[i];
subscription.unsubscribe();
this.remove(subscription);
}
this.subscriptions = null;
}
this.destination.complete();
} |
To me, it looks like it's behaving as expected. You have contradictory statements in this comment, so it's difficult to understand what it is that you believe to be the problem. |
I'm trying to create a marbles test case for this, but it turns out that the I've created a new example (https://stackblitz.com/edit/rxjs-w2fll9) and I will explain what happens and what strikes me. In case A (3 asynchronous observables), all side effects are triggered. In my initial report, I said that I think case C is wrong, because that is an unnecessary subscription (because it would immediately be unsubscribed, as B is already the winner). However, one could argue that B is the incorrect case here, as race should just always subscribe to all sources. I don't care whether case B or C is incorrect, I only think that it should not make a difference whether one of the sources completes synchronously or asynchronously. Sidenote: I've added a bonus test-case (D). If you change the concat to |
There is a bug in C. The empty observable should win the race and no further subscriptions should be made, as
This is definitely a bug, but the title and some of the comments in this issue are very confusing. I'll create another issue that better describes and bug and will then close this one. Unless creating the other issue is something that you really want to do yourself. |
Absolutely fine with me. Close it once you created the new one. |
Closed in favour of #4808 |
Potential Bug Report
Current Behavior
If a
race
observable is constructed with at least one synchronous observable, the order of events is confusing.Step 4 could be ommitted in my opinion. If this is intended behavior, maybe put a note about it in the documentation?
Reproduction
https://stackblitz.com/edit/rxjs-ec1mwd
The text was updated successfully, but these errors were encountered: