{ "$schema": "https://ui.shadcn.com/schema/registry-item.json", "name": "useDebounceFn", "title": "useDebounceFn", "description": "useDebounceFn hook", "registryDependencies": [ "https://shadcn-ahooks.vercel.app/r/useDebounce.json", "https://shadcn-ahooks.vercel.app/r/useLatest.json", "https://shadcn-ahooks.vercel.app/r/useUnmount.json", "https://shadcn-ahooks.vercel.app/r/utils.json" ], "files": [ { "path": "packages/hooks/src/useDebounceFn/index.ts", "content": "import { debounce } from 'es-toolkit/compat';\nimport { useMemo } from 'react';\nimport type { DebounceOptions } from '../useDebounce/debounceOptions';\nimport useLatest from '../useLatest';\nimport useUnmount from '../useUnmount';\nimport { isFunction } from '../utils';\nimport isDev from '../utils/isDev';\n\ntype noop = (...args: any[]) => any;\n\nfunction useDebounceFn(fn: T, options?: DebounceOptions) {\n if (isDev) {\n if (!isFunction(fn)) {\n console.error(`useDebounceFn expected parameter is a function, got ${typeof fn}`);\n }\n }\n\n const fnRef = useLatest(fn);\n\n const wait = options?.wait ?? 1000;\n\n const debounced = useMemo(\n () =>\n debounce(\n (...args: Parameters): ReturnType => {\n return fnRef.current(...args);\n },\n wait,\n options,\n ),\n [],\n );\n\n useUnmount(() => {\n debounced.cancel();\n });\n\n return {\n run: debounced,\n cancel: debounced.cancel,\n flush: debounced.flush,\n };\n}\n\nexport default useDebounceFn;\n", "type": "registry:file", "target": "src/hooks/ahooks/useDebounceFn/index.ts" } ], "type": "registry:hook" }