-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d27c882
commit 2f54c10
Showing
4 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { useEffect, useMemo, useRef } from "react"; | ||
import debounce from "lodash.debounce"; | ||
|
||
import { useUnmount } from "./useUnmounts"; | ||
|
||
type DebounceOptions = { | ||
leading?: boolean; | ||
trailing?: boolean; | ||
maxWait?: number; | ||
}; | ||
|
||
type ControlFunctions = { | ||
cancel: () => void; | ||
flush: () => void; | ||
isPending: () => boolean; | ||
}; | ||
|
||
export type DebouncedState<T extends (...args: any) => ReturnType<T>> = (( | ||
...args: Parameters<T> | ||
) => ReturnType<T> | undefined) & | ||
ControlFunctions; | ||
|
||
export function useDebounceCallback<T extends (...args: any) => ReturnType<T>>( | ||
func: T, | ||
delay = 500, | ||
options?: DebounceOptions, | ||
): DebouncedState<T> { | ||
const debouncedFunc = useRef<ReturnType<typeof debounce>>(); | ||
|
||
useUnmount(() => { | ||
if (debouncedFunc.current) { | ||
debouncedFunc.current.cancel(); | ||
} | ||
}); | ||
|
||
const debounced = useMemo(() => { | ||
const debouncedFuncInstance = debounce(func, delay, options); | ||
|
||
const wrappedFunc: DebouncedState<T> = (...args: Parameters<T>) => { | ||
return debouncedFuncInstance(...args); | ||
}; | ||
|
||
wrappedFunc.cancel = () => { | ||
debouncedFuncInstance.cancel(); | ||
}; | ||
|
||
wrappedFunc.isPending = () => { | ||
return !!debouncedFunc.current; | ||
}; | ||
|
||
wrappedFunc.flush = () => { | ||
return debouncedFuncInstance.flush(); | ||
}; | ||
|
||
return wrappedFunc; | ||
}, [func, delay, options]); | ||
|
||
useEffect(() => { | ||
debouncedFunc.current = debounce(func, delay, options); | ||
}, [func, delay, options]); | ||
|
||
return debounced; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
export function useUnmount(func: () => void) { | ||
const funcRef = useRef(func); | ||
|
||
funcRef.current = func; | ||
|
||
useEffect( | ||
() => () => { | ||
funcRef.current(); | ||
}, | ||
[], | ||
); | ||
} |