| 1 |
import { Observable } from '../Observable'; |
| 2 |
import { Subscriber } from '../Subscriber'; |
| 3 |
import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types'; |
| 4 |
import { map } from './map'; |
| 5 |
import { innerFrom } from '../observable/innerFrom'; |
| 6 |
import { operate } from '../util/lift'; |
| 7 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 8 |
|
| 9 |
/* tslint:disable:max-line-length */ |
| 10 |
export function exhaustMap<T, O extends ObservableInput<any>>( |
| 11 |
project: (value: T, index: number) => O |
| 12 |
): OperatorFunction<T, ObservedValueOf<O>>; |
| 13 |
/** @deprecated The `resultSelector` parameter will be removed in v8. Use an inner `map` instead. Details: https://rxjs.dev/deprecations/resultSelector */ |
| 14 |
export function exhaustMap<T, O extends ObservableInput<any>>( |
| 15 |
project: (value: T, index: number) => O, |
| 16 |
resultSelector: undefined |
| 17 |
): OperatorFunction<T, ObservedValueOf<O>>; |
| 18 |
/** @deprecated The `resultSelector` parameter will be removed in v8. Use an inner `map` instead. Details: https://rxjs.dev/deprecations/resultSelector */ |
| 19 |
export function exhaustMap<T, I, R>( |
| 20 |
project: (value: T, index: number) => ObservableInput<I>, |
| 21 |
resultSelector: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R |
| 22 |
): OperatorFunction<T, R>; |
| 23 |
/* tslint:enable:max-line-length */ |
| 24 |
|
| 25 |
/** |
| 26 |
* Projects each source value to an Observable which is merged in the output |
| 27 |
* Observable only if the previous projected Observable has completed. |
| 28 |
* |
| 29 |
* <span class="informal">Maps each value to an Observable, then flattens all of |
| 30 |
* these inner Observables using {@link exhaustAll}.</span> |
| 31 |
* |
| 32 |
*  |
| 33 |
* |
| 34 |
* Returns an Observable that emits items based on applying a function that you |
| 35 |
* supply to each item emitted by the source Observable, where that function |
| 36 |
* returns an (so-called "inner") Observable. When it projects a source value to |
| 37 |
* an Observable, the output Observable begins emitting the items emitted by |
| 38 |
* that projected Observable. However, `exhaustMap` ignores every new projected |
| 39 |
* Observable if the previous projected Observable has not yet completed. Once |
| 40 |
* that one completes, it will accept and flatten the next projected Observable |
| 41 |
* and repeat this process. |
| 42 |
* |
| 43 |
* ## Example |
| 44 |
* |
| 45 |
* Run a finite timer for each click, only if there is no currently active timer |
| 46 |
* |
| 47 |
* ```ts |
| 48 |
* import { fromEvent, exhaustMap, interval, take } from 'rxjs'; |
| 49 |
* |
| 50 |
* const clicks = fromEvent(document, 'click'); |
| 51 |
* const result = clicks.pipe( |
| 52 |
* exhaustMap(() => interval(1000).pipe(take(5))) |
| 53 |
* ); |
| 54 |
* result.subscribe(x => console.log(x)); |
| 55 |
* ``` |
| 56 |
* |
| 57 |
* @see {@link concatMap} |
| 58 |
* @see {@link exhaust} |
| 59 |
* @see {@link mergeMap} |
| 60 |
* @see {@link switchMap} |
| 61 |
* |
| 62 |
* @param project A function that, when applied to an item emitted by the source |
| 63 |
* Observable, returns an Observable. |
| 64 |
* @return A function that returns an Observable containing projected |
| 65 |
* Observables of each item of the source, ignoring projected Observables that |
| 66 |
* start before their preceding Observable has completed. |
| 67 |
*/ |
| 68 |
export function exhaustMap<T, R, O extends ObservableInput<any>>( |
| 69 |
project: (value: T, index: number) => O, |
| 70 |
resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R |
| 71 |
): OperatorFunction<T, ObservedValueOf<O> | R> { |
| 72 |
if (resultSelector) { |
| 73 |
// DEPRECATED PATH |
| 74 |
return (source: Observable<T>) => |
| 75 |
source.pipe(exhaustMap((a, i) => innerFrom(project(a, i)).pipe(map((b: any, ii: any) => resultSelector(a, b, i, ii))))); |
| 76 |
} |
| 77 |
return operate((source, subscriber) => { |
| 78 |
let index = 0; |
| 79 |
let innerSub: Subscriber<T> | null = null; |
| 80 |
let isComplete = false; |
| 81 |
source.subscribe( |
| 82 |
createOperatorSubscriber( |
| 83 |
subscriber, |
| 84 |
(outerValue) => { |
| 85 |
if (!innerSub) { |
| 86 |
innerSub = createOperatorSubscriber(subscriber, undefined, () => { |
| 87 |
innerSub = null; |
| 88 |
isComplete && subscriber.complete(); |
| 89 |
}); |
| 90 |
innerFrom(project(outerValue, index++)).subscribe(innerSub); |
| 91 |
} |
| 92 |
}, |
| 93 |
() => { |
| 94 |
isComplete = true; |
| 95 |
!innerSub && subscriber.complete(); |
| 96 |
} |
| 97 |
) |
| 98 |
); |
| 99 |
}); |
| 100 |
} |