1 | import { ThrottleConfig } from './throttle';
|
---|
2 | import { MonoTypeOperatorFunction, SchedulerLike } from '../types';
|
---|
3 | /**
|
---|
4 | * Emits a value from the source Observable, then ignores subsequent source
|
---|
5 | * values for `duration` milliseconds, then repeats this process.
|
---|
6 | *
|
---|
7 | * <span class="informal">Lets a value pass, then ignores source values for the
|
---|
8 | * next `duration` milliseconds.</span>
|
---|
9 | *
|
---|
10 | * ![](throttleTime.png)
|
---|
11 | *
|
---|
12 | * `throttleTime` emits the source Observable values on the output Observable
|
---|
13 | * when its internal timer is disabled, and ignores source values when the timer
|
---|
14 | * is enabled. Initially, the timer is disabled. As soon as the first source
|
---|
15 | * value arrives, it is forwarded to the output Observable, and then the timer
|
---|
16 | * is enabled. After `duration` milliseconds (or the time unit determined
|
---|
17 | * internally by the optional `scheduler`) has passed, the timer is disabled,
|
---|
18 | * and this process repeats for the next source value. Optionally takes a
|
---|
19 | * {@link SchedulerLike} for managing timers.
|
---|
20 | *
|
---|
21 | * ## Examples
|
---|
22 | *
|
---|
23 | * #### Limit click rate
|
---|
24 | *
|
---|
25 | * Emit clicks at a rate of at most one click per second
|
---|
26 | * ```ts
|
---|
27 | * import { fromEvent } from 'rxjs';
|
---|
28 | * import { throttleTime } from 'rxjs/operators';
|
---|
29 | *
|
---|
30 | * const clicks = fromEvent(document, 'click');
|
---|
31 | * const result = clicks.pipe(throttleTime(1000));
|
---|
32 | * result.subscribe(x => console.log(x));
|
---|
33 | * ```
|
---|
34 | *
|
---|
35 | * #### Double Click
|
---|
36 | *
|
---|
37 | * The following example only emits clicks which happen within a subsequent
|
---|
38 | * delay of 400ms of the previous click. This for example can emulate a double
|
---|
39 | * click. It makes use of the `trailing` parameter of the throttle configuration.
|
---|
40 | *
|
---|
41 | * ```ts
|
---|
42 | * import { fromEvent, asyncScheduler } from 'rxjs';
|
---|
43 | * import { throttleTime, withLatestFrom } from 'rxjs/operators';
|
---|
44 | *
|
---|
45 | * // defaultThottleConfig = { leading: true, trailing: false }
|
---|
46 | * const throttleConfig = {
|
---|
47 | * leading: false,
|
---|
48 | * trailing: true
|
---|
49 | * }
|
---|
50 | *
|
---|
51 | * const click = fromEvent(document, 'click');
|
---|
52 | * const doubleClick = click.pipe(
|
---|
53 | * throttleTime(400, asyncScheduler, throttleConfig)
|
---|
54 | * );
|
---|
55 | *
|
---|
56 | * doubleClick.subscribe((throttleValue: Event) => {
|
---|
57 | * console.log(`Double-clicked! Timestamp: ${throttleValue.timeStamp}`);
|
---|
58 | * });
|
---|
59 | * ```
|
---|
60 | *
|
---|
61 | * If you enable the `leading` parameter in this example, the output would be the primary click and
|
---|
62 | * the double click, but restricts additional clicks within 400ms.
|
---|
63 | *
|
---|
64 | * @see {@link auditTime}
|
---|
65 | * @see {@link debounceTime}
|
---|
66 | * @see {@link delay}
|
---|
67 | * @see {@link sampleTime}
|
---|
68 | * @see {@link throttle}
|
---|
69 | *
|
---|
70 | * @param {number} duration Time to wait before emitting another value after
|
---|
71 | * emitting the last value, measured in milliseconds or the time unit determined
|
---|
72 | * internally by the optional `scheduler`.
|
---|
73 | * @param {SchedulerLike} [scheduler=async] The {@link SchedulerLike} to use for
|
---|
74 | * managing the timers that handle the throttling.
|
---|
75 | * @param {Object} config a configuration object to define `leading` and
|
---|
76 | * `trailing` behavior. Defaults to `{ leading: true, trailing: false }`.
|
---|
77 | * @return {Observable<T>} An Observable that performs the throttle operation to
|
---|
78 | * limit the rate of emissions from the source.
|
---|
79 | * @method throttleTime
|
---|
80 | * @owner Observable
|
---|
81 | */
|
---|
82 | export declare function throttleTime<T>(duration: number, scheduler?: SchedulerLike, config?: ThrottleConfig): MonoTypeOperatorFunction<T>;
|
---|