Skip to content

Commit

Permalink
refactor: update useDebounced
Browse files Browse the repository at this point in the history
  • Loading branch information
macjuul committed Apr 24, 2024
1 parent 29bbe48 commit 149b0e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/hooks/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { useEffect, useRef } from "react";
import { useStable } from "./stable";

/**
* Execute a callback after a delay, if the callback is called again before the delay is over, the delay is reset.
* Similar to useDebouncedCallback, however this hook will pass arguments to the callback
*
* @param delay The delay in milliseconds
* @param exec The callback to execute
* @param delay The delay in milliseconds
* @returns The debounced callback
*/
export function useDebounced<T>(delay: number, exec: (value: T) => void): (value: T) => void {
export function useDebouncedFunction<F extends (...args: any) => any>(callback: F, delay: number): (...value: Parameters<F>) => void {
const task = useRef<any>(null);

useEffect(() => {
Expand All @@ -19,14 +19,14 @@ export function useDebounced<T>(delay: number, exec: (value: T) => void): (value
};
}, []);

return useStable((value) => {
return useStable((...value) => {
if (task.current) {
clearTimeout(task.current);
}

task.current = setTimeout(() => {
task.current = null;
exec(value);
callback(...value);

Check failure on line 29 in src/hooks/debounce.ts

View workflow job for this annotation

GitHub Actions / check_and_test

Type 'Parameters<F>' must have a '[Symbol.iterator]()' method that returns an iterator.

Check failure on line 29 in src/hooks/debounce.ts

View workflow job for this annotation

GitHub Actions / Build Web (18)

Type 'Parameters<F>' must have a '[Symbol.iterator]()' method that returns an iterator.
}, delay);
});
}
6 changes: 3 additions & 3 deletions src/views/query/QueryPane/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import autoFixAnim from "~/assets/animation/autofix.json";
import { useStable } from "~/hooks/stable";
import { ContentPane } from "~/components/Pane";
import { useDebounced } from "~/hooks/debounce";
import { useDebouncedFunction } from "~/hooks/debounce";
import { CodeEditor } from "~/components/CodeEditor";
import { ActionIcon, Group, Stack, Tooltip } from "@mantine/core";
import { useConfigStore } from '~/stores/config';
Expand Down Expand Up @@ -61,7 +61,7 @@ export function QueryPane({
});
});

const scheduleSetQuery = useDebounced(200, setQueryForced);
const scheduleSetQuery = useDebouncedFunction(setQueryForced, 200);

const handleFormat = useStable(() => {
updateQueryTab({
Expand Down Expand Up @@ -104,7 +104,7 @@ export function QueryPane({
});
});

const setSelection = useDebounced(350, onSelectionChange);
const setSelection = useDebouncedFunction(onSelectionChange, 350);

useIntent("format-query", handleFormat);
useIntent("infer-variables", inferVariables);
Expand Down

0 comments on commit 149b0e7

Please sign in to comment.