@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 useDebounceFn(fn, options) {
var _a;
if (__DEV__) {
if (typeof fn !== 'function') {
throw new Error("useDebounceFn 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 debounced = index$1(function () {
return lodashEs.debounce(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 () {
debounced.cancel();
});
return {
run: debounced,
cancel: debounced.cancel,
flush: debounced.flush,
};
}
module.exports = useDebounceFn;