@reactivex/rxjs
Version:
Reactive Extensions for modern JavaScript
58 lines • 1.64 kB
JavaScript
import { async } from '../scheduler/async';
import { Subscriber } from '../Subscriber';
/**
* @param delay
* @param scheduler
* @return {Observable<R>|WebSocketSubject<T>|Observable<T>}
* @method auditTime
* @owner Observable
*/
export function auditTime(delay, scheduler = async) {
return this.lift(new AuditTimeOperator(delay, scheduler));
}
class AuditTimeOperator {
constructor(delay, scheduler) {
this.delay = delay;
this.scheduler = scheduler;
}
call(subscriber, source) {
return source._subscribe(new AuditTimeSubscriber(subscriber, this.delay, this.scheduler));
}
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class AuditTimeSubscriber extends Subscriber {
constructor(destination, delay, scheduler) {
super(destination);
this.delay = delay;
this.scheduler = scheduler;
this.hasValue = false;
}
_next(value) {
this.value = value;
this.hasValue = true;
if (!this.throttled) {
this.add(this.throttled = this.scheduler.schedule(dispatchNext, this.delay, this));
}
}
clearThrottle() {
const { value, hasValue, throttled } = this;
if (throttled) {
this.remove(throttled);
this.throttled = null;
throttled.unsubscribe();
}
if (hasValue) {
this.value = null;
this.hasValue = false;
this.destination.next(value);
}
}
}
function dispatchNext(subscriber) {
subscriber.clearThrottle();
}
//# sourceMappingURL=auditTime.js.map