UNPKG

@taro-hooks/ahooks

Version:
43 lines (42 loc) 1.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var lodash_wechat_1 = require("lodash-wechat"); var core_1 = require("@taro-hooks/core"); var useLatest_1 = tslib_1.__importDefault(require("../useLatest")); var useUnmount_1 = tslib_1.__importDefault(require("../useUnmount")); function useThrottleFn(fn, options) { var _options$wait; if (process.env.NODE_ENV === 'development') { if (typeof fn !== 'function') { console.error("useThrottleFn expected parameter is a function, got " + typeof fn); } } var fnRef = (0, useLatest_1["default"])(fn); var wait = (_options$wait = options == null ? void 0 : options.wait) != null ? _options$wait : 1000; var throttled = (0, core_1.useMemo)(function () { return ( // @ts-ignore (0, lodash_wechat_1.throttle)(function () { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return fnRef == null ? void 0 : fnRef.current == null ? void 0 : fnRef.current.apply(fnRef, args); }, wait, options) ); }, []); (0, useUnmount_1["default"])(function () { // @ts-ignore throttled.cancel(); }); return { run: throttled, // @ts-ignore cancel: throttled.cancel, // @ts-ignore flush: throttled.flush }; } exports["default"] = useThrottleFn;