1 import { Observable } from '../Observable';
2 import { Operator } from '../Operator';
3 import { Subscriber } from '../Subscriber';
4 import { Subscription } from '../Subscription';
5 import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types';
6 import { map } from './map';
7 import { from } from '../observable/from';
8 import { SimpleOuterSubscriber, SimpleInnerSubscriber, innerSubscribe } from '../innerSubscribe';
10 /* tslint:disable:max-line-length */
11 export function mergeMap<T, O extends ObservableInput<any>>(project: (value: T, index: number) => O, concurrent?: number): OperatorFunction<T, ObservedValueOf<O>>;
12 /** @deprecated resultSelector no longer supported, use inner map instead */
13 export function mergeMap<T, O extends ObservableInput<any>>(project: (value: T, index: number) => O, resultSelector: undefined, concurrent?: number): OperatorFunction<T, ObservedValueOf<O>>;
14 /** @deprecated resultSelector no longer supported, use inner map instead */
15 export function mergeMap<T, R, O extends ObservableInput<any>>(project: (value: T, index: number) => O, resultSelector: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R, concurrent?: number): OperatorFunction<T, R>;
16 /* tslint:enable:max-line-length */
19 * Projects each source value to an Observable which is merged in the output
22 * <span class="informal">Maps each value to an Observable, then flattens all of
23 * these inner Observables using {@link mergeAll}.</span>
27 * Returns an Observable that emits items based on applying a function that you
28 * supply to each item emitted by the source Observable, where that function
29 * returns an Observable, and then merging those resulting Observables and
30 * emitting the results of this merger.
33 * Map and flatten each letter to an Observable ticking every 1 second
35 * import { of, interval } from 'rxjs';
36 * import { mergeMap, map } from 'rxjs/operators';
38 * const letters = of('a', 'b', 'c');
39 * const result = letters.pipe(
40 * mergeMap(x => interval(1000).pipe(map(i => x+i))),
42 * result.subscribe(x => console.log(x));
44 * // Results in the following:
51 * // continues to list a,b,c with respective ascending integers
54 * @see {@link concatMap}
55 * @see {@link exhaustMap}
57 * @see {@link mergeAll}
58 * @see {@link mergeMapTo}
59 * @see {@link mergeScan}
60 * @see {@link switchMap}
62 * @param {function(value: T, ?index: number): ObservableInput} project A function
63 * that, when applied to an item emitted by the source Observable, returns an
65 * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of input
66 * Observables being subscribed to concurrently.
67 * @return {Observable} An Observable that emits the result of applying the
68 * projection function (and the optional deprecated `resultSelector`) to each item
69 * emitted by the source Observable and merging the results of the Observables
70 * obtained from this transformation.
72 export function mergeMap<T, R, O extends ObservableInput<any>>(
73 project: (value: T, index: number) => O,
74 resultSelector?: ((outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R) | number,
75 concurrent: number = Number.POSITIVE_INFINITY
76 ): OperatorFunction<T, ObservedValueOf<O>|R> {
77 if (typeof resultSelector === 'function') {
79 return (source: Observable<T>) => source.pipe(
80 mergeMap((a, i) => from(project(a, i)).pipe(
81 map((b: any, ii: number) => resultSelector(a, b, i, ii)),
84 } else if (typeof resultSelector === 'number') {
85 concurrent = resultSelector;
87 return (source: Observable<T>) => source.lift(new MergeMapOperator(project, concurrent));
90 export class MergeMapOperator<T, R> implements Operator<T, R> {
91 constructor(private project: (value: T, index: number) => ObservableInput<R>,
92 private concurrent: number = Number.POSITIVE_INFINITY) {
95 call(observer: Subscriber<R>, source: any): any {
96 return source.subscribe(new MergeMapSubscriber(
97 observer, this.project, this.concurrent
103 * We need this JSDoc comment for affecting ESDoc.
107 export class MergeMapSubscriber<T, R> extends SimpleOuterSubscriber<T, R> {
108 private hasCompleted: boolean = false;
109 private buffer: T[] = [];
110 private active: number = 0;
111 protected index: number = 0;
113 constructor(destination: Subscriber<R>,
114 private project: (value: T, index: number) => ObservableInput<R>,
115 private concurrent: number = Number.POSITIVE_INFINITY) {
119 protected _next(value: T): void {
120 if (this.active < this.concurrent) {
121 this._tryNext(value);
123 this.buffer.push(value);
127 protected _tryNext(value: T) {
128 let result: ObservableInput<R>;
129 const index = this.index++;
131 result = this.project(value, index);
133 this.destination.error!(err);
137 this._innerSub(result);
140 private _innerSub(ish: ObservableInput<R>): void {
141 const innerSubscriber = new SimpleInnerSubscriber(this);
142 const destination = this.destination as Subscription;
143 destination.add(innerSubscriber);
144 const innerSubscription = innerSubscribe(ish, innerSubscriber);
145 // The returned subscription will usually be the subscriber that was
146 // passed. However, interop subscribers will be wrapped and for
147 // unsubscriptions to chain correctly, the wrapper needs to be added, too.
148 if (innerSubscription !== innerSubscriber) {
149 destination.add(innerSubscription);
153 protected _complete(): void {
154 this.hasCompleted = true;
155 if (this.active === 0 && this.buffer.length === 0) {
156 this.destination.complete!();
161 notifyNext(innerValue: R): void {
162 this.destination.next!(innerValue);
165 notifyComplete(): void {
166 const buffer = this.buffer;
168 if (buffer.length > 0) {
169 this._next(buffer.shift()!);
170 } else if (this.active === 0 && this.hasCompleted) {
171 this.destination.complete!();
177 * @deprecated renamed. Use {@link mergeMap}
179 export const flatMap = mergeMap;