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