@render-props/throttle
Version:
A state container which limits the frequency with which setState can be called using `requestAnimationFrame`.
36 lines (26 loc) • 637 B
JavaScript
var _interopRequireWildcard = require('@babel/runtime/helpers/interopRequireWildcard')
exports.__esModule = true
exports.default = void 0
var _raf = _interopRequireWildcard(require('raf'))
var _default = cb => {
let frame
function later(thisArg, args) {
return function() {
frame = void 0
cb.apply(thisArg, args)
}
}
function throttled(...args) {
if (frame === void 0) {
frame = (0, _raf.default)(later(this, args))
}
}
throttled.cancel = function() {
if (frame !== void 0) {
;(0, _raf.cancel)(frame)
}
}
return throttled
}
exports.default = _default