1 import { Operator } from '../Operator';
2 import { Observable } from '../Observable';
3 import { Subscriber } from '../Subscriber';
4 import { Subscription } from '../Subscription';
5 import { MonoTypeOperatorFunction, SubscribableOrPromise, TeardownLogic } from '../types';
7 import { OuterSubscriber } from '../OuterSubscriber';
8 import { InnerSubscriber } from '../InnerSubscriber';
9 import { subscribeToResult } from '../util/subscribeToResult';
12 * Emits a value from the source Observable only after a particular time span
13 * determined by another Observable has passed without another source emission.
15 * <span class="informal">It's like {@link debounceTime}, but the time span of
16 * emission silence is determined by a second Observable.</span>
20 * `debounce` delays values emitted by the source Observable, but drops previous
21 * pending delayed emissions if a new value arrives on the source Observable.
22 * This operator keeps track of the most recent value from the source
23 * Observable, and spawns a duration Observable by calling the
24 * `durationSelector` function. The value is emitted only when the duration
25 * Observable emits a value or completes, and if no other value was emitted on
26 * the source Observable since the duration Observable was spawned. If a new
27 * value appears before the duration Observable emits, the previous value will
28 * be dropped and will not be emitted on the output Observable.
30 * Like {@link debounceTime}, this is a rate-limiting operator, and also a
31 * delay-like operator since output emissions do not necessarily occur at the
32 * same time as they did on the source Observable.
35 * Emit the most recent click after a burst of clicks
37 * import { fromEvent, interval } from 'rxjs';
38 * import { debounce } from 'rxjs/operators';
40 * const clicks = fromEvent(document, 'click');
41 * const result = clicks.pipe(debounce(() => interval(1000)));
42 * result.subscribe(x => console.log(x));
46 * @see {@link debounceTime}
47 * @see {@link delayWhen}
48 * @see {@link throttle}
50 * @param {function(value: T): SubscribableOrPromise} durationSelector A function
51 * that receives a value from the source Observable, for computing the timeout
52 * duration for each source value, returned as an Observable or a Promise.
53 * @return {Observable} An Observable that delays the emissions of the source
54 * Observable by the specified duration Observable returned by
55 * `durationSelector`, and may drop some values if they occur too frequently.
59 export function debounce<T>(durationSelector: (value: T) => SubscribableOrPromise<any>): MonoTypeOperatorFunction<T> {
60 return (source: Observable<T>) => source.lift(new DebounceOperator(durationSelector));
63 class DebounceOperator<T> implements Operator<T, T> {
64 constructor(private durationSelector: (value: T) => SubscribableOrPromise<any>) {
67 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
68 return source.subscribe(new DebounceSubscriber(subscriber, this.durationSelector));
73 * We need this JSDoc comment for affecting ESDoc.
77 class DebounceSubscriber<T, R> extends OuterSubscriber<T, R> {
79 private hasValue: boolean = false;
80 private durationSubscription: Subscription = null;
82 constructor(destination: Subscriber<R>,
83 private durationSelector: (value: T) => SubscribableOrPromise<any>) {
87 protected _next(value: T): void {
89 const result = this.durationSelector.call(this, value);
92 this._tryNext(value, result);
95 this.destination.error(err);
99 protected _complete(): void {
101 this.destination.complete();
104 private _tryNext(value: T, duration: SubscribableOrPromise<any>): void {
105 let subscription = this.durationSubscription;
107 this.hasValue = true;
109 subscription.unsubscribe();
110 this.remove(subscription);
113 subscription = subscribeToResult(this, duration);
114 if (subscription && !subscription.closed) {
115 this.add(this.durationSubscription = subscription);
119 notifyNext(outerValue: T, innerValue: R,
120 outerIndex: number, innerIndex: number,
121 innerSub: InnerSubscriber<T, R>): void {
125 notifyComplete(): void {
131 const value = this.value;
132 const subscription = this.durationSubscription;
134 this.durationSubscription = null;
135 subscription.unsubscribe();
136 this.remove(subscription);
138 // This must be done *before* passing the value
139 // along to the destination because it's possible for
140 // the value to synchronously re-enter this operator
141 // recursively if the duration selector Observable
142 // emits synchronously
144 this.hasValue = false;