Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rustam-cb committed Jan 30, 2025
1 parent 56454fd commit 95467db
Showing 1 changed file with 1 addition and 30 deletions.
31 changes: 1 addition & 30 deletions src/internal/hooks/useThrottle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,13 @@ type AnyFunction = (...args: unknown[]) => unknown;
* A hook that returns a throttled version of a callback function.
* Throttling ensures the callback is executed at most once within the specified delay period.
*
* Behavior:
* 1. First call is always executed immediately
* 2. Subsequent calls within the delay period are handled as follows:
* - If no delayed execution is scheduled, schedules one with the latest arguments
* - If a delayed execution is already scheduled, updates it with the latest arguments
* 3. After the delay period passes, the next call will execute immediately
*
* Example timing with 1000ms delay:
* ```
* 0ms: first call → executes immediately
* 100ms: second call → schedules execution for 1000ms
* 300ms: third call → ignored (already scheduled)
* 1000ms: scheduled execution happens
* 1100ms: fourth call → executes immediately (delay passed)
* ```
*
* @param callback - The function to throttle
* @param delay - The number of milliseconds to wait before allowing another execution
*
* @returns A throttled version of the callback that maintains the same arguments
* and automatically cleans up any pending executions on unmount
*
* @example
* ```tsx
* const throttledFetch = useThrottle((value: string) => {
* // This will execute immediately once,
* // then wait 1000ms before allowing another execution
* fetchData(value);
* }, 1000);
*
* // Usage
* throttledFetch('search'); // executes immediately
* throttledFetch('se'); // schedules execution
* throttledFetch('s'); // updates scheduled execution with 's'
* // After 1000ms: executes with latest value ('s')
* ```
* More details on throttle: https://developer.mozilla.org/en-US/docs/Glossary/Throttle
*/
export const useThrottle = <T extends AnyFunction>(
callback: T,
Expand Down

0 comments on commit 95467db

Please sign in to comment.