From 6b568e8c86fc8928cfb895a330d21131407ce3e7 Mon Sep 17 00:00:00 2001 From: Nik Date: Sat, 4 Nov 2023 14:17:14 +0000 Subject: [PATCH] update prettier --- .eslintrc | 6 +++++- package.json | 2 +- src/index.ts | 7 ++++++- src/useDebounce.ts | 22 ++++++++++++++++++---- src/useDebouncedCallback.ts | 29 +++++++++++++++++++---------- src/useThrottledCallback.ts | 9 +++++++-- 6 files changed, 56 insertions(+), 19 deletions(-) diff --git a/.eslintrc b/.eslintrc index a5382ab..772b25c 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,6 +1,10 @@ { "parser": "@typescript-eslint/parser", - "extends": ["plugin:@typescript-eslint/recommended", "prettier/@typescript-eslint", "plugin:prettier/recommended"], + "extends": [ + "plugin:@typescript-eslint/recommended", + "prettier", + "plugin:prettier/recommended" + ], "parserOptions": { "ecmaVersion": 2018, "sourceType": "module" diff --git a/package.json b/package.json index bc0d774..a5a712f 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "react-hook", "react" ], - "author": "Nikita Mostovoy (nik.mostovoy@gmail.com)", + "author": "Nik (nik@xnim.me)", "license": "MIT", "bugs": { "url": "https://github.com/xnimorz/use-debounce/issues" diff --git a/src/index.ts b/src/index.ts index e4ad47f..1dbefd0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,10 @@ import useDebounce from './useDebounce'; -import useDebouncedCallback, { CallOptions, ControlFunctions, DebouncedState, Options } from './useDebouncedCallback'; +import useDebouncedCallback, { + CallOptions, + ControlFunctions, + DebouncedState, + Options, +} from './useDebouncedCallback'; import useThrottledCallback from './useThrottledCallback'; export { diff --git a/src/useDebounce.ts b/src/useDebounce.ts index 926a6ed..52298a4 100644 --- a/src/useDebounce.ts +++ b/src/useDebounce.ts @@ -10,20 +10,34 @@ function adjustFunctionValueOfSetState(value: T): T | (() => T) { } function useStateIgnoreCallback(initialState: T): [T, Dispatch] { - const [state, setState] = useState(adjustFunctionValueOfSetState(initialState)); - const setStateIgnoreCallback = useCallback((value: T) => setState(adjustFunctionValueOfSetState(value)), []); + const [state, setState] = useState( + adjustFunctionValueOfSetState(initialState) + ); + const setStateIgnoreCallback = useCallback( + (value: T) => setState(adjustFunctionValueOfSetState(value)), + [] + ); return [state, setStateIgnoreCallback]; } export default function useDebounce( value: T, delay: number, - options?: { maxWait?: number; leading?: boolean; trailing?: boolean; equalityFn?: (left: T, right: T) => boolean } + options?: { + maxWait?: number; + leading?: boolean; + trailing?: boolean; + equalityFn?: (left: T, right: T) => boolean; + } ): [T, DebouncedState<(value: T) => void>] { const eq = (options && options.equalityFn) || valueEquality; const [state, dispatch] = useStateIgnoreCallback(value); - const debounced = useDebouncedCallback(useCallback((value: T) => dispatch(value), [dispatch]), delay, options); + const debounced = useDebouncedCallback( + useCallback((value: T) => dispatch(value), [dispatch]), + delay, + options + ); const previousValue = useRef(value); if (!eq(previousValue.current, value)) { diff --git a/src/useDebouncedCallback.ts b/src/useDebouncedCallback.ts index 50be548..42c3f00 100644 --- a/src/useDebouncedCallback.ts +++ b/src/useDebouncedCallback.ts @@ -37,7 +37,8 @@ export interface ControlFunctions { * Subsequent calls to the debounced function `debounced.callback` return the result of the last func invocation. * Note, that if there are no previous invocations it's mean you will get undefined. You should check it in your code properly. */ -export interface DebouncedState ReturnType> extends ControlFunctions { +export interface DebouncedState ReturnType> + extends ControlFunctions { (...args: Parameters): ReturnType | undefined; } @@ -107,11 +108,9 @@ export interface DebouncedState ReturnType> exten * // Check for pending invocations. * const status = debounced.pending() ? "Pending..." : "Ready" */ -export default function useDebouncedCallback ReturnType>( - func: T, - wait?: number, - options?: Options -): DebouncedState { +export default function useDebouncedCallback< + T extends (...args: any) => ReturnType, +>(func: T, wait?: number, options?: Options): DebouncedState { const lastCallTime = useRef(null); const lastInvokeTime = useRef(0); const timerId = useRef(null); @@ -169,7 +168,9 @@ export default function useDebouncedCallback ReturnT const startTimer = (pendingFunc: () => void, wait: number) => { if (useRAF) cancelAnimationFrame(timerId.current); - timerId.current = useRAF ? requestAnimationFrame(pendingFunc) : setTimeout(pendingFunc, wait); + timerId.current = useRAF + ? requestAnimationFrame(pendingFunc) + : setTimeout(pendingFunc, wait); }; const shouldInvoke = (time: number) => { @@ -214,7 +215,9 @@ export default function useDebouncedCallback ReturnT const timeSinceLastCall = time - lastCallTime.current; const timeSinceLastInvoke = time - lastInvokeTime.current; const timeWaiting = wait - timeSinceLastCall; - const remainingWait = maxing ? Math.min(timeWaiting, maxWait - timeSinceLastInvoke) : timeWaiting; + const remainingWait = maxing + ? Math.min(timeWaiting, maxWait - timeSinceLastInvoke) + : timeWaiting; // Restart the timer startTimer(timerExpired, remainingWait); @@ -251,10 +254,16 @@ export default function useDebouncedCallback ReturnT func.cancel = () => { if (timerId.current) { - useRAF ? cancelAnimationFrame(timerId.current) : clearTimeout(timerId.current); + useRAF + ? cancelAnimationFrame(timerId.current) + : clearTimeout(timerId.current); } lastInvokeTime.current = 0; - lastArgs.current = lastCallTime.current = lastThis.current = timerId.current = null; + lastArgs.current = + lastCallTime.current = + lastThis.current = + timerId.current = + null; }; func.isPending = () => { diff --git a/src/useThrottledCallback.ts b/src/useThrottledCallback.ts index 0c111eb..561cac3 100644 --- a/src/useThrottledCallback.ts +++ b/src/useThrottledCallback.ts @@ -1,4 +1,7 @@ -import useDebouncedCallback, { CallOptions, DebouncedState } from './useDebouncedCallback'; +import useDebouncedCallback, { + CallOptions, + DebouncedState, +} from './useDebouncedCallback'; /** * Creates a throttled function that only invokes `func` at most once per @@ -52,7 +55,9 @@ import useDebouncedCallback, { CallOptions, DebouncedState } from './useDebounce * // Cancel the trailing throttled invocation. * window.addEventListener('popstate', throttled.cancel); */ -export default function useThrottledCallback ReturnType>( +export default function useThrottledCallback< + T extends (...args: any) => ReturnType, +>( func: T, wait: number, { leading = true, trailing = true }: CallOptions = {}