Skip to content

Commit

Permalink
fix: improve type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
martinstark committed Feb 18, 2021
1 parent 2c8db82 commit 537ee1d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# throttle-ts

Correctly typed, generic, typescript throttle function
Correctly typed, generic, typescript throttle function.

Yields the return value of the throttled function.
Yields the return value of the throttled function, or undefined when throttled/cancelled.

The throttled function keeps the type signature of the original function, plus `void`.

Returns a cancel function which enables cleanup of the timeout, and blocks future calls to the throttled function. Useful when unmounting react/view components.

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export const throttle = <T extends (...args: any) => any>(
fn: T,
delay: number
): [T | ((...args: any) => void), () => void] => {
): [T | (() => void), () => void] => {
let wait = false;
let timeout: undefined | number;
let cancelled = false;

return [
(...args) => {
(...args: any[]) => {
if (cancelled) return undefined;
if (wait) return undefined;

Expand Down

0 comments on commit 537ee1d

Please sign in to comment.