Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / src / internal / operators / debounce.ts
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
7 import { OuterSubscriber } from '../OuterSubscriber';
8 import { InnerSubscriber } from '../InnerSubscriber';
9 import { subscribeToResult } from '../util/subscribeToResult';
10
11 /**
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.
14  *
15  * <span class="informal">It's like {@link debounceTime}, but the time span of
16  * emission silence is determined by a second Observable.</span>
17  *
18  * ![](debounce.png)
19  *
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.
29  *
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.
33  *
34  * ## Example
35  * Emit the most recent click after a burst of clicks
36  * ```ts
37  * import { fromEvent, interval } from 'rxjs';
38  * import { debounce } from 'rxjs/operators';
39  *
40  * const clicks = fromEvent(document, 'click');
41  * const result = clicks.pipe(debounce(() => interval(1000)));
42  * result.subscribe(x => console.log(x));
43  * ```
44  *
45  * @see {@link audit}
46  * @see {@link debounceTime}
47  * @see {@link delayWhen}
48  * @see {@link throttle}
49  *
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.
56  * @method debounce
57  * @owner Observable
58  */
59 export function debounce<T>(durationSelector: (value: T) => SubscribableOrPromise<any>): MonoTypeOperatorFunction<T> {
60   return (source: Observable<T>) => source.lift(new DebounceOperator(durationSelector));
61 }
62
63 class DebounceOperator<T> implements Operator<T, T> {
64   constructor(private durationSelector: (value: T) => SubscribableOrPromise<any>) {
65   }
66
67   call(subscriber: Subscriber<T>, source: any): TeardownLogic {
68     return source.subscribe(new DebounceSubscriber(subscriber, this.durationSelector));
69   }
70 }
71
72 /**
73  * We need this JSDoc comment for affecting ESDoc.
74  * @ignore
75  * @extends {Ignored}
76  */
77 class DebounceSubscriber<T, R> extends OuterSubscriber<T, R> {
78   private value: T;
79   private hasValue: boolean = false;
80   private durationSubscription: Subscription = null;
81
82   constructor(destination: Subscriber<R>,
83               private durationSelector: (value: T) => SubscribableOrPromise<any>) {
84     super(destination);
85   }
86
87   protected _next(value: T): void {
88     try {
89       const result = this.durationSelector.call(this, value);
90
91       if (result) {
92         this._tryNext(value, result);
93       }
94     } catch (err) {
95       this.destination.error(err);
96     }
97   }
98
99   protected _complete(): void {
100     this.emitValue();
101     this.destination.complete();
102   }
103
104   private _tryNext(value: T, duration: SubscribableOrPromise<any>): void {
105     let subscription = this.durationSubscription;
106     this.value = value;
107     this.hasValue = true;
108     if (subscription) {
109       subscription.unsubscribe();
110       this.remove(subscription);
111     }
112
113     subscription = subscribeToResult(this, duration);
114     if (subscription && !subscription.closed) {
115       this.add(this.durationSubscription = subscription);
116     }
117   }
118
119   notifyNext(outerValue: T, innerValue: R,
120              outerIndex: number, innerIndex: number,
121              innerSub: InnerSubscriber<T, R>): void {
122     this.emitValue();
123   }
124
125   notifyComplete(): void {
126     this.emitValue();
127   }
128
129   emitValue(): void {
130     if (this.hasValue) {
131       const value = this.value;
132       const subscription = this.durationSubscription;
133       if (subscription) {
134         this.durationSubscription = null;
135         subscription.unsubscribe();
136         this.remove(subscription);
137       }
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
143       this.value = null;
144       this.hasValue = false;
145       super._next(value);
146     }
147   }
148 }