Skip to content

Commit

Permalink
fix: remove any types in useThrottleFn
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich authored Nov 12, 2019
2 parents d2cc1b6 + 61e29cd commit bb5baea
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/useThrottleFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ import { useEffect, useRef, useState } from 'react';
import useUnmount from './useUnmount';

const useThrottleFn = <T, U extends any[]>(fn: (...args: U) => T, ms: number = 200, args: U) => {
const [state, setState] = useState<T>(null as any);
const [state, setState] = useState<T | null>(null);
const timeout = useRef<ReturnType<typeof setTimeout>>();
const nextArgs = useRef(null) as any;
const hasNextArgs = useRef(false) as any;
const nextArgs = useRef<U>();

useEffect(() => {
if (!timeout.current) {
setState(fn(...args));
const timeoutCallback = () => {
if (hasNextArgs.current) {
hasNextArgs.current = false;
if (nextArgs.current) {
setState(fn(...nextArgs.current));
nextArgs.current = undefined;
timeout.current = setTimeout(timeoutCallback, ms);
} else {
timeout.current = undefined;
Expand All @@ -22,7 +21,6 @@ const useThrottleFn = <T, U extends any[]>(fn: (...args: U) => T, ms: number = 2
timeout.current = setTimeout(timeoutCallback, ms);
} else {
nextArgs.current = args;
hasNextArgs.current = true;
}
}, args);

Expand Down

0 comments on commit bb5baea

Please sign in to comment.