2 var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
9 return function (d, b) {
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15 Object.defineProperty(exports, "__esModule", { value: true });
16 var OuterSubscriber_1 = require("../OuterSubscriber");
17 var subscribeToResult_1 = require("../util/subscribeToResult");
18 exports.defaultThrottleConfig = {
22 function throttle(durationSelector, config) {
23 if (config === void 0) { config = exports.defaultThrottleConfig; }
24 return function (source) { return source.lift(new ThrottleOperator(durationSelector, config.leading, config.trailing)); };
26 exports.throttle = throttle;
27 var ThrottleOperator = (function () {
28 function ThrottleOperator(durationSelector, leading, trailing) {
29 this.durationSelector = durationSelector;
30 this.leading = leading;
31 this.trailing = trailing;
33 ThrottleOperator.prototype.call = function (subscriber, source) {
34 return source.subscribe(new ThrottleSubscriber(subscriber, this.durationSelector, this.leading, this.trailing));
36 return ThrottleOperator;
38 var ThrottleSubscriber = (function (_super) {
39 __extends(ThrottleSubscriber, _super);
40 function ThrottleSubscriber(destination, durationSelector, _leading, _trailing) {
41 var _this = _super.call(this, destination) || this;
42 _this.destination = destination;
43 _this.durationSelector = durationSelector;
44 _this._leading = _leading;
45 _this._trailing = _trailing;
46 _this._hasValue = false;
49 ThrottleSubscriber.prototype._next = function (value) {
50 this._hasValue = true;
51 this._sendValue = value;
52 if (!this._throttled) {
61 ThrottleSubscriber.prototype.send = function () {
62 var _a = this, _hasValue = _a._hasValue, _sendValue = _a._sendValue;
64 this.destination.next(_sendValue);
65 this.throttle(_sendValue);
67 this._hasValue = false;
68 this._sendValue = null;
70 ThrottleSubscriber.prototype.throttle = function (value) {
71 var duration = this.tryDurationSelector(value);
73 this.add(this._throttled = subscribeToResult_1.subscribeToResult(this, duration));
76 ThrottleSubscriber.prototype.tryDurationSelector = function (value) {
78 return this.durationSelector(value);
81 this.destination.error(err);
85 ThrottleSubscriber.prototype.throttlingDone = function () {
86 var _a = this, _throttled = _a._throttled, _trailing = _a._trailing;
88 _throttled.unsubscribe();
90 this._throttled = null;
95 ThrottleSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
96 this.throttlingDone();
98 ThrottleSubscriber.prototype.notifyComplete = function () {
99 this.throttlingDone();
101 return ThrottleSubscriber;
102 }(OuterSubscriber_1.OuterSubscriber));
103 //# sourceMappingURL=throttle.js.map