race.js
2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { isArray } from '../util/isArray';
import { fromArray } from './fromArray';
import { OuterSubscriber } from '../OuterSubscriber';
import { subscribeToResult } from '../util/subscribeToResult';
export function race(...observables) {
if (observables.length === 1) {
if (isArray(observables[0])) {
observables = observables[0];
}
else {
return observables[0];
}
}
return fromArray(observables, undefined).lift(new RaceOperator());
}
export class RaceOperator {
call(subscriber, source) {
return source.subscribe(new RaceSubscriber(subscriber));
}
}
export class RaceSubscriber extends OuterSubscriber {
constructor(destination) {
super(destination);
this.hasFirst = false;
this.observables = [];
this.subscriptions = [];
}
_next(observable) {
this.observables.push(observable);
}
_complete() {
const observables = this.observables;
const len = observables.length;
if (len === 0) {
this.destination.complete();
}
else {
for (let i = 0; i < len && !this.hasFirst; i++) {
const observable = observables[i];
const subscription = subscribeToResult(this, observable, undefined, i);
if (this.subscriptions) {
this.subscriptions.push(subscription);
}
this.add(subscription);
}
this.observables = null;
}
}
notifyNext(_outerValue, innerValue, outerIndex) {
if (!this.hasFirst) {
this.hasFirst = true;
for (let i = 0; i < this.subscriptions.length; i++) {
if (i !== outerIndex) {
let subscription = this.subscriptions[i];
subscription.unsubscribe();
this.remove(subscription);
}
}
this.subscriptions = null;
}
this.destination.next(innerValue);
}
}
//# sourceMappingURL=race.js.map