Skip to content
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

fix(race): ignore latter sources after first complete or error #4809

Merged
merged 2 commits into from Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion spec/operators/race-spec.ts
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';
import { NEVER, of, race as staticRace, timer, defer, Observable } from 'rxjs';
import { EMPTY, NEVER, of, race as staticRace, timer, defer, Observable, throwError } from 'rxjs';
import { race, mergeMap, map, finalize, startWith } from 'rxjs/operators';

/** @test {race} */
Expand Down Expand Up @@ -184,6 +184,28 @@ describe('race operator', () => {
expect(onSubscribe.called).to.be.false;
});

it('should ignore latter observables if a former one completes immediately', () => {
const onComplete = sinon.spy();
const onSubscribe = sinon.spy();
const e1 = EMPTY; // Wins the race
const e2 = defer(onSubscribe); // Should be ignored

e1.pipe(race(e2)).subscribe({ complete: onComplete });
expect(onComplete.calledWithExactly()).to.be.true;
expect(onSubscribe.called).to.be.false;
});

it('should ignore latter observables if a former one errors immediately', () => {
const onError = sinon.spy();
const onSubscribe = sinon.spy();
const e1 = throwError('kaboom'); // Wins the race
const e2 = defer(onSubscribe); // Should be ignored

e1.pipe(race(e2)).subscribe({ error: onError });
expect(onError.calledWithExactly('kaboom')).to.be.true;
expect(onSubscribe.called).to.be.false;
});

it('should unsubscribe former observables if a latter one emits immediately', () => {
const onNext = sinon.spy();
const onUnsubscribe = sinon.spy();
Expand Down
10 changes: 10 additions & 0 deletions src/internal/observable/race.ts
Expand Up @@ -137,4 +137,14 @@ export class RaceSubscriber<T> extends OuterSubscriber<T, T> {

this.destination.next(innerValue);
}

notifyComplete(innerSub: InnerSubscriber<T, T>): void {
this.hasFirst = true;
super.notifyComplete(innerSub);
}

notifyError(error: any, innerSub: InnerSubscriber<T, T>): void {
this.hasFirst = true;
super.notifyError(error, innerSub);
}
}