wishthis/node_modules/rxjs/_esm2015/internal/operators/throttleTime.js
2022-05-29 11:24:36 +02:00

73 lines
No EOL
2.4 KiB
JavaScript

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