a57b4496dc554bd9cc02492a81bf811c1116e1843234a60a7e45c5be6cc24a7d70d1751eff09f637a67f4d551b9dc21c7c41b66ec7bc9a9ca399dc8d618ffb 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { OperatorFunction, ObservableInput, ObservedValueOf, SchedulerLike } from '../types';
  2. import { operate } from '../util/lift';
  3. import { mergeInternals } from './mergeInternals';
  4. /* tslint:disable:max-line-length */
  5. export function expand<T, O extends ObservableInput<unknown>>(
  6. project: (value: T, index: number) => O,
  7. concurrent?: number,
  8. scheduler?: SchedulerLike
  9. ): OperatorFunction<T, ObservedValueOf<O>>;
  10. /**
  11. * @deprecated The `scheduler` parameter will be removed in v8. If you need to schedule the inner subscription,
  12. * use `subscribeOn` within the projection function: `expand((value) => fn(value).pipe(subscribeOn(scheduler)))`.
  13. * Details: Details: https://rxjs.dev/deprecations/scheduler-argument
  14. */
  15. export function expand<T, O extends ObservableInput<unknown>>(
  16. project: (value: T, index: number) => O,
  17. concurrent: number | undefined,
  18. scheduler: SchedulerLike
  19. ): OperatorFunction<T, ObservedValueOf<O>>;
  20. /* tslint:enable:max-line-length */
  21. /**
  22. * Recursively projects each source value to an Observable which is merged in
  23. * the output Observable.
  24. *
  25. * <span class="informal">It's similar to {@link mergeMap}, but applies the
  26. * projection function to every source value as well as every output value.
  27. * It's recursive.</span>
  28. *
  29. * ![](expand.png)
  30. *
  31. * Returns an Observable that emits items based on applying a function that you
  32. * supply to each item emitted by the source Observable, where that function
  33. * returns an Observable, and then merging those resulting Observables and
  34. * emitting the results of this merger. *Expand* will re-emit on the output
  35. * Observable every source value. Then, each output value is given to the
  36. * `project` function which returns an inner Observable to be merged on the
  37. * output Observable. Those output values resulting from the projection are also
  38. * given to the `project` function to produce new output values. This is how
  39. * *expand* behaves recursively.
  40. *
  41. * ## Example
  42. *
  43. * Start emitting the powers of two on every click, at most 10 of them
  44. *
  45. * ```ts
  46. * import { fromEvent, map, expand, of, delay, take } from 'rxjs';
  47. *
  48. * const clicks = fromEvent(document, 'click');
  49. * const powersOfTwo = clicks.pipe(
  50. * map(() => 1),
  51. * expand(x => of(2 * x).pipe(delay(1000))),
  52. * take(10)
  53. * );
  54. * powersOfTwo.subscribe(x => console.log(x));
  55. * ```
  56. *
  57. * @see {@link mergeMap}
  58. * @see {@link mergeScan}
  59. *
  60. * @param project A function that, when applied to an item emitted by the source
  61. * or the output Observable, returns an Observable.
  62. * @param concurrent Maximum number of input Observables being subscribed to
  63. * concurrently.
  64. * @param scheduler The {@link SchedulerLike} to use for subscribing to
  65. * each projected inner Observable.
  66. * @return A function that returns an Observable that emits the source values
  67. * and also result of applying the projection function to each value emitted on
  68. * the output Observable and merging the results of the Observables obtained
  69. * from this transformation.
  70. */
  71. export function expand<T, O extends ObservableInput<unknown>>(
  72. project: (value: T, index: number) => O,
  73. concurrent = Infinity,
  74. scheduler?: SchedulerLike
  75. ): OperatorFunction<T, ObservedValueOf<O>> {
  76. concurrent = (concurrent || 0) < 1 ? Infinity : concurrent;
  77. return operate((source, subscriber) =>
  78. mergeInternals(
  79. // General merge params
  80. source,
  81. subscriber,
  82. project,
  83. concurrent,
  84. // onBeforeNext
  85. undefined,
  86. // Expand-specific
  87. true, // Use expand path
  88. scheduler // Inner subscription scheduler
  89. )
  90. );
  91. }