1 import { Operator } from '../Operator';
2 import { Observable } from '../Observable';
3 import { Subscriber } from '../Subscriber';
4 import { Subscription } from '../Subscription';
5 import { OuterSubscriber } from '../OuterSubscriber';
6 import { InnerSubscriber } from '../InnerSubscriber';
7 import { subscribeToResult } from '../util/subscribeToResult';
8 import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types';
9 import { map } from './map';
10 import { from } from '../observable/from';
12 /* tslint:disable:max-line-length */
13 export function exhaustMap<T, O extends ObservableInput<any>>(project: (value: T, index: number) => O): OperatorFunction<T, ObservedValueOf<O>>;
14 /** @deprecated resultSelector is no longer supported. Use inner map instead. */
15 export function exhaustMap<T, O extends ObservableInput<any>>(project: (value: T, index: number) => O, resultSelector: undefined): OperatorFunction<T, ObservedValueOf<O>>;
16 /** @deprecated resultSelector is no longer supported. Use inner map instead. */
17 export function exhaustMap<T, I, R>(project: (value: T, index: number) => ObservableInput<I>, resultSelector: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R): OperatorFunction<T, R>;
18 /* tslint:enable:max-line-length */
21 * Projects each source value to an Observable which is merged in the output
22 * Observable only if the previous projected Observable has completed.
24 * <span class="informal">Maps each value to an Observable, then flattens all of
25 * these inner Observables using {@link exhaust}.</span>
29 * Returns an Observable that emits items based on applying a function that you
30 * supply to each item emitted by the source Observable, where that function
31 * returns an (so-called "inner") Observable. When it projects a source value to
32 * an Observable, the output Observable begins emitting the items emitted by
33 * that projected Observable. However, `exhaustMap` ignores every new projected
34 * Observable if the previous projected Observable has not yet completed. Once
35 * that one completes, it will accept and flatten the next projected Observable
36 * and repeat this process.
39 * Run a finite timer for each click, only if there is no currently active timer
41 * import { fromEvent, interval } from 'rxjs';
42 * import { exhaustMap, take } from 'rxjs/operators';
44 * const clicks = fromEvent(document, 'click');
45 * const result = clicks.pipe(
46 * exhaustMap(ev => interval(1000).pipe(take(5)))
48 * result.subscribe(x => console.log(x));
51 * @see {@link concatMap}
52 * @see {@link exhaust}
53 * @see {@link mergeMap}
54 * @see {@link switchMap}
56 * @param {function(value: T, ?index: number): ObservableInput} project A function
57 * that, when applied to an item emitted by the source Observable, returns an
59 * @return {Observable} An Observable containing projected Observables
60 * of each item of the source, ignoring projected Observables that start before
61 * their preceding Observable has completed.
65 export function exhaustMap<T, R, O extends ObservableInput<any>>(
66 project: (value: T, index: number) => O,
67 resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R,
68 ): OperatorFunction<T, ObservedValueOf<O>|R> {
71 return (source: Observable<T>) => source.pipe(
72 exhaustMap((a, i) => from(project(a, i)).pipe(
73 map((b: any, ii: any) => resultSelector(a, b, i, ii)),
77 return (source: Observable<T>) =>
78 source.lift(new ExhaustMapOperator(project));
81 class ExhaustMapOperator<T, R> implements Operator<T, R> {
82 constructor(private project: (value: T, index: number) => ObservableInput<R>) {
85 call(subscriber: Subscriber<R>, source: any): any {
86 return source.subscribe(new ExhaustMapSubscriber(subscriber, this.project));
91 * We need this JSDoc comment for affecting ESDoc.
95 class ExhaustMapSubscriber<T, R> extends OuterSubscriber<T, R> {
96 private hasSubscription = false;
97 private hasCompleted = false;
100 constructor(destination: Subscriber<R>,
101 private project: (value: T, index: number) => ObservableInput<R>) {
105 protected _next(value: T): void {
106 if (!this.hasSubscription) {
111 private tryNext(value: T): void {
112 let result: ObservableInput<R>;
113 const index = this.index++;
115 result = this.project(value, index);
117 this.destination.error(err);
120 this.hasSubscription = true;
121 this._innerSub(result, value, index);
124 private _innerSub(result: ObservableInput<R>, value: T, index: number): void {
125 const innerSubscriber = new InnerSubscriber(this, value, index);
126 const destination = this.destination as Subscription;
127 destination.add(innerSubscriber);
128 const innerSubscription = subscribeToResult<T, R>(this, result, undefined, undefined, innerSubscriber);
129 // The returned subscription will usually be the subscriber that was
130 // passed. However, interop subscribers will be wrapped and for
131 // unsubscriptions to chain correctly, the wrapper needs to be added, too.
132 if (innerSubscription !== innerSubscriber) {
133 destination.add(innerSubscription);
137 protected _complete(): void {
138 this.hasCompleted = true;
139 if (!this.hasSubscription) {
140 this.destination.complete();
145 notifyNext(outerValue: T, innerValue: R,
146 outerIndex: number, innerIndex: number,
147 innerSub: InnerSubscriber<T, R>): void {
148 this.destination.next(innerValue);
151 notifyError(err: any): void {
152 this.destination.error(err);
155 notifyComplete(innerSub: Subscription): void {
156 const destination = this.destination as Subscription;
157 destination.remove(innerSub);
159 this.hasSubscription = false;
160 if (this.hasCompleted) {
161 this.destination.complete();