UNPKG

@taro-hooks/ahooks

Version:
37 lines 1.17 kB
import { debounce } from 'lodash-wechat'; import { useMemo } from '@taro-hooks/core'; import useLatest from '../useLatest'; import useUnmount from '../useUnmount'; function useDebounceFn(fn, options) { var _options$wait; if (process.env.NODE_ENV === 'development') { if (typeof fn !== 'function') { console.error("useDebounceFn expected parameter is a function, got " + typeof fn); } } var fnRef = useLatest(fn); var wait = (_options$wait = options == null ? void 0 : options.wait) != null ? _options$wait : 1000; var debounced = useMemo(function () { return ( // @ts-ignore debounce(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) ); }, []); useUnmount(function () { // @ts-ignore debounced.cancel(); }); return { run: debounced, // @ts-ignore cancel: debounced.cancel, // @ts-ignore flush: debounced.flush }; } export default useDebounceFn;