Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compose: Add types to useDebounce #32015

Merged
merged 3 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,13 @@ render in components in `useCallback`.

_Parameters_

- _args_ `...any`: Arguments passed to Lodash's `debounce`.
- _fn_ `TFunc`:
- _wait_ `[number]`:
- _options_ `[import('lodash').DebounceSettings]`:
ciampo marked this conversation as resolved.
Show resolved Hide resolved

_Returns_

- `Function`: Debounced function.
- `TFunc & import('lodash').Cancelable`: Debounced function.

<a name="useFocusOnMount" href="#useFocusOnMount">#</a> **useFocusOnMount**

Expand Down
17 changes: 13 additions & 4 deletions packages/compose/src/hooks/use-debounce/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@ import { useMemoOne } from 'use-memo-one';
*/
import { useEffect } from '@wordpress/element';

/* eslint-disable jsdoc/valid-types */
/**
* 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`.
*
* @param {...any} args Arguments passed to Lodash's `debounce`.
* @template {(...args: any[]) => void} TFunc
*
* @return {Function} Debounced function.
* @param {TFunc} fn
* @param {number} [wait]
* @param {import('lodash').DebounceSettings} [options]
* @return {TFunc & import('lodash').Cancelable} Debounced function.
*/
export default function useDebounce( ...args ) {
const debounced = useMemoOne( () => debounce( ...args ), args );
export default function useDebounce( fn, wait, options ) {
/* eslint-enable jsdoc/valid-types */
const debounced = useMemoOne( () => debounce( fn, wait, options ), [
fn,
wait,
options,
] );
useEffect( () => () => debounced.cancel(), [ debounced ] );
return debounced;
}
1 change: 1 addition & 0 deletions packages/compose/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"src/higher-order/with-instance-id/**/*",
"src/hooks/use-async-list/**/*",
"src/hooks/use-constrained-tabbing/**/*",
"src/hooks/use-debounce/**/*",
"src/hooks/use-instance-id/**/*",
"src/utils/**/*"
]
Expand Down