[6a3a178] | 1 | import { Operator } from '../Operator';
|
---|
| 2 | import { Observable } from '../Observable';
|
---|
| 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';
|
---|
| 9 |
|
---|
| 10 | /* tslint:disable:max-line-length */
|
---|
| 11 | export function exhaustMap<T, O extends ObservableInput<any>>(project: (value: T, index: number) => O): OperatorFunction<T, ObservedValueOf<O>>;
|
---|
| 12 | /** @deprecated resultSelector is no longer supported. Use inner map instead. */
|
---|
| 13 | export function exhaustMap<T, O extends ObservableInput<any>>(project: (value: T, index: number) => O, resultSelector: undefined): OperatorFunction<T, ObservedValueOf<O>>;
|
---|
| 14 | /** @deprecated resultSelector is no longer supported. Use inner map instead. */
|
---|
| 15 | 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>;
|
---|
| 16 | /* tslint:enable:max-line-length */
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * Projects each source value to an Observable which is merged in the output
|
---|
| 20 | * Observable only if the previous projected Observable has completed.
|
---|
| 21 | *
|
---|
| 22 | * <span class="informal">Maps each value to an Observable, then flattens all of
|
---|
| 23 | * these inner Observables using {@link exhaust}.</span>
|
---|
| 24 | *
|
---|
| 25 | * ![](exhaustMap.png)
|
---|
| 26 | *
|
---|
| 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 (so-called "inner") Observable. When it projects a source value to
|
---|
| 30 | * an Observable, the output Observable begins emitting the items emitted by
|
---|
| 31 | * that projected Observable. However, `exhaustMap` ignores every new projected
|
---|
| 32 | * Observable if the previous projected Observable has not yet completed. Once
|
---|
| 33 | * that one completes, it will accept and flatten the next projected Observable
|
---|
| 34 | * and repeat this process.
|
---|
| 35 | *
|
---|
| 36 | * ## Example
|
---|
| 37 | * Run a finite timer for each click, only if there is no currently active timer
|
---|
| 38 | * ```ts
|
---|
| 39 | * import { fromEvent, interval } from 'rxjs';
|
---|
| 40 | * import { exhaustMap, take } from 'rxjs/operators';
|
---|
| 41 | *
|
---|
| 42 | * const clicks = fromEvent(document, 'click');
|
---|
| 43 | * const result = clicks.pipe(
|
---|
| 44 | * exhaustMap(ev => interval(1000).pipe(take(5)))
|
---|
| 45 | * );
|
---|
| 46 | * result.subscribe(x => console.log(x));
|
---|
| 47 | * ```
|
---|
| 48 | *
|
---|
| 49 | * @see {@link concatMap}
|
---|
| 50 | * @see {@link exhaust}
|
---|
| 51 | * @see {@link mergeMap}
|
---|
| 52 | * @see {@link switchMap}
|
---|
| 53 | *
|
---|
| 54 | * @param {function(value: T, ?index: number): ObservableInput} project A function
|
---|
| 55 | * that, when applied to an item emitted by the source Observable, returns an
|
---|
| 56 | * Observable.
|
---|
| 57 | * @return {Observable} An Observable containing projected Observables
|
---|
| 58 | * of each item of the source, ignoring projected Observables that start before
|
---|
| 59 | * their preceding Observable has completed.
|
---|
| 60 | * @method exhaustMap
|
---|
| 61 | * @owner Observable
|
---|
| 62 | */
|
---|
| 63 | export function exhaustMap<T, R, O extends ObservableInput<any>>(
|
---|
| 64 | project: (value: T, index: number) => O,
|
---|
| 65 | resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R,
|
---|
| 66 | ): OperatorFunction<T, ObservedValueOf<O>|R> {
|
---|
| 67 | if (resultSelector) {
|
---|
| 68 | // DEPRECATED PATH
|
---|
| 69 | return (source: Observable<T>) => source.pipe(
|
---|
| 70 | exhaustMap((a, i) => from(project(a, i)).pipe(
|
---|
| 71 | map((b: any, ii: any) => resultSelector(a, b, i, ii)),
|
---|
| 72 | )),
|
---|
| 73 | );
|
---|
| 74 | }
|
---|
| 75 | return (source: Observable<T>) =>
|
---|
| 76 | source.lift(new ExhaustMapOperator(project));
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | class ExhaustMapOperator<T, R> implements Operator<T, R> {
|
---|
| 80 | constructor(private project: (value: T, index: number) => ObservableInput<R>) {
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | call(subscriber: Subscriber<R>, source: any): any {
|
---|
| 84 | return source.subscribe(new ExhaustMapSubscriber(subscriber, this.project));
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * We need this JSDoc comment for affecting ESDoc.
|
---|
| 90 | * @ignore
|
---|
| 91 | * @extends {Ignored}
|
---|
| 92 | */
|
---|
| 93 | class ExhaustMapSubscriber<T, R> extends SimpleOuterSubscriber<T, R> {
|
---|
| 94 | private hasSubscription = false;
|
---|
| 95 | private hasCompleted = false;
|
---|
| 96 | private index = 0;
|
---|
| 97 |
|
---|
| 98 | constructor(destination: Subscriber<R>,
|
---|
| 99 | private project: (value: T, index: number) => ObservableInput<R>) {
|
---|
| 100 | super(destination);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | protected _next(value: T): void {
|
---|
| 104 | if (!this.hasSubscription) {
|
---|
| 105 | this.tryNext(value);
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | private tryNext(value: T): void {
|
---|
| 110 | let result: ObservableInput<R>;
|
---|
| 111 | const index = this.index++;
|
---|
| 112 | try {
|
---|
| 113 | result = this.project(value, index);
|
---|
| 114 | } catch (err) {
|
---|
| 115 | this.destination.error!(err);
|
---|
| 116 | return;
|
---|
| 117 | }
|
---|
| 118 | this.hasSubscription = true;
|
---|
| 119 | this._innerSub(result);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | private _innerSub(result: ObservableInput<R>): void {
|
---|
| 123 | const innerSubscriber = new SimpleInnerSubscriber(this);
|
---|
| 124 | const destination = this.destination as Subscription;
|
---|
| 125 | destination.add(innerSubscriber);
|
---|
| 126 | const innerSubscription = innerSubscribe(result, innerSubscriber);
|
---|
| 127 | // The returned subscription will usually be the subscriber that was
|
---|
| 128 | // passed. However, interop subscribers will be wrapped and for
|
---|
| 129 | // unsubscriptions to chain correctly, the wrapper needs to be added, too.
|
---|
| 130 | if (innerSubscription !== innerSubscriber) {
|
---|
| 131 | destination.add(innerSubscription);
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | protected _complete(): void {
|
---|
| 136 | this.hasCompleted = true;
|
---|
| 137 | if (!this.hasSubscription) {
|
---|
| 138 | this.destination.complete!();
|
---|
| 139 | }
|
---|
| 140 | this.unsubscribe();
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | notifyNext(innerValue: R): void {
|
---|
| 144 | this.destination.next!(innerValue);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | notifyError(err: any): void {
|
---|
| 148 | this.destination.error!(err);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | notifyComplete(): void {
|
---|
| 152 | this.hasSubscription = false;
|
---|
| 153 | if (this.hasCompleted) {
|
---|
| 154 | this.destination.complete!();
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 | }
|
---|