UNPKG

ahooks-v2

Version:
83 lines (67 loc) 1.88 kB
"use strict"; var __read = this && this.__read || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) { ar.push(r.value); } } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = this && this.__spread || function () { for (var ar = [], i = 0; i < arguments.length; i++) { ar = ar.concat(__read(arguments[i])); } return ar; }; var __importDefault = this && this.__importDefault || function (mod) { return mod && mod.__esModule ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var lodash_debounce_1 = __importDefault(require("lodash.debounce")); var react_1 = require("react"); var useCreation_1 = __importDefault(require("../useCreation")); var useUnmount_1 = __importDefault(require("../useUnmount")); function useDebounceFn(fn, options) { var _a; var fnRef = react_1.useRef(fn); fnRef.current = fn; var wait = (_a = options === null || options === void 0 ? void 0 : options.wait) !== null && _a !== void 0 ? _a : 1000; var debounced = useCreation_1["default"](function () { return lodash_debounce_1["default"](function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return fnRef.current.apply(fnRef, __spread(args)); }, wait, options); }, []); useUnmount_1["default"](function () { debounced.cancel(); }); return { run: debounced, cancel: debounced.cancel, flush: debounced.flush }; } exports["default"] = useDebounceFn;