source: trip-planner-front/node_modules/rxjs/src/internal/operators/debounceTime.ts

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.5 KB
Line 
1import { Operator } from '../Operator';
2import { Observable } from '../Observable';
3import { Subscriber } from '../Subscriber';
4import { Subscription } from '../Subscription';
5import { async } from '../scheduler/async';
6import { MonoTypeOperatorFunction, SchedulerLike, TeardownLogic } from '../types';
7
8/**
9 * Emits a value from the source Observable only after a particular time span
10 * has passed without another source emission.
11 *
12 * <span class="informal">It's like {@link delay}, but passes only the most
13 * recent value from each burst of emissions.</span>
14 *
15 * ![](debounceTime.png)
16 *
17 * `debounceTime` delays values emitted by the source Observable, but drops
18 * previous pending delayed emissions if a new value arrives on the source
19 * Observable. This operator keeps track of the most recent value from the
20 * source Observable, and emits that only when `dueTime` enough time has passed
21 * without any other value appearing on the source Observable. If a new value
22 * appears before `dueTime` silence occurs, the previous value will be dropped
23 * and will not be emitted on the output Observable.
24 *
25 * This is a rate-limiting operator, because it is impossible for more than one
26 * value to be emitted in any time window of duration `dueTime`, but it is also
27 * a delay-like operator since output emissions do not occur at the same time as
28 * they did on the source Observable. Optionally takes a {@link SchedulerLike} for
29 * managing timers.
30 *
31 * ## Example
32 * Emit the most recent click after a burst of clicks
33 * ```ts
34 * import { fromEvent } from 'rxjs';
35 * import { debounceTime } from 'rxjs/operators';
36 *
37 * const clicks = fromEvent(document, 'click');
38 * const result = clicks.pipe(debounceTime(1000));
39 * result.subscribe(x => console.log(x));
40 * ```
41 *
42 * @see {@link auditTime}
43 * @see {@link debounce}
44 * @see {@link delay}
45 * @see {@link sampleTime}
46 * @see {@link throttleTime}
47 *
48 * @param {number} dueTime The timeout duration in milliseconds (or the time
49 * unit determined internally by the optional `scheduler`) for the window of
50 * time required to wait for emission silence before emitting the most recent
51 * source value.
52 * @param {SchedulerLike} [scheduler=async] The {@link SchedulerLike} to use for
53 * managing the timers that handle the timeout for each value.
54 * @return {Observable} An Observable that delays the emissions of the source
55 * Observable by the specified `dueTime`, and may drop some values if they occur
56 * too frequently.
57 * @method debounceTime
58 * @owner Observable
59 */
60export function debounceTime<T>(dueTime: number, scheduler: SchedulerLike = async): MonoTypeOperatorFunction<T> {
61 return (source: Observable<T>) => source.lift(new DebounceTimeOperator(dueTime, scheduler));
62}
63
64class DebounceTimeOperator<T> implements Operator<T, T> {
65 constructor(private dueTime: number, private scheduler: SchedulerLike) {
66 }
67
68 call(subscriber: Subscriber<T>, source: any): TeardownLogic {
69 return source.subscribe(new DebounceTimeSubscriber(subscriber, this.dueTime, this.scheduler));
70 }
71}
72
73/**
74 * We need this JSDoc comment for affecting ESDoc.
75 * @ignore
76 * @extends {Ignored}
77 */
78class DebounceTimeSubscriber<T> extends Subscriber<T> {
79 private debouncedSubscription: Subscription = null;
80 private lastValue: T = null;
81 private hasValue: boolean = false;
82
83 constructor(destination: Subscriber<T>,
84 private dueTime: number,
85 private scheduler: SchedulerLike) {
86 super(destination);
87 }
88
89 protected _next(value: T) {
90 this.clearDebounce();
91 this.lastValue = value;
92 this.hasValue = true;
93 this.add(this.debouncedSubscription = this.scheduler.schedule(dispatchNext, this.dueTime, this));
94 }
95
96 protected _complete() {
97 this.debouncedNext();
98 this.destination.complete();
99 }
100
101 debouncedNext(): void {
102 this.clearDebounce();
103
104 if (this.hasValue) {
105 const { lastValue } = this;
106 // This must be done *before* passing the value
107 // along to the destination because it's possible for
108 // the value to synchronously re-enter this operator
109 // recursively when scheduled with things like
110 // VirtualScheduler/TestScheduler.
111 this.lastValue = null;
112 this.hasValue = false;
113 this.destination.next(lastValue);
114 }
115 }
116
117 private clearDebounce(): void {
118 const debouncedSubscription = this.debouncedSubscription;
119
120 if (debouncedSubscription !== null) {
121 this.remove(debouncedSubscription);
122 debouncedSubscription.unsubscribe();
123 this.debouncedSubscription = null;
124 }
125 }
126}
127
128function dispatchNext(subscriber: DebounceTimeSubscriber<any>) {
129 subscriber.debouncedNext();
130}
Note: See TracBrowser for help on using the repository browser.