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