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