Skip to content

Commit

Permalink
Use more descriptive types
Browse files Browse the repository at this point in the history
  • Loading branch information
sarayourfriend committed May 20, 2021
1 parent 3be9815 commit 21ebe47
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
4 changes: 3 additions & 1 deletion packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ render in components in `useCallback`.

_Parameters_

- _args_ `[TFunc, ...DebounceTailParameters]`: Arguments passed to Lodash's `debounce`.
- _fn_ `TFunc`:
- _wait_ `[number]`:
- _options_ `[import('lodash').DebounceSettings]`:

_Returns_

Expand Down
23 changes: 9 additions & 14 deletions packages/compose/src/hooks/use-debounce/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,26 @@ import { useMemoOne } from 'use-memo-one';
import { useEffect } from '@wordpress/element';

/* eslint-disable jsdoc/valid-types */
/**
* @template T
* @typedef {T extends [any, ...infer R] ? R : never} TailParameters
*/

// We want to keep the type of the function that is passed in but the rest we don't
// really care about, so this allows us to ignore the details of the rest of the paramters
// while still maintaining the proper generic type for `debounce` where the returned
// function matchest the signature of the passed in function.
/** @typedef {TailParameters<Parameters<typeof debounce>>} DebounceTailParameters */

/**
* Debounces a function with Lodash's `debounce`. A new debounced function will
* be returned and any scheduled calls cancelled if any of the arguments change,
* including the function to debounce, so please wrap functions created on
* render in components in `useCallback`.
*
* @template {(...args: any[]) => void} TFunc
* @param {[TFunc, ...DebounceTailParameters]} args Arguments passed to Lodash's `debounce`.
*
* @param {TFunc} fn
* @param {number} [wait]
* @param {import('lodash').DebounceSettings} [options]
* @return {TFunc & import('lodash').Cancelable} Debounced function.
*/
export default function useDebounce( ...args ) {
export default function useDebounce( fn, wait, options ) {
/* eslint-enable jsdoc/valid-types */
const debounced = useMemoOne( () => debounce( ...args ), args );
const debounced = useMemoOne( () => debounce( fn, wait, options ), [
fn,
wait,
options,
] );
useEffect( () => () => debounced.cancel(), [ debounced ] );
return debounced;
}

0 comments on commit 21ebe47

Please sign in to comment.