| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.throttle = void 0; |
| 4 |
var lift_1 = require("../util/lift"); |
| 5 |
var OperatorSubscriber_1 = require("./OperatorSubscriber"); |
| 6 |
var innerFrom_1 = require("../observable/innerFrom"); |
| 7 |
function throttle(durationSelector, config) { |
| 8 |
return lift_1.operate(function (source, subscriber) { |
| 9 |
var _a = config !== null && config !== void 0 ? config : {}, _b = _a.leading, leading = _b === void 0 ? true : _b, _c = _a.trailing, trailing = _c === void 0 ? false : _c; |
| 10 |
var hasValue = false; |
| 11 |
var sendValue = null; |
| 12 |
var throttled = null; |
| 13 |
var isComplete = false; |
| 14 |
var endThrottling = function () { |
| 15 |
throttled === null || throttled === void 0 ? void 0 : throttled.unsubscribe(); |
| 16 |
throttled = null; |
| 17 |
if (trailing) { |
| 18 |
send(); |
| 19 |
isComplete && subscriber.complete(); |
| 20 |
} |
| 21 |
}; |
| 22 |
var cleanupThrottling = function () { |
| 23 |
throttled = null; |
| 24 |
isComplete && subscriber.complete(); |
| 25 |
}; |
| 26 |
var startThrottle = function (value) { |
| 27 |
return (throttled = innerFrom_1.innerFrom(durationSelector(value)).subscribe(OperatorSubscriber_1.createOperatorSubscriber(subscriber, endThrottling, cleanupThrottling))); |
| 28 |
}; |
| 29 |
var send = function () { |
| 30 |
if (hasValue) { |
| 31 |
hasValue = false; |
| 32 |
var value = sendValue; |
| 33 |
sendValue = null; |
| 34 |
subscriber.next(value); |
| 35 |
!isComplete && startThrottle(value); |
| 36 |
} |
| 37 |
}; |
| 38 |
source.subscribe(OperatorSubscriber_1.createOperatorSubscriber(subscriber, function (value) { |
| 39 |
hasValue = true; |
| 40 |
sendValue = value; |
| 41 |
!(throttled && !throttled.closed) && (leading ? send() : startThrottle(value)); |
| 42 |
}, function () { |
| 43 |
isComplete = true; |
| 44 |
!(trailing && hasValue && throttled && !throttled.closed) && subscriber.complete(); |
| 45 |
})); |
| 46 |
}); |
| 47 |
} |
| 48 |
exports.throttle = throttle; |
| 49 |
//# sourceMappingURL=throttle.js.map |