| 1 |
import { operate } from '../util/lift'; |
| 2 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 3 |
import { innerFrom } from '../observable/innerFrom'; |
| 4 |
export function throttle(durationSelector, config) { |
| 5 |
return operate(function (source, subscriber) { |
| 6 |
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; |
| 7 |
var hasValue = false; |
| 8 |
var sendValue = null; |
| 9 |
var throttled = null; |
| 10 |
var isComplete = false; |
| 11 |
var endThrottling = function () { |
| 12 |
throttled === null || throttled === void 0 ? void 0 : throttled.unsubscribe(); |
| 13 |
throttled = null; |
| 14 |
if (trailing) { |
| 15 |
send(); |
| 16 |
isComplete && subscriber.complete(); |
| 17 |
} |
| 18 |
}; |
| 19 |
var cleanupThrottling = function () { |
| 20 |
throttled = null; |
| 21 |
isComplete && subscriber.complete(); |
| 22 |
}; |
| 23 |
var startThrottle = function (value) { |
| 24 |
return (throttled = innerFrom(durationSelector(value)).subscribe(createOperatorSubscriber(subscriber, endThrottling, cleanupThrottling))); |
| 25 |
}; |
| 26 |
var send = function () { |
| 27 |
if (hasValue) { |
| 28 |
hasValue = false; |
| 29 |
var value = sendValue; |
| 30 |
sendValue = null; |
| 31 |
subscriber.next(value); |
| 32 |
!isComplete && startThrottle(value); |
| 33 |
} |
| 34 |
}; |
| 35 |
source.subscribe(createOperatorSubscriber(subscriber, function (value) { |
| 36 |
hasValue = true; |
| 37 |
sendValue = value; |
| 38 |
!(throttled && !throttled.closed) && (leading ? send() : startThrottle(value)); |
| 39 |
}, function () { |
| 40 |
isComplete = true; |
| 41 |
!(trailing && hasValue && throttled && !throttled.closed) && subscriber.complete(); |
| 42 |
})); |
| 43 |
}); |
| 44 |
} |
| 45 |
//# sourceMappingURL=throttle.js.map |