UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

87 lines 2.91 kB
import { OuterSubscriber } from '../OuterSubscriber'; import { subscribeToResult } from '../util/subscribeToResult'; /** * Returns the source Observable delayed by the computed debounce duration, * with the duration lengthened if a new source item arrives before the delay * duration ends. * In practice, for each item emitted on the source, this operator holds the * latest item, waits for a silence as long as the `durationSelector` specifies, * and only then emits the latest source item on the result Observable. * @param {function} durationSelector function for computing the timeout duration for each item. * @return {Observable} an Observable the same as source Observable, but drops items. * @method debounce * @owner Observable */ export function debounce(durationSelector) { return this.lift(new DebounceOperator(durationSelector)); } class DebounceOperator { constructor(durationSelector) { this.durationSelector = durationSelector; } call(subscriber, source) { return source._subscribe(new DebounceSubscriber(subscriber, this.durationSelector)); } } /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ class DebounceSubscriber extends OuterSubscriber { constructor(destination, durationSelector) { super(destination); this.durationSelector = durationSelector; this.hasValue = false; this.durationSubscription = null; } _next(value) { try { const result = this.durationSelector.call(this, value); if (result) { this._tryNext(value, result); } } catch (err) { this.destination.error(err); } } _complete() { this.emitValue(); this.destination.complete(); } _tryNext(value, duration) { let subscription = this.durationSubscription; this.value = value; this.hasValue = true; if (subscription) { subscription.unsubscribe(); this.remove(subscription); } subscription = subscribeToResult(this, duration); if (!subscription.isUnsubscribed) { this.add(this.durationSubscription = subscription); } } notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) { this.emitValue(); } notifyComplete() { this.emitValue(); } emitValue() { if (this.hasValue) { const value = this.value; const subscription = this.durationSubscription; if (subscription) { this.durationSubscription = null; subscription.unsubscribe(); this.remove(subscription); } this.value = null; this.hasValue = false; super._next(value); } } } //# sourceMappingURL=debounce.js.map