103 lines
No EOL
3.8 KiB
JavaScript
103 lines
No EOL
3.8 KiB
JavaScript
"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 OuterSubscriber_1 = require("../OuterSubscriber");
|
|
var subscribeToResult_1 = require("../util/subscribeToResult");
|
|
exports.defaultThrottleConfig = {
|
|
leading: true,
|
|
trailing: false
|
|
};
|
|
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 = (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;
|
|
}());
|
|
var ThrottleSubscriber = (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 = null;
|
|
};
|
|
ThrottleSubscriber.prototype.throttle = function (value) {
|
|
var duration = this.tryDurationSelector(value);
|
|
if (!!duration) {
|
|
this.add(this._throttled = subscribeToResult_1.subscribeToResult(this, duration));
|
|
}
|
|
};
|
|
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 = null;
|
|
if (_trailing) {
|
|
this.send();
|
|
}
|
|
};
|
|
ThrottleSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
|
|
this.throttlingDone();
|
|
};
|
|
ThrottleSubscriber.prototype.notifyComplete = function () {
|
|
this.throttlingDone();
|
|
};
|
|
return ThrottleSubscriber;
|
|
}(OuterSubscriber_1.OuterSubscriber));
|
|
//# sourceMappingURL=throttle.js.map
|