123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
- export const defaultThrottleConfig = {
- leading: true,
- trailing: false
- };
- export function throttle(durationSelector, config = defaultThrottleConfig) {
- return (source) => source.lift(new ThrottleOperator(durationSelector, !!config.leading, !!config.trailing));
- }
- class ThrottleOperator {
- constructor(durationSelector, leading, trailing) {
- this.durationSelector = durationSelector;
- this.leading = leading;
- this.trailing = trailing;
- }
- call(subscriber, source) {
- return source.subscribe(new ThrottleSubscriber(subscriber, this.durationSelector, this.leading, this.trailing));
- }
- }
- class ThrottleSubscriber extends SimpleOuterSubscriber {
- constructor(destination, durationSelector, _leading, _trailing) {
- super(destination);
- this.destination = destination;
- this.durationSelector = durationSelector;
- this._leading = _leading;
- this._trailing = _trailing;
- this._hasValue = false;
- }
- _next(value) {
- this._hasValue = true;
- this._sendValue = value;
- if (!this._throttled) {
- if (this._leading) {
- this.send();
- }
- else {
- this.throttle(value);
- }
- }
- }
- send() {
- const { _hasValue, _sendValue } = this;
- if (_hasValue) {
- this.destination.next(_sendValue);
- this.throttle(_sendValue);
- }
- this._hasValue = false;
- this._sendValue = undefined;
- }
- throttle(value) {
- const duration = this.tryDurationSelector(value);
- if (!!duration) {
- this.add(this._throttled = innerSubscribe(duration, new SimpleInnerSubscriber(this)));
- }
- }
- tryDurationSelector(value) {
- try {
- return this.durationSelector(value);
- }
- catch (err) {
- this.destination.error(err);
- return null;
- }
- }
- throttlingDone() {
- const { _throttled, _trailing } = this;
- if (_throttled) {
- _throttled.unsubscribe();
- }
- this._throttled = undefined;
- if (_trailing) {
- this.send();
- }
- }
- notifyNext() {
- this.throttlingDone();
- }
- notifyComplete() {
- this.throttlingDone();
- }
- }
- //# sourceMappingURL=throttle.js.map
|