Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

exhaustMap.ts 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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';
  11. /* tslint:disable:max-line-length */
  12. export function exhaustMap<T, O extends ObservableInput<any>>(project: (value: T, index: number) => O): OperatorFunction<T, ObservedValueOf<O>>;
  13. /** @deprecated resultSelector is no longer supported. Use inner map instead. */
  14. export function exhaustMap<T, O extends ObservableInput<any>>(project: (value: T, index: number) => O, resultSelector: undefined): OperatorFunction<T, ObservedValueOf<O>>;
  15. /** @deprecated resultSelector is no longer supported. Use inner map instead. */
  16. 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>;
  17. /* tslint:enable:max-line-length */
  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. class ExhaustMapOperator<T, R> implements Operator<T, R> {
  79. constructor(private project: (value: T, index: number) => ObservableInput<R>) {
  80. }
  81. call(subscriber: Subscriber<R>, source: any): any {
  82. return source.subscribe(new ExhaustMapSubscriber(subscriber, this.project));
  83. }
  84. }
  85. /**
  86. * We need this JSDoc comment for affecting ESDoc.
  87. * @ignore
  88. * @extends {Ignored}
  89. */
  90. class ExhaustMapSubscriber<T, R> extends OuterSubscriber<T, R> {
  91. private hasSubscription = false;
  92. private hasCompleted = false;
  93. private index = 0;
  94. constructor(destination: Subscriber<R>,
  95. private project: (value: T, index: number) => ObservableInput<R>) {
  96. super(destination);
  97. }
  98. protected _next(value: T): void {
  99. if (!this.hasSubscription) {
  100. this.tryNext(value);
  101. }
  102. }
  103. private tryNext(value: T): void {
  104. let result: ObservableInput<R>;
  105. const index = this.index++;
  106. try {
  107. result = this.project(value, index);
  108. } catch (err) {
  109. this.destination.error(err);
  110. return;
  111. }
  112. this.hasSubscription = true;
  113. this._innerSub(result, value, index);
  114. }
  115. private _innerSub(result: ObservableInput<R>, value: T, index: number): void {
  116. const innerSubscriber = new InnerSubscriber(this, undefined, undefined);
  117. const destination = this.destination as Subscription;
  118. destination.add(innerSubscriber);
  119. subscribeToResult<T, R>(this, result, value, index, innerSubscriber);
  120. }
  121. protected _complete(): void {
  122. this.hasCompleted = true;
  123. if (!this.hasSubscription) {
  124. this.destination.complete();
  125. }
  126. this.unsubscribe();
  127. }
  128. notifyNext(outerValue: T, innerValue: R,
  129. outerIndex: number, innerIndex: number,
  130. innerSub: InnerSubscriber<T, R>): void {
  131. this.destination.next(innerValue);
  132. }
  133. notifyError(err: any): void {
  134. this.destination.error(err);
  135. }
  136. notifyComplete(innerSub: Subscription): void {
  137. const destination = this.destination as Subscription;
  138. destination.remove(innerSub);
  139. this.hasSubscription = false;
  140. if (this.hasCompleted) {
  141. this.destination.complete();
  142. }
  143. }
  144. }