Debian Codegraph @

node/rxjs/src/internal/operators/delay.ts

raw
1 import { asyncScheduler } from '../scheduler/async';
2 import { MonoTypeOperatorFunction, SchedulerLike } from '../types';
3 import { delayWhen } from './delayWhen';
4 import { timer } from '../observable/timer';
5
6 /**
7 * Delays the emission of items from the source Observable by a given timeout or
8 * until a given Date.
9 *
10 * <span class="informal">Time shifts each item by some specified amount of
11 * milliseconds.</span>
12 *
13 * ![](delay.svg)
14 *
15 * If the delay argument is a Number, this operator time shifts the source
16 * Observable by that amount of time expressed in milliseconds. The relative
17 * time intervals between the values are preserved.
18 *
19 * If the delay argument is a Date, this operator time shifts the start of the
20 * Observable execution until the given date occurs.
21 *
22 * ## Examples
23 *
24 * Delay each click by one second
25 *
26 * ```ts
27 * import { fromEvent, delay } from 'rxjs';
28 *
29 * const clicks = fromEvent(document, 'click');
30 * const delayedClicks = clicks.pipe(delay(1000)); // each click emitted after 1 second
31 * delayedClicks.subscribe(x => console.log(x));
32 * ```
33 *
34 * Delay all clicks until a future date happens
35 *
36 * ```ts
37 * import { fromEvent, delay } from 'rxjs';
38 *
39 * const clicks = fromEvent(document, 'click');
40 * const date = new Date('March 15, 2050 12:00:00'); // in the future
41 * const delayedClicks = clicks.pipe(delay(date)); // click emitted only after that date
42 * delayedClicks.subscribe(x => console.log(x));
43 * ```
44 *
45 * @see {@link delayWhen}
46 * @see {@link throttle}
47 * @see {@link throttleTime}
48 * @see {@link debounce}
49 * @see {@link debounceTime}
50 * @see {@link sample}
51 * @see {@link sampleTime}
52 * @see {@link audit}
53 * @see {@link auditTime}
54 *
55 * @param due The delay duration in milliseconds (a `number`) or a `Date` until
56 * which the emission of the source items is delayed.
57 * @param scheduler The {@link SchedulerLike} to use for managing the timers
58 * that handle the time-shift for each item.
59 * @return A function that returns an Observable that delays the emissions of
60 * the source Observable by the specified timeout or Date.
61 */
62 export function delay<T>(due: number | Date, scheduler: SchedulerLike = asyncScheduler): MonoTypeOperatorFunction<T> {
63 const duration = timer(due, scheduler);
64 return delayWhen(() => duration);
65 }