-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
maxWait
parameter to useDebouncedCallback
hook
- Loading branch information
Showing
4 changed files
with
117 additions
and
25 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
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 |
---|---|---|
@@ -1,40 +1,85 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { DependencyList, useMemo, useRef } from 'react'; | ||
import { useUnmountEffect } from '../useUnmountEffect/useUnmountEffect'; | ||
|
||
export interface IDebouncedFunction<Args extends any[], This> { | ||
(this: This, ...args: Args): void; | ||
} | ||
|
||
/** | ||
* Makes passed function debounced, otherwise acts like `useCallback`. | ||
* | ||
* @param cb Function that will be debounced. | ||
* @param callback Function that will be debounced. | ||
* @param delay Debounce delay. | ||
* @param deps Dependencies list when to update callback. | ||
* @param maxWait Maximum amount of milliseconds that function can be delayed | ||
* before it's force execution. 0 means no max wait. | ||
*/ | ||
export function useDebouncedCallback<T extends (...args: any[]) => any>( | ||
cb: T, | ||
export function useDebouncedCallback<Args extends any[], This>( | ||
callback: (this: This, ...args: Args) => any, | ||
delay: number, | ||
deps: DependencyList | ||
): (...args: Parameters<T>) => void { | ||
deps: DependencyList, | ||
maxWait = 0 | ||
): IDebouncedFunction<Args, This> { | ||
const timeout = useRef<ReturnType<typeof setTimeout>>(); | ||
const waitTimeout = useRef<ReturnType<typeof setTimeout>>(); | ||
const lastCall = useRef<{ args: Args; this: This }>(); | ||
|
||
const clear = () => { | ||
if (timeout.current) { | ||
clearTimeout(timeout.current); | ||
timeout.current = undefined; | ||
} | ||
|
||
if (waitTimeout.current) { | ||
clearTimeout(waitTimeout.current); | ||
waitTimeout.current = undefined; | ||
} | ||
}; | ||
|
||
// cancel scheduled execution on unmount | ||
useUnmountEffect(clear); | ||
|
||
return useMemo( | ||
() => { | ||
// eslint-disable-next-line func-names | ||
const debounced = function (...args: Parameters<T>): void { | ||
if (timeout.current) clearTimeout(timeout.current); | ||
const execute = () => { | ||
// barely possible to test this line | ||
/* istanbul ignore next */ | ||
if (!lastCall.current) return; | ||
|
||
timeout.current = setTimeout(() => { | ||
timeout.current = undefined; | ||
const context = lastCall.current; | ||
lastCall.current = undefined; | ||
|
||
cb(...args); | ||
}, delay); | ||
callback.apply(context.this, context.args); | ||
|
||
clear(); | ||
}; | ||
|
||
Object.defineProperties(debounced, { | ||
length: { value: cb.length }, | ||
name: { value: `${cb.name || 'anonymous'}__debounced__${delay}` }, | ||
// eslint-disable-next-line func-names | ||
const wrapped = function (this, ...args) { | ||
if (timeout.current) { | ||
clearTimeout(timeout.current); | ||
} | ||
|
||
lastCall.current = { args, this: this }; | ||
|
||
// plan regular execution | ||
timeout.current = setTimeout(execute, delay); | ||
|
||
// plan maxWait execution if required | ||
if (maxWait > 0 && !waitTimeout.current) { | ||
waitTimeout.current = setTimeout(execute, maxWait); | ||
} | ||
} as IDebouncedFunction<Args, This>; | ||
|
||
Object.defineProperties(wrapped, { | ||
length: { value: callback.length }, | ||
name: { value: `${callback.name || 'anonymous'}__debounced__${delay}` }, | ||
}); | ||
|
||
return debounced; | ||
return wrapped; | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[delay, ...deps] | ||
[delay, maxWait, ...deps] | ||
); | ||
} |