source: trip-planner-front/node_modules/rxjs/src/internal/operators/exhaustMap.ts

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.6 KB
RevLine 
[6a3a178]1import { Operator } from '../Operator';
2import { Observable } from '../Observable';
3import { Subscriber } from '../Subscriber';
4import { Subscription } from '../Subscription';
5import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types';
6import { map } from './map';
7import { from } from '../observable/from';
8import { SimpleOuterSubscriber, SimpleInnerSubscriber, innerSubscribe } from '../innerSubscribe';
9
10/* tslint:disable:max-line-length */
11export 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. */
13export 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. */
15export 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 */
63export 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
79class 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 */
93class 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}
Note: See TracBrowser for help on using the repository browser.