UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

153 lines 5.86 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var innerSubscribe_1 = require("../innerSubscribe"); exports.defaultThrottleConfig = { leading: true, trailing: false }; /** * Emits a value from the source Observable, then ignores subsequent source * values for a duration determined by another Observable, then repeats this * process. * * <span class="informal">It's like {@link throttleTime}, but the silencing * duration is determined by a second Observable.</span> * * ![](throttle.png) * * `throttle` emits the source Observable values on the output Observable * when its internal timer is disabled, and ignores source values when the timer * is enabled. Initially, the timer is disabled. As soon as the first source * value arrives, it is forwarded to the output Observable, and then the timer * is enabled by calling the `durationSelector` function with the source value, * which returns the "duration" Observable. When the duration Observable emits a * value or completes, the timer is disabled, and this process repeats for the * next source value. * * ## Example * Emit clicks at a rate of at most one click per second * ```ts * import { fromEvent } from 'rxjs'; * import { throttle } from 'rxjs/operators'; * * const clicks = fromEvent(document, 'click'); * const result = clicks.pipe(throttle(ev => interval(1000))); * result.subscribe(x => console.log(x)); * ``` * * @see {@link audit} * @see {@link debounce} * @see {@link delayWhen} * @see {@link sample} * @see {@link throttleTime} * * @param {function(value: T): SubscribableOrPromise} durationSelector A function * that receives a value from the source Observable, for computing the silencing * duration for each source value, returned as an Observable or a Promise. * @param {Object} config a configuration object to define `leading` and `trailing` behavior. Defaults * to `{ leading: true, trailing: false }`. * @return {Observable<T>} An Observable that performs the throttle operation to * limit the rate of emissions from the source. * @method throttle * @owner Observable */ function throttle(durationSelector, config) { if (config === void 0) { config = exports.defaultThrottleConfig; } return function (source) { return source.lift(new ThrottleOperator(durationSelector, !!config.leading, !!config.trailing)); }; } exports.throttle = throttle; var ThrottleOperator = /** @class */ (function () { function ThrottleOperator(durationSelector, leading, trailing) { this.durationSelector = durationSelector; this.leading = leading; this.trailing = trailing; } ThrottleOperator.prototype.call = function (subscriber, source) { return source.subscribe(new ThrottleSubscriber(subscriber, this.durationSelector, this.leading, this.trailing)); }; return ThrottleOperator; }()); /** * We need this JSDoc comment for affecting ESDoc * @ignore * @extends {Ignored} */ var ThrottleSubscriber = /** @class */ (function (_super) { __extends(ThrottleSubscriber, _super); function ThrottleSubscriber(destination, durationSelector, _leading, _trailing) { var _this = _super.call(this, destination) || this; _this.destination = destination; _this.durationSelector = durationSelector; _this._leading = _leading; _this._trailing = _trailing; _this._hasValue = false; return _this; } ThrottleSubscriber.prototype._next = function (value) { this._hasValue = true; this._sendValue = value; if (!this._throttled) { if (this._leading) { this.send(); } else { this.throttle(value); } } }; ThrottleSubscriber.prototype.send = function () { var _a = this, _hasValue = _a._hasValue, _sendValue = _a._sendValue; if (_hasValue) { this.destination.next(_sendValue); this.throttle(_sendValue); } this._hasValue = false; this._sendValue = undefined; }; ThrottleSubscriber.prototype.throttle = function (value) { var duration = this.tryDurationSelector(value); if (!!duration) { this.add(this._throttled = innerSubscribe_1.innerSubscribe(duration, new innerSubscribe_1.SimpleInnerSubscriber(this))); } }; ThrottleSubscriber.prototype.tryDurationSelector = function (value) { try { return this.durationSelector(value); } catch (err) { this.destination.error(err); return null; } }; ThrottleSubscriber.prototype.throttlingDone = function () { var _a = this, _throttled = _a._throttled, _trailing = _a._trailing; if (_throttled) { _throttled.unsubscribe(); } this._throttled = undefined; if (_trailing) { this.send(); } }; ThrottleSubscriber.prototype.notifyNext = function () { this.throttlingDone(); }; ThrottleSubscriber.prototype.notifyComplete = function () { this.throttlingDone(); }; return ThrottleSubscriber; }(innerSubscribe_1.SimpleOuterSubscriber)); //# sourceMappingURL=throttle.js.map