b4866bcf266e09671acae2ad722c7d9bc6e0747d4a93c1b1ac307af21a1ca3734196349cce28396ebbad0121ad94150894b3573656d9b06e004c95911b0536 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { concatMap } from './concatMap';
  2. import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types';
  3. import { isFunction } from '../util/isFunction';
  4. /** @deprecated Will be removed in v9. Use {@link concatMap} instead: `concatMap(() => result)` */
  5. export function concatMapTo<O extends ObservableInput<unknown>>(observable: O): OperatorFunction<unknown, ObservedValueOf<O>>;
  6. /** @deprecated The `resultSelector` parameter will be removed in v8. Use an inner `map` instead. Details: https://rxjs.dev/deprecations/resultSelector */
  7. export function concatMapTo<O extends ObservableInput<unknown>>(
  8. observable: O,
  9. resultSelector: undefined
  10. ): OperatorFunction<unknown, ObservedValueOf<O>>;
  11. /** @deprecated The `resultSelector` parameter will be removed in v8. Use an inner `map` instead. Details: https://rxjs.dev/deprecations/resultSelector */
  12. export function concatMapTo<T, R, O extends ObservableInput<unknown>>(
  13. observable: O,
  14. resultSelector: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R
  15. ): OperatorFunction<T, R>;
  16. /**
  17. * Projects each source value to the same Observable which is merged multiple
  18. * times in a serialized fashion on the output Observable.
  19. *
  20. * <span class="informal">It's like {@link concatMap}, but maps each value
  21. * always to the same inner Observable.</span>
  22. *
  23. * ![](concatMapTo.png)
  24. *
  25. * Maps each source value to the given Observable `innerObservable` regardless
  26. * of the source value, and then flattens those resulting Observables into one
  27. * single Observable, which is the output Observable. Each new `innerObservable`
  28. * instance emitted on the output Observable is concatenated with the previous
  29. * `innerObservable` instance.
  30. *
  31. * __Warning:__ if source values arrive endlessly and faster than their
  32. * corresponding inner Observables can complete, it will result in memory issues
  33. * as inner Observables amass in an unbounded buffer waiting for their turn to
  34. * be subscribed to.
  35. *
  36. * Note: `concatMapTo` is equivalent to `mergeMapTo` with concurrency parameter
  37. * set to `1`.
  38. *
  39. * ## Example
  40. *
  41. * For each click event, tick every second from 0 to 3, with no concurrency
  42. *
  43. * ```ts
  44. * import { fromEvent, concatMapTo, interval, take } from 'rxjs';
  45. *
  46. * const clicks = fromEvent(document, 'click');
  47. * const result = clicks.pipe(
  48. * concatMapTo(interval(1000).pipe(take(4)))
  49. * );
  50. * result.subscribe(x => console.log(x));
  51. *
  52. * // Results in the following:
  53. * // (results are not concurrent)
  54. * // For every click on the "document" it will emit values 0 to 3 spaced
  55. * // on a 1000ms interval
  56. * // one click = 1000ms-> 0 -1000ms-> 1 -1000ms-> 2 -1000ms-> 3
  57. * ```
  58. *
  59. * @see {@link concat}
  60. * @see {@link concatAll}
  61. * @see {@link concatMap}
  62. * @see {@link mergeMapTo}
  63. * @see {@link switchMapTo}
  64. *
  65. * @param innerObservable An `ObservableInput` to replace each value from the
  66. * source Observable.
  67. * @return A function that returns an Observable of values merged together by
  68. * joining the passed Observable with itself, one after the other, for each
  69. * value emitted from the source.
  70. * @deprecated Will be removed in v9. Use {@link concatMap} instead: `concatMap(() => result)`
  71. */
  72. export function concatMapTo<T, R, O extends ObservableInput<unknown>>(
  73. innerObservable: O,
  74. resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R
  75. ): OperatorFunction<T, ObservedValueOf<O> | R> {
  76. return isFunction(resultSelector) ? concatMap(() => innerObservable, resultSelector) : concatMap(() => innerObservable);
  77. }