|
| 1 | +import { Observable, ObservableInput } from '../Observable'; |
| 2 | +import { Operator } from '../Operator'; |
| 3 | +import { Subscriber } from '../Subscriber'; |
| 4 | +import { Subscription } from '../Subscription'; |
| 5 | +import { subscribeToResult } from '../util/subscribeToResult'; |
| 6 | +import { OuterSubscriber } from '../OuterSubscriber'; |
| 7 | +import { InnerSubscriber } from '../InnerSubscriber'; |
| 8 | +import { OperatorFunction } from './OperatorFunction'; |
| 9 | + |
| 10 | +/* tslint:disable:max-line-length */ |
| 11 | +export function mergeMap<T, R>(project: (value: T, index: number) => ObservableInput<R>, concurrent?: number): OperatorFunction<T, R>; |
| 12 | +export function mergeMap<T, I, R>(project: (value: T, index: number) => ObservableInput<I>, resultSelector: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R, concurrent?: number): OperatorFunction<T, R>; |
| 13 | +/* tslint:enable:max-line-length */ |
| 14 | + |
| 15 | +/** |
| 16 | + * Projects each source value to an Observable which is merged in the output |
| 17 | + * Observable. |
| 18 | + * |
| 19 | + * <span class="informal">Maps each value to an Observable, then flattens all of |
| 20 | + * these inner Observables using {@link mergeAll}.</span> |
| 21 | + * |
| 22 | + * <img src="./img/mergeMap.png" width="100%"> |
| 23 | + * |
| 24 | + * Returns an Observable that emits items based on applying a function that you |
| 25 | + * supply to each item emitted by the source Observable, where that function |
| 26 | + * returns an Observable, and then merging those resulting Observables and |
| 27 | + * emitting the results of this merger. |
| 28 | + * |
| 29 | + * @example <caption>Map and flatten each letter to an Observable ticking every 1 second</caption> |
| 30 | + * var letters = Rx.Observable.of('a', 'b', 'c'); |
| 31 | + * var result = letters.mergeMap(x => |
| 32 | + * Rx.Observable.interval(1000).map(i => x+i) |
| 33 | + * ); |
| 34 | + * result.subscribe(x => console.log(x)); |
| 35 | + * |
| 36 | + * // Results in the following: |
| 37 | + * // a0 |
| 38 | + * // b0 |
| 39 | + * // c0 |
| 40 | + * // a1 |
| 41 | + * // b1 |
| 42 | + * // c1 |
| 43 | + * // continues to list a,b,c with respective ascending integers |
| 44 | + * |
| 45 | + * @see {@link concatMap} |
| 46 | + * @see {@link exhaustMap} |
| 47 | + * @see {@link merge} |
| 48 | + * @see {@link mergeAll} |
| 49 | + * @see {@link mergeMapTo} |
| 50 | + * @see {@link mergeScan} |
| 51 | + * @see {@link switchMap} |
| 52 | + * |
| 53 | + * @param {function(value: T, ?index: number): ObservableInput} project A function |
| 54 | + * that, when applied to an item emitted by the source Observable, returns an |
| 55 | + * Observable. |
| 56 | + * @param {function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any} [resultSelector] |
| 57 | + * A function to produce the value on the output Observable based on the values |
| 58 | + * and the indices of the source (outer) emission and the inner Observable |
| 59 | + * emission. The arguments passed to this function are: |
| 60 | + * - `outerValue`: the value that came from the source |
| 61 | + * - `innerValue`: the value that came from the projected Observable |
| 62 | + * - `outerIndex`: the "index" of the value that came from the source |
| 63 | + * - `innerIndex`: the "index" of the value from the projected Observable |
| 64 | + * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of input |
| 65 | + * Observables being subscribed to concurrently. |
| 66 | + * @return {Observable} An Observable that emits the result of applying the |
| 67 | + * projection function (and the optional `resultSelector`) to each item emitted |
| 68 | + * by the source Observable and merging the results of the Observables obtained |
| 69 | + * from this transformation. |
| 70 | + * @method mergeMap |
| 71 | + * @owner Observable |
| 72 | + */ |
| 73 | +export function mergeMap<T, I, R>(project: (value: T, index: number) => ObservableInput<I>, |
| 74 | + resultSelector?: ((outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R) | number, |
| 75 | + concurrent: number = Number.POSITIVE_INFINITY): OperatorFunction<T, I|R> { |
| 76 | + return function mergeMapOperatorFunction(source: Observable<T>) { |
| 77 | + if (typeof resultSelector === 'number') { |
| 78 | + concurrent = <number>resultSelector; |
| 79 | + resultSelector = null; |
| 80 | + } |
| 81 | + return source.lift(new MergeMapOperator(project, <any>resultSelector, concurrent)); |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +export class MergeMapOperator<T, I, R> implements Operator<T, I> { |
| 86 | + constructor(private project: (value: T, index: number) => ObservableInput<I>, |
| 87 | + private resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R, |
| 88 | + private concurrent: number = Number.POSITIVE_INFINITY) { |
| 89 | + } |
| 90 | + |
| 91 | + call(observer: Subscriber<I>, source: any): any { |
| 92 | + return source.subscribe(new MergeMapSubscriber( |
| 93 | + observer, this.project, this.resultSelector, this.concurrent |
| 94 | + )); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * We need this JSDoc comment for affecting ESDoc. |
| 100 | + * @ignore |
| 101 | + * @extends {Ignored} |
| 102 | + */ |
| 103 | +export class MergeMapSubscriber<T, I, R> extends OuterSubscriber<T, I> { |
| 104 | + private hasCompleted: boolean = false; |
| 105 | + private buffer: T[] = []; |
| 106 | + private active: number = 0; |
| 107 | + protected index: number = 0; |
| 108 | + |
| 109 | + constructor(destination: Subscriber<I>, |
| 110 | + private project: (value: T, index: number) => ObservableInput<I>, |
| 111 | + private resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R, |
| 112 | + private concurrent: number = Number.POSITIVE_INFINITY) { |
| 113 | + super(destination); |
| 114 | + } |
| 115 | + |
| 116 | + protected _next(value: T): void { |
| 117 | + if (this.active < this.concurrent) { |
| 118 | + this._tryNext(value); |
| 119 | + } else { |
| 120 | + this.buffer.push(value); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + protected _tryNext(value: T) { |
| 125 | + let result: ObservableInput<I>; |
| 126 | + const index = this.index++; |
| 127 | + try { |
| 128 | + result = this.project(value, index); |
| 129 | + } catch (err) { |
| 130 | + this.destination.error(err); |
| 131 | + return; |
| 132 | + } |
| 133 | + this.active++; |
| 134 | + this._innerSub(result, value, index); |
| 135 | + } |
| 136 | + |
| 137 | + private _innerSub(ish: ObservableInput<I>, value: T, index: number): void { |
| 138 | + this.add(subscribeToResult<T, I>(this, ish, value, index)); |
| 139 | + } |
| 140 | + |
| 141 | + protected _complete(): void { |
| 142 | + this.hasCompleted = true; |
| 143 | + if (this.active === 0 && this.buffer.length === 0) { |
| 144 | + this.destination.complete(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + notifyNext(outerValue: T, innerValue: I, |
| 149 | + outerIndex: number, innerIndex: number, |
| 150 | + innerSub: InnerSubscriber<T, I>): void { |
| 151 | + if (this.resultSelector) { |
| 152 | + this._notifyResultSelector(outerValue, innerValue, outerIndex, innerIndex); |
| 153 | + } else { |
| 154 | + this.destination.next(innerValue); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private _notifyResultSelector(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) { |
| 159 | + let result: R; |
| 160 | + try { |
| 161 | + result = this.resultSelector(outerValue, innerValue, outerIndex, innerIndex); |
| 162 | + } catch (err) { |
| 163 | + this.destination.error(err); |
| 164 | + return; |
| 165 | + } |
| 166 | + this.destination.next(result); |
| 167 | + } |
| 168 | + |
| 169 | + notifyComplete(innerSub: Subscription): void { |
| 170 | + const buffer = this.buffer; |
| 171 | + this.remove(innerSub); |
| 172 | + this.active--; |
| 173 | + if (buffer.length > 0) { |
| 174 | + this._next(buffer.shift()); |
| 175 | + } else if (this.active === 0 && this.hasCompleted) { |
| 176 | + this.destination.complete(); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments