Skip to content

Commit 87c2daa

Browse files
committed
main 🧊 add signal for use query
1 parent 6594829 commit 87c2daa

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

‎.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
'@typescript-eslint/no-floating-promises': 'off',
1616
'@typescript-eslint/no-unsafe-assignment': 'off',
1717
'@typescript-eslint/await-thenable': 'off',
18+
'@typescript-eslint/unbound-method': 'off',
1819
'jsx-a11y/label-has-associated-control': 'off',
1920
'no-nested-ternary': 'off',
2021
'promise/always-return': 'off',

‎src/hooks/useQuery/useQuery.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ export interface UseQueryOptions<QueryData, Data> {
2828
enabled?: boolean;
2929
}
3030

31+
interface UseQueryCallbackParams {
32+
/* The abort signal */
33+
signal: AbortSignal;
34+
/* The depends for the hook */
35+
keys: DependencyList;
36+
}
37+
3138
/* The use query return type */
3239
export interface UseQueryReturn<Data> {
3340
/* The state of the query */
@@ -44,6 +51,10 @@ export interface UseQueryReturn<Data> {
4451
refetch: () => void;
4552
/* The refetching state of the query */
4653
isRefetching: boolean;
54+
/* The abort function */
55+
abort: AbortController['abort'];
56+
/* The aborted state of the query */
57+
isAborted: boolean;
4758
}
4859

4960
/**
@@ -67,7 +78,7 @@ export interface UseQueryReturn<Data> {
6778
* const { data, isLoading, isError, isSuccess, error, refetch, isRefetching } = useQuery(() => fetch('url'));
6879
*/
6980
export const useQuery = <QueryData, Data = QueryData>(
70-
callback: () => Promise<QueryData>,
81+
callback: (params: UseQueryCallbackParams) => Promise<QueryData>,
7182
options?: UseQueryOptions<QueryData, Data>
7283
): UseQueryReturn<Data> => {
7384
const enabled = options?.enabled ?? true;
@@ -77,16 +88,28 @@ export const useQuery = <QueryData, Data = QueryData>(
7788
const [isError, setIsError] = useState(false);
7889
const [isRefetching, setIsRefetching] = useState(false);
7990
const [isSuccess, setIsSuccess] = useState(!!options?.initialData);
91+
const [isAborted, setIsAborted] = useState(!!options?.initialData);
8092

8193
const [error, setError] = useState<Error | undefined>(undefined);
8294
const [data, setData] = useState<Data | undefined>(options?.initialData);
8395

96+
const abortControllerRef = useRef<AbortController>(new AbortController());
8497
const intervalIdRef = useRef<ReturnType<typeof setInterval>>();
8598

99+
const keys = options?.keys ?? [];
100+
101+
const abort = () => {
102+
abortControllerRef.current.abort();
103+
abortControllerRef.current = new AbortController();
104+
abortControllerRef.current.signal.onabort = () => setIsAborted(true);
105+
};
106+
86107
const request = (action: 'init' | 'refetch') => {
108+
abort();
87109
setIsLoading(true);
110+
88111
if (action === 'refetch') setIsRefetching(true);
89-
callback()
112+
callback({ signal: abortControllerRef.current.signal, keys })
90113
.then((response) => {
91114
const data = options?.select ? options?.select(response) : response;
92115
options?.onSuccess?.(data as Data);
@@ -130,13 +153,13 @@ export const useQuery = <QueryData, Data = QueryData>(
130153
useDidUpdate(() => {
131154
if (!enabled) return;
132155
request('refetch');
133-
}, [enabled, ...(options?.keys ?? [])]);
156+
}, [enabled, ...keys]);
134157

135158
useEffect(() => {
136159
return () => {
137160
clearInterval(intervalIdRef.current);
138161
};
139-
}, [enabled, options?.refetchInterval, options?.retry, ...(options?.keys ?? [])]);
162+
}, [enabled, options?.refetchInterval, options?.retry, ...keys]);
140163

141164
const refetch = () => request('refetch');
142165

@@ -146,12 +169,14 @@ export const useQuery = <QueryData, Data = QueryData>(
146169
: options?.placeholderData;
147170

148171
return {
172+
abort,
149173
data: data ?? placeholderData,
150174
error,
151175
refetch,
152176
isLoading,
153177
isError,
154178
isSuccess,
155-
isRefetching
179+
isRefetching,
180+
isAborted
156181
};
157182
};

0 commit comments

Comments
 (0)