UNPKG

1.99 kBPlain TextView Raw
1
2import { Observable, ObservableInput } from 'rxjs';
3import { exhaustMap as higherOrder } from 'rxjs/operators';
4/**
5 * Projects each source value to an Observable which is merged in the output
6 * Observable only if the previous projected Observable has completed.
7 *
8 * <span class="informal">Maps each value to an Observable, then flattens all of
9 * these inner Observables using {@link exhaust}.</span>
10 *
11 * <img src="./img/exhaustMap.png" width="100%">
12 *
13 * Returns an Observable that emits items based on applying a function that you
14 * supply to each item emitted by the source Observable, where that function
15 * returns an (so-called "inner") Observable. When it projects a source value to
16 * an Observable, the output Observable begins emitting the items emitted by
17 * that projected Observable. However, `exhaustMap` ignores every new projected
18 * Observable if the previous projected Observable has not yet completed. Once
19 * that one completes, it will accept and flatten the next projected Observable
20 * and repeat this process.
21 *
22 * @example <caption>Run a finite timer for each click, only if there is no currently active timer</caption>
23 * var clicks = fromEvent(document, 'click');
24 * var result = clicks.pipe(exhaustMap((ev) => Rx.Observable.interval(1000).take(5)));
25 * result.subscribe(x => console.log(x));
26 *
27 * @see {@link concatMap}
28 * @see {@link exhaust}
29 * @see {@link mergeMap}
30 * @see {@link switchMap}
31 *
32 * @param {function(value: T, ?index: number): ObservableInput} project A function
33 * that, when applied to an item emitted by the source Observable, returns an
34 * Observable.
35 * @return {Observable} An Observable containing projected Observables
36 * of each item of the source, ignoring projected Observables that start before
37 * their preceding Observable has completed.
38 */
39export function exhaustMap<T, R>(
40 this: Observable<T>,
41 project: (value: T, index: number) => ObservableInput<R>
42): Observable<R> {
43 return higherOrder(project)(this);
44}