1c1e57f493617c5b2b17eec824336311b014fe75a995d25e53e5627e1d3181727d31b9d8e01ee48c46c65d33eacbc8a3ad035470c11803381af4a3cb371955 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { mergeMap } from './mergeMap';
  2. import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types';
  3. import { isFunction } from '../util/isFunction';
  4. /* tslint:disable:max-line-length */
  5. export function concatMap<T, O extends ObservableInput<any>>(
  6. project: (value: T, index: number) => O
  7. ): OperatorFunction<T, ObservedValueOf<O>>;
  8. /** @deprecated The `resultSelector` parameter will be removed in v8. Use an inner `map` instead. Details: https://rxjs.dev/deprecations/resultSelector */
  9. export function concatMap<T, O extends ObservableInput<any>>(
  10. project: (value: T, index: number) => O,
  11. resultSelector: undefined
  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 concatMap<T, R, O extends ObservableInput<any>>(
  15. project: (value: T, index: number) => O,
  16. resultSelector: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R
  17. ): OperatorFunction<T, R>;
  18. /* tslint:enable:max-line-length */
  19. /**
  20. * Projects each source value to an Observable which is merged in the output
  21. * Observable, in a serialized fashion waiting for each one to complete before
  22. * merging the next.
  23. *
  24. * <span class="informal">Maps each value to an Observable, then flattens all of
  25. * these inner Observables using {@link concatAll}.</span>
  26. *
  27. * ![](concatMap.png)
  28. *
  29. * Returns an Observable that emits items based on applying a function that you
  30. * supply to each item emitted by the source Observable, where that function
  31. * returns an (so-called "inner") Observable. Each new inner Observable is
  32. * concatenated with the previous inner Observable.
  33. *
  34. * __Warning:__ if source values arrive endlessly and faster than their
  35. * corresponding inner Observables can complete, it will result in memory issues
  36. * as inner Observables amass in an unbounded buffer waiting for their turn to
  37. * be subscribed to.
  38. *
  39. * Note: `concatMap` is equivalent to `mergeMap` with concurrency parameter set
  40. * to `1`.
  41. *
  42. * ## Example
  43. *
  44. * For each click event, tick every second from 0 to 3, with no concurrency
  45. *
  46. * ```ts
  47. * import { fromEvent, concatMap, interval, take } from 'rxjs';
  48. *
  49. * const clicks = fromEvent(document, 'click');
  50. * const result = clicks.pipe(
  51. * concatMap(ev => interval(1000).pipe(take(4)))
  52. * );
  53. * result.subscribe(x => console.log(x));
  54. *
  55. * // Results in the following:
  56. * // (results are not concurrent)
  57. * // For every click on the "document" it will emit values 0 to 3 spaced
  58. * // on a 1000ms interval
  59. * // one click = 1000ms-> 0 -1000ms-> 1 -1000ms-> 2 -1000ms-> 3
  60. * ```
  61. *
  62. * @see {@link concat}
  63. * @see {@link concatAll}
  64. * @see {@link concatMapTo}
  65. * @see {@link exhaustMap}
  66. * @see {@link mergeMap}
  67. * @see {@link switchMap}
  68. *
  69. * @param project A function that, when applied to an item emitted by the source
  70. * Observable, returns an Observable.
  71. * @return A function that returns an Observable that emits the result of
  72. * applying the projection function (and the optional deprecated
  73. * `resultSelector`) to each item emitted by the source Observable and taking
  74. * values from each projected inner Observable sequentially.
  75. */
  76. export function concatMap<T, R, O extends ObservableInput<any>>(
  77. project: (value: T, index: number) => O,
  78. resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R
  79. ): OperatorFunction<T, ObservedValueOf<O> | R> {
  80. return isFunction(resultSelector) ? mergeMap(project, resultSelector, 1) : mergeMap(project, 1);
  81. }