UNPKG

@wordpress/compose

Version:
42 lines (38 loc) 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useThrottle; var _useMemoOne = require("use-memo-one"); var _element = require("@wordpress/element"); var _throttle = require("../../utils/throttle"); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ /** * Throttles a function similar to Lodash's `throttle`. A new throttled function will * be returned and any scheduled calls cancelled if any of the arguments change, * including the function to throttle, so please wrap functions created on * render in components in `useCallback`. * * @see https://lodash.com/docs/4#throttle * * @template {(...args: any[]) => void} TFunc * * @param {TFunc} fn The function to throttle. * @param {number} [wait] The number of milliseconds to throttle invocations to. * @param {import('../../utils/throttle').ThrottleOptions} [options] The options object. See linked documentation for details. * @return {import('../../utils/debounce').DebouncedFunc<TFunc>} Throttled function. */ function useThrottle(fn, wait, options) { const throttled = (0, _useMemoOne.useMemoOne)(() => (0, _throttle.throttle)(fn, wait !== null && wait !== void 0 ? wait : 0, options), [fn, wait, options]); (0, _element.useEffect)(() => () => throttled.cancel(), [throttled]); return throttled; } //# sourceMappingURL=index.js.map