Skip to content

Commit

Permalink
update prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
xnimorz committed Nov 4, 2023
1 parent ac70c67 commit 6b568e8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 19 deletions.
6 changes: 5 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
22 changes: 18 additions & 4 deletions src/useDebounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,34 @@ function adjustFunctionValueOfSetState<T>(value: T): T | (() => T) {
}

function useStateIgnoreCallback<T>(initialState: T): [T, Dispatch<T>] {
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<T>(
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)) {
Expand Down
29 changes: 19 additions & 10 deletions src/useDebouncedCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends (...args: any) => ReturnType<T>> extends ControlFunctions {
export interface DebouncedState<T extends (...args: any) => ReturnType<T>>
extends ControlFunctions {
(...args: Parameters<T>): ReturnType<T> | undefined;
}

Expand Down Expand Up @@ -107,11 +108,9 @@ export interface DebouncedState<T extends (...args: any) => ReturnType<T>> exten
* // Check for pending invocations.
* const status = debounced.pending() ? "Pending..." : "Ready"
*/
export default function useDebouncedCallback<T extends (...args: any) => ReturnType<T>>(
func: T,
wait?: number,
options?: Options
): DebouncedState<T> {
export default function useDebouncedCallback<
T extends (...args: any) => ReturnType<T>,
>(func: T, wait?: number, options?: Options): DebouncedState<T> {
const lastCallTime = useRef(null);
const lastInvokeTime = useRef(0);
const timerId = useRef(null);
Expand Down Expand Up @@ -169,7 +168,9 @@ export default function useDebouncedCallback<T extends (...args: any) => 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) => {
Expand Down Expand Up @@ -214,7 +215,9 @@ export default function useDebouncedCallback<T extends (...args: any) => 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);
Expand Down Expand Up @@ -251,10 +254,16 @@ export default function useDebouncedCallback<T extends (...args: any) => 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 = () => {
Expand Down
9 changes: 7 additions & 2 deletions src/useThrottledCallback.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -52,7 +55,9 @@ import useDebouncedCallback, { CallOptions, DebouncedState } from './useDebounce
* // Cancel the trailing throttled invocation.
* window.addEventListener('popstate', throttled.cancel);
*/
export default function useThrottledCallback<T extends (...args: any) => ReturnType<T>>(
export default function useThrottledCallback<
T extends (...args: any) => ReturnType<T>,
>(
func: T,
wait: number,
{ leading = true, trailing = true }: CallOptions = {}
Expand Down

0 comments on commit 6b568e8

Please sign in to comment.