@modern-kit/react
Version:
32 lines (29 loc) • 1.64 kB
TypeScript
import { throttle } from '@modern-kit/utils';
type ThrottleParameters = Parameters<typeof throttle>;
type ThrottleReturnType<T extends (...args: any) => any> = ReturnType<typeof throttle<T>>;
/**
* @description 주어진 콜백 함수를 지정된 시간 동안 쓰로틀링 처리하여 특정 시간 동안 반복 호출을 방지하는 훅입니다.
*
* @param {ThrottleParameters[0]} callback - 쓰로틀링할 콜백 함수입니다.
* @param {ThrottleParameters[1]} wait - 쓰로틀이 적용될 시간(ms)입니다.
* @param {ThrottleParameters[2]} [options={}] - 쓰로틀 동작에 영향을 주는 추가 옵션입니다.
* - `signal` 옵션을 통해 쓰로틀링 된 함수를 중단할 수 있습니다.
* - `leading` 옵션을 통해 초기 호출을 실행할지 여부를 설정할 수 있습니다.
* - `trailing` 옵션을 통해 마지막 호출을 실행할지 여부를 설정할 수 있습니다.
*
* @returns {ThrottleReturnType<T>} 쓰로틀링 된 함수가 반환됩니다.
*
* @example
* const throttledFunction = useThrottle(callback, 300);
* throttledFunction(); // 쓰로틀링 된 콜백이 실행됩니다.
*
* @example
* // AbortSignal 사용 예시
* const controller = new AbortController();
* controller.abort(); // 쓰로틀링된 함수 호출을 취소합니다.
*
* const throttled = useThrottle(callback, 300, { signal: controller.signal });
* throttled(); // 호출되지 않습니다.
*/
declare function useThrottle<T extends ThrottleParameters[0]>(callback: T, wait: ThrottleParameters[1], options?: ThrottleParameters[2]): ThrottleReturnType<T>;
export { useThrottle };