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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | 1 1 1 1 1 320 161 1 186 186 186 1 186 1 1 186 186 186 186 1 186 186 186 186 1 325 286 286 286 286 286 286 39 1 286 1 163 163 12 1 752 752 136 136 8 128 616 1 39 1 171 171 171 171 20 151 61 1 | import {Observable} from '../Observable'; import {Operator} from '../Operator'; import {Observer} from '../Observer'; import {Subscriber} from '../Subscriber'; import {tryCatch} from '../util/tryCatch'; import {errorObject} from '../util/errorObject'; import {Subscription} from '../Subscription'; import {OuterSubscriber} from '../OuterSubscriber'; import {subscribeToResult} from '../util/subscribeToResult'; export function mergeMapTo<T, R, R2>(observable: Observable<R>, resultSelector?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2 | number, concurrent: number = Number.POSITIVE_INFINITY): Observable<R2> { return this.lift(new MergeMapToOperator(observable, <any>resultSelector, concurrent)); } export class MergeMapToOperator<T, R, R2> implements Operator<Observable<T>, R2> { constructor(private ish: Observable<R> | Promise<R>, private resultSelector?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2, Iprivate concurrent: number = Number.POSITIVE_INFINITY) { } call(observer: Subscriber<R2>): Subscriber<T> { return new MergeMapToSubscriber(observer, this.ish, this.resultSelector, this.concurrent); } } export class MergeMapToSubscriber<T, R, R2> extends OuterSubscriber<T, R> { private hasCompleted: boolean = false; private buffer: Observable<any>[] = []; private active: number = 0; protected index: number = 0; constructor(destination: Subscriber<R2>, private ish: Observable<R> | Promise<R>, private resultSelector?: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2, Iprivate concurrent: number = Number.POSITIVE_INFINITY) { super(destination); } protected _next(value: any): void { if (this.active < this.concurrent) { const resultSelector = this.resultSelector; const index = this.index++; const ish = this.ish; const destination = this.destination; this.active++; this._innerSub(ish, destination, resultSelector, value, index); } else { this.buffer.push(value); } } private _innerSub(ish: any, destination: Observer<R>, resultSelector: (outerValue: T, innerValue: R, outerIndex: number, innerIndex: number) => R2, value: T, index: number): void { this.add(subscribeToResult<T, R>(this, ish, value, index)); } protected _complete(): void { this.hasCompleted = true; if (this.active === 0 && this.buffer.length === 0) { this.destination.complete(); } } notifyNext(outerValue: T, innerValue: R, outerIndex: number, innerIndex: number): void { const { resultSelector, destination } = this; if (resultSelector) { const result = tryCatch(resultSelector)(outerValue, innerValue, outerIndex, innerIndex); if (result === errorObject) { destination.error(errorObject.e); } else { destination.next(result); } } else { destination.next(innerValue); } } notifyError(err: any): void { this.destination.error(err); } notifyComplete(innerSub: Subscription): void { const buffer = this.buffer; this.remove(innerSub); this.active--; if (buffer.length > 0) { this._next(buffer.shift()); } else if (this.active === 0 && this.hasCompleted) { this.destination.complete(); } } } |