throttleTime.js
2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { Subscriber } from '../Subscriber';
import { async } from '../scheduler/async';
import { defaultThrottleConfig } from './throttle';
export function throttleTime(duration, scheduler = async, config = defaultThrottleConfig) {
return (source) => source.lift(new ThrottleTimeOperator(duration, scheduler, config.leading, config.trailing));
}
class ThrottleTimeOperator {
constructor(duration, scheduler, leading, trailing) {
this.duration = duration;
this.scheduler = scheduler;
this.leading = leading;
this.trailing = trailing;
}
call(subscriber, source) {
return source.subscribe(new ThrottleTimeSubscriber(subscriber, this.duration, this.scheduler, this.leading, this.trailing));
}
}
class ThrottleTimeSubscriber extends Subscriber {
constructor(destination, duration, scheduler, leading, trailing) {
super(destination);
this.duration = duration;
this.scheduler = scheduler;
this.leading = leading;
this.trailing = trailing;
this._hasTrailingValue = false;
this._trailingValue = null;
}
_next(value) {
if (this.throttled) {
if (this.trailing) {
this._trailingValue = value;
this._hasTrailingValue = true;
}
}
else {
this.add(this.throttled = this.scheduler.schedule(dispatchNext, this.duration, { subscriber: this }));
if (this.leading) {
this.destination.next(value);
}
else if (this.trailing) {
this._trailingValue = value;
this._hasTrailingValue = true;
}
}
}
_complete() {
if (this._hasTrailingValue) {
this.destination.next(this._trailingValue);
this.destination.complete();
}
else {
this.destination.complete();
}
}
clearThrottle() {
const throttled = this.throttled;
if (throttled) {
if (this.trailing && this._hasTrailingValue) {
this.destination.next(this._trailingValue);
this._trailingValue = null;
this._hasTrailingValue = false;
}
throttled.unsubscribe();
this.remove(throttled);
this.throttled = null;
}
}
}
function dispatchNext(arg) {
const { subscriber } = arg;
subscriber.clearThrottle();
}
//# sourceMappingURL=throttleTime.js.map