ea0c85f311e852d4d6f646b1770fa9369b2325104f08810b277c8b58c33f4e1c442cb4410abd621175271ae00807c95cf898dd79e6143d00cd250b0dc1fd4b 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { Observable } from '../Observable';
  2. import { MonoTypeOperatorFunction, ObservableInput } from '../types';
  3. import { concat } from '../observable/concat';
  4. import { take } from './take';
  5. import { ignoreElements } from './ignoreElements';
  6. import { mapTo } from './mapTo';
  7. import { mergeMap } from './mergeMap';
  8. import { innerFrom } from '../observable/innerFrom';
  9. /** @deprecated The `subscriptionDelay` parameter will be removed in v8. */
  10. export function delayWhen<T>(
  11. delayDurationSelector: (value: T, index: number) => ObservableInput<any>,
  12. subscriptionDelay: Observable<any>
  13. ): MonoTypeOperatorFunction<T>;
  14. export function delayWhen<T>(delayDurationSelector: (value: T, index: number) => ObservableInput<any>): MonoTypeOperatorFunction<T>;
  15. /**
  16. * Delays the emission of items from the source Observable by a given time span
  17. * determined by the emissions of another Observable.
  18. *
  19. * <span class="informal">It's like {@link delay}, but the time span of the
  20. * delay duration is determined by a second Observable.</span>
  21. *
  22. * ![](delayWhen.png)
  23. *
  24. * `delayWhen` operator shifts each emitted value from the source Observable by
  25. * a time span determined by another Observable. When the source emits a value,
  26. * the `delayDurationSelector` function is called with the value emitted from
  27. * the source Observable as the first argument to the `delayDurationSelector`.
  28. * The `delayDurationSelector` function should return an {@link ObservableInput},
  29. * that is internally converted to an Observable that is called the "duration"
  30. * Observable.
  31. *
  32. * The source value is emitted on the output Observable only when the "duration"
  33. * Observable emits ({@link guide/glossary-and-semantics#next next}s) any value.
  34. * Upon that, the "duration" Observable gets unsubscribed.
  35. *
  36. * Before RxJS V7, the {@link guide/glossary-and-semantics#complete completion}
  37. * of the "duration" Observable would have been triggering the emission of the
  38. * source value to the output Observable, but with RxJS V7, this is not the case
  39. * anymore.
  40. *
  41. * Only next notifications (from the "duration" Observable) trigger values from
  42. * the source Observable to be passed to the output Observable. If the "duration"
  43. * Observable only emits the complete notification (without next), the value
  44. * emitted by the source Observable will never get to the output Observable - it
  45. * will be swallowed. If the "duration" Observable errors, the error will be
  46. * propagated to the output Observable.
  47. *
  48. * Optionally, `delayWhen` takes a second argument, `subscriptionDelay`, which
  49. * is an Observable. When `subscriptionDelay` emits its first value or
  50. * completes, the source Observable is subscribed to and starts behaving like
  51. * described in the previous paragraph. If `subscriptionDelay` is not provided,
  52. * `delayWhen` will subscribe to the source Observable as soon as the output
  53. * Observable is subscribed.
  54. *
  55. * ## Example
  56. *
  57. * Delay each click by a random amount of time, between 0 and 5 seconds
  58. *
  59. * ```ts
  60. * import { fromEvent, delayWhen, interval } from 'rxjs';
  61. *
  62. * const clicks = fromEvent(document, 'click');
  63. * const delayedClicks = clicks.pipe(
  64. * delayWhen(() => interval(Math.random() * 5000))
  65. * );
  66. * delayedClicks.subscribe(x => console.log(x));
  67. * ```
  68. *
  69. * @see {@link delay}
  70. * @see {@link throttle}
  71. * @see {@link throttleTime}
  72. * @see {@link debounce}
  73. * @see {@link debounceTime}
  74. * @see {@link sample}
  75. * @see {@link sampleTime}
  76. * @see {@link audit}
  77. * @see {@link auditTime}
  78. *
  79. * @param delayDurationSelector A function that returns an `ObservableInput` for
  80. * each `value` emitted by the source Observable, which is then used to delay the
  81. * emission of that `value` on the output Observable until the `ObservableInput`
  82. * returned from this function emits a next value. When called, beside `value`,
  83. * this function receives a zero-based `index` of the emission order.
  84. * @param subscriptionDelay An Observable that triggers the subscription to the
  85. * source Observable once it emits any value.
  86. * @return A function that returns an Observable that delays the emissions of
  87. * the source Observable by an amount of time specified by the Observable
  88. * returned by `delayDurationSelector`.
  89. */
  90. export function delayWhen<T>(
  91. delayDurationSelector: (value: T, index: number) => ObservableInput<any>,
  92. subscriptionDelay?: Observable<any>
  93. ): MonoTypeOperatorFunction<T> {
  94. if (subscriptionDelay) {
  95. // DEPRECATED PATH
  96. return (source: Observable<T>) =>
  97. concat(subscriptionDelay.pipe(take(1), ignoreElements()), source.pipe(delayWhen(delayDurationSelector)));
  98. }
  99. return mergeMap((value, index) => innerFrom(delayDurationSelector(value, index)).pipe(take(1), mapTo(value)));
  100. }