@td-design/rn-hooks
Version:
从ahooks里面抽取的一些在rn项目里可以使用的hooks。提高开发效率
43 lines (39 loc) • 1.32 kB
JavaScript
;
var _tslib = require('../_virtual/_tslib.js');
var lodashEs = require('lodash-es');
var index$1 = require('../useCreation/index.js');
var index = require('../useMemoizedFn/index.js');
var index$2 = require('../useUnmount/index.js');
/**
* 用来处理节流函数的 Hook。
* @param fn 需要节流的函数
* @param options 配置节流的行为
*/
function useThrottleFn(fn, options) {
var _a;
if (__DEV__) {
if (typeof fn !== 'function') {
throw new Error("useThrottleFn expected parameter is a function, got ".concat(typeof fn));
}
}
var fnRef = index(fn);
var wait = (_a = options === null || options === void 0 ? void 0 : options.wait) !== null && _a !== void 0 ? _a : 1000;
var throttled = index$1(function () {
return lodashEs.throttle(function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return fnRef.apply(void 0, _tslib.__spreadArray([], _tslib.__read(args), false));
}, wait, options);
}, []);
index$2(function () {
throttled.cancel();
});
return {
run: throttled,
cancel: throttled.cancel,
flush: throttled.flush,
};
}
module.exports = useThrottleFn;