mirror of
https://github.com/SigNoz/signoz.git
synced 2025-12-22 18:06:35 +00:00
35 lines
766 B
TypeScript
35 lines
766 B
TypeScript
|
|
import { useCallback } from 'react';
|
||
|
|
import debounce from 'lodash-es/debounce';
|
||
|
|
|
||
|
|
export interface DebouncedFunc<T extends (...args: any[]) => any> {
|
||
|
|
(...args: Parameters<T>): ReturnType<T> | undefined;
|
||
|
|
|
||
|
|
cancel(): void;
|
||
|
|
|
||
|
|
flush(): ReturnType<T> | undefined;
|
||
|
|
}
|
||
|
|
|
||
|
|
export type DebounceOptions = {
|
||
|
|
leading?: boolean;
|
||
|
|
maxWait?: number;
|
||
|
|
trailing?: boolean;
|
||
|
|
};
|
||
|
|
|
||
|
|
const defaultOptions: DebounceOptions = {
|
||
|
|
leading: false,
|
||
|
|
trailing: true,
|
||
|
|
};
|
||
|
|
|
||
|
|
const useDebouncedFn = <T extends (...args: any) => any>(
|
||
|
|
fn: T,
|
||
|
|
wait: number = 100,
|
||
|
|
options: DebounceOptions = defaultOptions,
|
||
|
|
dependencies?: ReadonlyArray<any>,
|
||
|
|
): DebouncedFunc<T> => {
|
||
|
|
const debounced = debounce(fn, wait, options);
|
||
|
|
|
||
|
|
return useCallback(debounced, dependencies || []);
|
||
|
|
};
|
||
|
|
|
||
|
|
export default useDebouncedFn;
|