UNPKG

etc-hooks

Version:
40 lines (39 loc) 1.4 kB
function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } import { __read, __spreadArray } from "tslib"; import { throttle } from "lodash-es"; import { useMemo } from "react"; import useLatest from "../useLatest"; import useUnmount from "../useUnmount"; import { isFunction } from "../utils"; import isDev from "../utils/isDev"; function useThrottleFn(fn, options) { var _a; if (isDev) { if (!isFunction(fn)) { console.error("useThrottleFn expected parameter is a function, got ".concat(typeof fn === "undefined" ? "undefined" : _type_of(fn))); } } var fnRef = useLatest(fn); var wait = (_a = options === null || options === void 0 ? void 0 : options.wait) !== null && _a !== void 0 ? _a : 1000; var throttled = useMemo(function() { return throttle(function() { var args = []; for(var _i = 0; _i < arguments.length; _i++){ args[_i] = arguments[_i]; } return fnRef.current.apply(fnRef, __spreadArray([], __read(args), false)); }, wait, options); }, []); useUnmount(function() { throttled.cancel(); }); return { run: throttled, cancel: throttled.cancel, flush: throttled.flush }; } export default useThrottleFn;