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';
|
---|
7 |
|
---|
8 | /**
|
---|
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.
|
---|
11 | *
|
---|
12 | * <span class="informal">It's like {@link debounceTime}, but the time span of
|
---|
13 | * emission silence is determined by a second Observable.</span>
|
---|
14 | *
|
---|
15 | * ![](debounce.png)
|
---|
16 | *
|
---|
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.
|
---|
26 | *
|
---|
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.
|
---|
30 | *
|
---|
31 | * ## Example
|
---|
32 | * Emit the most recent click after a burst of clicks
|
---|
33 | * ```ts
|
---|
34 | * import { fromEvent, interval } from 'rxjs';
|
---|
35 | * import { debounce } from 'rxjs/operators';
|
---|
36 | *
|
---|
37 | * const clicks = fromEvent(document, 'click');
|
---|
38 | * const result = clicks.pipe(debounce(() => interval(1000)));
|
---|
39 | * result.subscribe(x => console.log(x));
|
---|
40 | * ```
|
---|
41 | *
|
---|
42 | * @see {@link audit}
|
---|
43 | * @see {@link debounceTime}
|
---|
44 | * @see {@link delayWhen}
|
---|
45 | * @see {@link throttle}
|
---|
46 | *
|
---|
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.
|
---|
53 | * @method debounce
|
---|
54 | * @owner Observable
|
---|
55 | */
|
---|
56 | export function debounce<T>(durationSelector: (value: T) => SubscribableOrPromise<any>): MonoTypeOperatorFunction<T> {
|
---|
57 | return (source: Observable<T>) => source.lift(new DebounceOperator(durationSelector));
|
---|
58 | }
|
---|
59 |
|
---|
60 | class DebounceOperator<T> implements Operator<T, T> {
|
---|
61 | constructor(private durationSelector: (value: T) => SubscribableOrPromise<any>) {
|
---|
62 | }
|
---|
63 |
|
---|
64 | call(subscriber: Subscriber<T>, source: any): TeardownLogic {
|
---|
65 | return source.subscribe(new DebounceSubscriber(subscriber, this.durationSelector));
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * We need this JSDoc comment for affecting ESDoc.
|
---|
71 | * @ignore
|
---|
72 | * @extends {Ignored}
|
---|
73 | */
|
---|
74 | class DebounceSubscriber<T, R> extends SimpleOuterSubscriber<T, R> {
|
---|
75 | private value?: T;
|
---|
76 | private hasValue = false;
|
---|
77 | private durationSubscription?: Subscription;
|
---|
78 |
|
---|
79 | constructor(destination: Subscriber<R>,
|
---|
80 | private durationSelector: (value: T) => SubscribableOrPromise<any>) {
|
---|
81 | super(destination);
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected _next(value: T): void {
|
---|
85 | try {
|
---|
86 | const result = this.durationSelector.call(this, value);
|
---|
87 |
|
---|
88 | if (result) {
|
---|
89 | this._tryNext(value, result);
|
---|
90 | }
|
---|
91 | } catch (err) {
|
---|
92 | this.destination.error!(err);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected _complete(): void {
|
---|
97 | this.emitValue();
|
---|
98 | this.destination.complete!();
|
---|
99 | }
|
---|
100 |
|
---|
101 | private _tryNext(value: T, duration: SubscribableOrPromise<any>): void {
|
---|
102 | let subscription = this.durationSubscription;
|
---|
103 | this.value = value;
|
---|
104 | this.hasValue = true;
|
---|
105 | if (subscription) {
|
---|
106 | subscription.unsubscribe();
|
---|
107 | this.remove(subscription);
|
---|
108 | }
|
---|
109 |
|
---|
110 | subscription = innerSubscribe(duration, new SimpleInnerSubscriber(this));
|
---|
111 | if (subscription && !subscription.closed) {
|
---|
112 | this.add(this.durationSubscription = subscription);
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | notifyNext(): void {
|
---|
117 | this.emitValue();
|
---|
118 | }
|
---|
119 |
|
---|
120 | notifyComplete(): void {
|
---|
121 | this.emitValue();
|
---|
122 | }
|
---|
123 |
|
---|
124 | emitValue(): void {
|
---|
125 | if (this.hasValue) {
|
---|
126 | const value = this.value;
|
---|
127 | const subscription = this.durationSubscription;
|
---|
128 | if (subscription) {
|
---|
129 | this.durationSubscription = undefined;
|
---|
130 | subscription.unsubscribe();
|
---|
131 | this.remove(subscription);
|
---|
132 | }
|
---|
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;
|
---|
140 | super._next(value!);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|