|
| 1 | +import { Observable } from '../Observable'; |
| 2 | +import { isArray } from '../util/isArray'; |
| 3 | +import { ArrayObservable } from '../observable/ArrayObservable'; |
| 4 | +import { Operator } from '../Operator'; |
| 5 | +import { Subscriber } from '../Subscriber'; |
| 6 | +import { Subscription, TeardownLogic } from '../Subscription'; |
| 7 | +import { OuterSubscriber } from '../OuterSubscriber'; |
| 8 | +import { InnerSubscriber } from '../InnerSubscriber'; |
| 9 | +import { subscribeToResult } from '../util/subscribeToResult'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Returns an Observable that mirrors the first source Observable to emit an item. |
| 13 | + * @param {...Observables} ...observables sources used to race for which Observable emits first. |
| 14 | + * @return {Observable} an Observable that mirrors the output of the first Observable to emit an item. |
| 15 | + * @static true |
| 16 | + * @name race |
| 17 | + * @owner Observable |
| 18 | + */ |
| 19 | +export function race<T>(observables: Array<Observable<T>>): Observable<T>; |
| 20 | +export function race<T>(observables: Array<Observable<any>>): Observable<T>; |
| 21 | +export function race<T>(...observables: Array<Observable<T> | Array<Observable<T>>>): Observable<T>; |
| 22 | +export function race<T>(...observables: Array<Observable<any> | Array<Observable<any>>>): Observable<T> { |
| 23 | + // if the only argument is an array, it was most likely called with |
| 24 | + // `race([obs1, obs2, ...])` |
| 25 | + if (observables.length === 1) { |
| 26 | + if (isArray(observables[0])) { |
| 27 | + observables = <Array<Observable<any>>>observables[0]; |
| 28 | + } else { |
| 29 | + return <Observable<any>>observables[0]; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return new ArrayObservable<T>(<any>observables).lift(new RaceOperator<T>()); |
| 34 | +} |
| 35 | + |
| 36 | +export class RaceOperator<T> implements Operator<T, T> { |
| 37 | + call(subscriber: Subscriber<T>, source: any): TeardownLogic { |
| 38 | + return source.subscribe(new RaceSubscriber(subscriber)); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * We need this JSDoc comment for affecting ESDoc. |
| 44 | + * @ignore |
| 45 | + * @extends {Ignored} |
| 46 | + */ |
| 47 | +export class RaceSubscriber<T> extends OuterSubscriber<T, T> { |
| 48 | + private hasFirst: boolean = false; |
| 49 | + private observables: Observable<any>[] = []; |
| 50 | + private subscriptions: Subscription[] = []; |
| 51 | + |
| 52 | + constructor(destination: Subscriber<T>) { |
| 53 | + super(destination); |
| 54 | + } |
| 55 | + |
| 56 | + protected _next(observable: any): void { |
| 57 | + this.observables.push(observable); |
| 58 | + } |
| 59 | + |
| 60 | + protected _complete() { |
| 61 | + const observables = this.observables; |
| 62 | + const len = observables.length; |
| 63 | + |
| 64 | + if (len === 0) { |
| 65 | + this.destination.complete(); |
| 66 | + } else { |
| 67 | + for (let i = 0; i < len && !this.hasFirst; i++) { |
| 68 | + let observable = observables[i]; |
| 69 | + let subscription = subscribeToResult(this, observable, observable, i); |
| 70 | + |
| 71 | + if (this.subscriptions) { |
| 72 | + this.subscriptions.push(subscription); |
| 73 | + } |
| 74 | + this.add(subscription); |
| 75 | + } |
| 76 | + this.observables = null; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + notifyNext(outerValue: T, innerValue: T, |
| 81 | + outerIndex: number, innerIndex: number, |
| 82 | + innerSub: InnerSubscriber<T, T>): void { |
| 83 | + if (!this.hasFirst) { |
| 84 | + this.hasFirst = true; |
| 85 | + |
| 86 | + for (let i = 0; i < this.subscriptions.length; i++) { |
| 87 | + if (i !== outerIndex) { |
| 88 | + let subscription = this.subscriptions[i]; |
| 89 | + |
| 90 | + subscription.unsubscribe(); |
| 91 | + this.remove(subscription); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + this.subscriptions = null; |
| 96 | + } |
| 97 | + |
| 98 | + this.destination.next(innerValue); |
| 99 | + } |
| 100 | +} |
0 commit comments