Skip to content

Commit b1521b7

Browse files
committed
feat: deprecate debounce function and introduce useDebounce hook for improved performance
1 parent 7395c1a commit b1521b7

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/Common/Helper.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ export const applyCompareDiffOnUneditedDocument = (uneditedDocument: object, edi
679679

680680
/**
681681
* Returns a debounced variant of the function
682+
* @deprecated - It should use useRef instead, pls use useDebounce
682683
*/
683684
export const debounce = (func, timeout = 500) => {
684685
let timer
@@ -693,6 +694,17 @@ export const debounce = (func, timeout = 500) => {
693694
}
694695
}
695696

697+
export const useDebounce = <Callback extends (...args: any[]) => void>(cb: Callback, delay: number) => {
698+
const timeoutId = useRef(null)
699+
700+
return (...args: Parameters<Callback>) => {
701+
if (timeoutId.current) {
702+
clearTimeout(timeoutId.current)
703+
}
704+
timeoutId.current = setTimeout(() => cb(...args), delay)
705+
}
706+
}
707+
696708
/**
697709
* Returns a capitalized string with first letter in uppercase and rest in lowercase
698710
*/

0 commit comments

Comments
 (0)