Skip to content

Commit

Permalink
fix(race): handle observables completes immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Apr 14, 2016
1 parent 815f775 commit e54111e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 10 additions & 0 deletions spec/operators/race-spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {expect} from 'chai';
import * as Rx from '../../dist/cjs/Rx';
declare const {hot, cold, expectObservable, expectSubscriptions};

Expand Down Expand Up @@ -147,4 +148,13 @@ describe('Observable.prototype.race', () => {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});

it('should allow observable emits immediately', (done: MochaDone) => {
const e1 = Observable.of(true);
const e2 = Observable.timer(200).map(_ => false);

Observable.race(e1, e2).subscribe(x => {
expect(x).to.be.true;
}, done, done);
});
});
7 changes: 5 additions & 2 deletions src/operator/race.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,18 @@ export class RaceSubscriber<T> extends OuterSubscriber<T, T> {
protected _complete() {
const observables = this.observables;
const len = observables.length;

if (len === 0) {
this.destination.complete();
} else {
for (let i = 0; i < len; i++) {
let observable = observables[i];
let subscription = subscribeToResult(this, observable, observable, i);

this.subscriptions.push(subscription);
this.add(subscription);
if (this.subscriptions) {
this.subscriptions.push(subscription);
this.add(subscription);
}
}
this.observables = null;
}
Expand Down

0 comments on commit e54111e

Please sign in to comment.