1 import { Operator } from '../Operator';
2 import { Observable } from '../Observable';
3 import { Subscriber } from '../Subscriber';
4 import { Subscription } from '../Subscription';
6 import { MonoTypeOperatorFunction, SubscribableOrPromise, TeardownLogic } from '../types';
7 import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
9 export interface ThrottleConfig {
14 export const defaultThrottleConfig: ThrottleConfig = {
20 * Emits a value from the source Observable, then ignores subsequent source
21 * values for a duration determined by another Observable, then repeats this
24 * <span class="informal">It's like {@link throttleTime}, but the silencing
25 * duration is determined by a second Observable.</span>
29 * `throttle` emits the source Observable values on the output Observable
30 * when its internal timer is disabled, and ignores source values when the timer
31 * is enabled. Initially, the timer is disabled. As soon as the first source
32 * value arrives, it is forwarded to the output Observable, and then the timer
33 * is enabled by calling the `durationSelector` function with the source value,
34 * which returns the "duration" Observable. When the duration Observable emits a
35 * value or completes, the timer is disabled, and this process repeats for the
39 * Emit clicks at a rate of at most one click per second
41 * import { fromEvent } from 'rxjs';
42 * import { throttle } from 'rxjs/operators';
44 * const clicks = fromEvent(document, 'click');
45 * const result = clicks.pipe(throttle(ev => interval(1000)));
46 * result.subscribe(x => console.log(x));
50 * @see {@link debounce}
51 * @see {@link delayWhen}
53 * @see {@link throttleTime}
55 * @param {function(value: T): SubscribableOrPromise} durationSelector A function
56 * that receives a value from the source Observable, for computing the silencing
57 * duration for each source value, returned as an Observable or a Promise.
58 * @param {Object} config a configuration object to define `leading` and `trailing` behavior. Defaults
59 * to `{ leading: true, trailing: false }`.
60 * @return {Observable<T>} An Observable that performs the throttle operation to
61 * limit the rate of emissions from the source.
65 export function throttle<T>(durationSelector: (value: T) => SubscribableOrPromise<any>,
66 config: ThrottleConfig = defaultThrottleConfig): MonoTypeOperatorFunction<T> {
67 return (source: Observable<T>) => source.lift(new ThrottleOperator(durationSelector, !!config.leading, !!config.trailing));
70 class ThrottleOperator<T> implements Operator<T, T> {
71 constructor(private durationSelector: (value: T) => SubscribableOrPromise<any>,
72 private leading: boolean,
73 private trailing: boolean) {
76 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
77 return source.subscribe(
78 new ThrottleSubscriber(subscriber, this.durationSelector, this.leading, this.trailing)
84 * We need this JSDoc comment for affecting ESDoc
88 class ThrottleSubscriber<T, R> extends SimpleOuterSubscriber<T, R> {
89 private _throttled?: Subscription;
90 private _sendValue?: T;
91 private _hasValue = false;
93 constructor(protected destination: Subscriber<T>,
94 private durationSelector: (value: T) => SubscribableOrPromise<number>,
95 private _leading: boolean,
96 private _trailing: boolean) {
100 protected _next(value: T): void {
101 this._hasValue = true;
102 this._sendValue = value;
104 if (!this._throttled) {
108 this.throttle(value);
114 const { _hasValue, _sendValue } = this;
116 this.destination.next(_sendValue);
117 this.throttle(_sendValue!);
119 this._hasValue = false;
120 this._sendValue = undefined;
123 private throttle(value: T): void {
124 const duration = this.tryDurationSelector(value);
126 this.add(this._throttled = innerSubscribe(duration, new SimpleInnerSubscriber(this)));
130 private tryDurationSelector(value: T): SubscribableOrPromise<any> | null {
132 return this.durationSelector(value);
134 this.destination.error(err);
139 private throttlingDone() {
140 const { _throttled, _trailing } = this;
142 _throttled.unsubscribe();
144 this._throttled = undefined;
152 this.throttlingDone();
155 notifyComplete(): void {
156 this.throttlingDone();