import { useCallback, useRef } from 'react'
export function useLockFn<P extends any[] = any[], V = any>(
fn: (...args: P) => Promise<V>,
) {
const lockRef = useRef(false)
return useCallback(
async (...args: P) => {
if (lockRef.current) {
return
}
lockRef.current = true
try {
return await fn(...args)
} finally {
lockRef.current = false
}
},
[fn],
)
}
npx shadcn@latest add @hooks/use-lock-fnimport { UseLockFn } from "@/hooks/use-lock-fn"const value = UseLockFn()