Files
2026-01-09 14:52:46 +00:00

76 lines
2.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useDebounceFn = useDebounceFn;
var _regeneratorRuntime2 = _interopRequireDefault(require("@babel/runtime/helpers/regeneratorRuntime"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = require("react");
var _useRefFunction = require("../useRefFunction");
/**
* 一个去抖的 hook传入一个 function返回一个去抖后的 function
* @param {(...args:T) => Promise<any>} fn
* @param {number} wait?
*/
function useDebounceFn(fn, wait) {
var callback = (0, _useRefFunction.useRefFunction)(fn);
var timer = (0, _react.useRef)();
var cancel = (0, _react.useCallback)(function () {
if (timer.current) {
clearTimeout(timer.current);
timer.current = null;
}
}, []);
var run = (0, _react.useCallback)( /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee2() {
var _len,
args,
_key,
_args2 = arguments;
return (0, _regeneratorRuntime2.default)().wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
for (_len = _args2.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = _args2[_key];
}
if (!(wait === 0 || wait === undefined)) {
_context2.next = 3;
break;
}
return _context2.abrupt("return", callback.apply(void 0, args));
case 3:
cancel();
return _context2.abrupt("return", new Promise(function (resolve) {
timer.current = setTimeout( /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee() {
return (0, _regeneratorRuntime2.default)().wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
_context.t0 = resolve;
_context.next = 3;
return callback.apply(void 0, args);
case 3:
_context.t1 = _context.sent;
(0, _context.t0)(_context.t1);
return _context.abrupt("return");
case 6:
case "end":
return _context.stop();
}
}, _callee);
})), wait);
}));
case 5:
case "end":
return _context2.stop();
}
}, _callee2);
})), [callback, cancel, wait]);
(0, _react.useEffect)(function () {
return cancel;
}, [cancel]);
return {
run: run,
cancel: cancel
};
}