1 /** PURE_IMPORTS_START tslib,_innerSubscribe PURE_IMPORTS_END */
2 import * as tslib_1 from "tslib";
3 import { SimpleOuterSubscriber, innerSubscribe, SimpleInnerSubscriber } from '../innerSubscribe';
4 export var defaultThrottleConfig = {
8 export function throttle(durationSelector, config) {
9 if (config === void 0) {
10 config = defaultThrottleConfig;
12 return function (source) { return source.lift(new ThrottleOperator(durationSelector, !!config.leading, !!config.trailing)); };
14 var ThrottleOperator = /*@__PURE__*/ (function () {
15 function ThrottleOperator(durationSelector, leading, trailing) {
16 this.durationSelector = durationSelector;
17 this.leading = leading;
18 this.trailing = trailing;
20 ThrottleOperator.prototype.call = function (subscriber, source) {
21 return source.subscribe(new ThrottleSubscriber(subscriber, this.durationSelector, this.leading, this.trailing));
23 return ThrottleOperator;
25 var ThrottleSubscriber = /*@__PURE__*/ (function (_super) {
26 tslib_1.__extends(ThrottleSubscriber, _super);
27 function ThrottleSubscriber(destination, durationSelector, _leading, _trailing) {
28 var _this = _super.call(this, destination) || this;
29 _this.destination = destination;
30 _this.durationSelector = durationSelector;
31 _this._leading = _leading;
32 _this._trailing = _trailing;
33 _this._hasValue = false;
36 ThrottleSubscriber.prototype._next = function (value) {
37 this._hasValue = true;
38 this._sendValue = value;
39 if (!this._throttled) {
48 ThrottleSubscriber.prototype.send = function () {
49 var _a = this, _hasValue = _a._hasValue, _sendValue = _a._sendValue;
51 this.destination.next(_sendValue);
52 this.throttle(_sendValue);
54 this._hasValue = false;
55 this._sendValue = undefined;
57 ThrottleSubscriber.prototype.throttle = function (value) {
58 var duration = this.tryDurationSelector(value);
60 this.add(this._throttled = innerSubscribe(duration, new SimpleInnerSubscriber(this)));
63 ThrottleSubscriber.prototype.tryDurationSelector = function (value) {
65 return this.durationSelector(value);
68 this.destination.error(err);
72 ThrottleSubscriber.prototype.throttlingDone = function () {
73 var _a = this, _throttled = _a._throttled, _trailing = _a._trailing;
75 _throttled.unsubscribe();
77 this._throttled = undefined;
82 ThrottleSubscriber.prototype.notifyNext = function () {
83 this.throttlingDone();
85 ThrottleSubscriber.prototype.notifyComplete = function () {
86 this.throttlingDone();
88 return ThrottleSubscriber;
89 }(SimpleOuterSubscriber));
90 //# sourceMappingURL=throttle.js.map