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';
6 import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
9 * Emits a value from the source Observable only after a particular time span
10 * determined by another Observable has passed without another source emission.
12 * <span class="informal">It's like {@link debounceTime}, but the time span of
13 * emission silence is determined by a second Observable.</span>
17 * `debounce` delays values emitted by the source Observable, but drops previous
18 * pending delayed emissions if a new value arrives on the source Observable.
19 * This operator keeps track of the most recent value from the source
20 * Observable, and spawns a duration Observable by calling the
21 * `durationSelector` function. The value is emitted only when the duration
22 * Observable emits a value or completes, and if no other value was emitted on
23 * the source Observable since the duration Observable was spawned. If a new
24 * value appears before the duration Observable emits, the previous value will
25 * be dropped and will not be emitted on the output Observable.
27 * Like {@link debounceTime}, this is a rate-limiting operator, and also a
28 * delay-like operator since output emissions do not necessarily occur at the
29 * same time as they did on the source Observable.
32 * Emit the most recent click after a burst of clicks
34 * import { fromEvent, interval } from 'rxjs';
35 * import { debounce } from 'rxjs/operators';
37 * const clicks = fromEvent(document, 'click');
38 * const result = clicks.pipe(debounce(() => interval(1000)));
39 * result.subscribe(x => console.log(x));
43 * @see {@link debounceTime}
44 * @see {@link delayWhen}
45 * @see {@link throttle}
47 * @param {function(value: T): SubscribableOrPromise} durationSelector A function
48 * that receives a value from the source Observable, for computing the timeout
49 * duration for each source value, returned as an Observable or a Promise.
50 * @return {Observable} An Observable that delays the emissions of the source
51 * Observable by the specified duration Observable returned by
52 * `durationSelector`, and may drop some values if they occur too frequently.
56 export function debounce<T>(durationSelector: (value: T) => SubscribableOrPromise<any>): MonoTypeOperatorFunction<T> {
57 return (source: Observable<T>) => source.lift(new DebounceOperator(durationSelector));
60 class DebounceOperator<T> implements Operator<T, T> {
61 constructor(private durationSelector: (value: T) => SubscribableOrPromise<any>) {
64 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
65 return source.subscribe(new DebounceSubscriber(subscriber, this.durationSelector));
70 * We need this JSDoc comment for affecting ESDoc.
74 class DebounceSubscriber<T, R> extends SimpleOuterSubscriber<T, R> {
76 private hasValue = false;
77 private durationSubscription?: Subscription;
79 constructor(destination: Subscriber<R>,
80 private durationSelector: (value: T) => SubscribableOrPromise<any>) {
84 protected _next(value: T): void {
86 const result = this.durationSelector.call(this, value);
89 this._tryNext(value, result);
92 this.destination.error!(err);
96 protected _complete(): void {
98 this.destination.complete!();
101 private _tryNext(value: T, duration: SubscribableOrPromise<any>): void {
102 let subscription = this.durationSubscription;
104 this.hasValue = true;
106 subscription.unsubscribe();
107 this.remove(subscription);
110 subscription = innerSubscribe(duration, new SimpleInnerSubscriber(this));
111 if (subscription && !subscription.closed) {
112 this.add(this.durationSubscription = subscription);
120 notifyComplete(): void {
126 const value = this.value;
127 const subscription = this.durationSubscription;
129 this.durationSubscription = undefined;
130 subscription.unsubscribe();
131 this.remove(subscription);
133 // This must be done *before* passing the value
134 // along to the destination because it's possible for
135 // the value to synchronously re-enter this operator
136 // recursively if the duration selector Observable
137 // emits synchronously
138 this.value = undefined;
139 this.hasValue = false;