-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse-query.ts
216 lines (194 loc) · 6.13 KB
/
use-query.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
type QueryContext<TKey extends readonly unknown[]> = {
/**
* Query key associated with the current query function invocation.
*/
readonly queryKey: TKey;
/**
* Abort signal provided by the query hook. Aborted when a new query begins
* or the component is unmounted.
*/
readonly signal?: AbortSignal;
};
type QueryOptions = {
/**
* Default: true
*/
readonly enabled?: boolean;
/**
* Default: 0 (no auto-refresh).
*/
readonly refetchInterval?: number;
/**
* Default: true
*/
readonly refetchOnReconnect?: boolean;
/**
* Default: true
*/
readonly refetchOnWindowFocus?: boolean;
};
type QueryRefetchOptions = {
/**
* Per default, a currently running request will be cancelled before a new
* request is made. When set to false, no refetch will be made if there is
* already a request running.
*/
cancelRefetch?: boolean;
};
type QueryResult<TData> = {
/**
* Most recent successful query data. Only cleared if the query key changes.
*/
readonly data: TData | undefined;
/**
* Error thrown if the most recent query failed. Cleared when a subsequent
* query succeeds.
*/
readonly error: unknown;
/**
* True when the query function has been called and the returned promise is
* still pending.
*/
readonly isFetching: boolean;
/**
* Immediately cause the query to be refetched, even if the query is not
* currently enabled.
*/
refetch(options?: QueryRefetchOptions): void;
};
type QueryFn<TData = unknown, TKey extends readonly unknown[] = readonly unknown[]> = (
ctx: QueryContext<TKey>,
) => Promise<TData>;
const useStableQueryKey = <TKey extends readonly unknown[]>(queryKey: TKey): TKey => {
const serialized = JSON.stringify(queryKey, (_, value: Record<string, unknown>) => {
return Object.prototype.toString.call(value) === '[object Object]'
? Object.keys(value)
.sort()
.reduce((result: Record<string, unknown>, key) => {
result[key] = value[key];
return result;
}, {})
: value;
});
return useMemo(() => JSON.parse(serialized), [serialized]);
};
/**
* A minimal asynchronous data read hook.
*
* This hook is suitable for
* [safe](https://developer.mozilla.org/en-US/docs/Glossary/Safe/HTTP)
* operations which should not have any side effects (eg. GET, HEAD, OPTIONS,
* and TRACE requests)
*
* Inspired by (and API compatible with) React Query's
* [useQuery](https://tanstack.com/query/v4/docs/reference/useQuery) hook.
*
* Implements the following React Query `useQuery` options:
* - `enabled`
* - `refetchInterval`
* - `refetchOnReconnect`
* - `refetchOnWindowFocus`
*
* Implements the following React Query `useQuery` response properties:
* - `data`
* - `error`
* - `isFetching`
* - `refetch()`
*
* Implements the following React Query `queryFn` context properties:
* - `queryKey`
* - `signal`
*/
const useQuery = <TData, TKey extends readonly unknown[]>(
queryKey: TKey,
queryFn: QueryFn<TData, TKey>,
queryOptions: QueryOptions = {},
): QueryResult<TData> => {
const stableQueryKey = useStableQueryKey(queryKey);
const { enabled = true, refetchOnReconnect = true, refetchOnWindowFocus = true } = queryOptions;
const refetchInterval = queryOptions.refetchInterval || 0;
const [data, setData] = useState<TData | undefined>();
const [error, setError] = useState<unknown>();
const [isFetching, setIsFetching] = useState(false);
const queryFnRef = useRef(queryFn);
const abortControllerRef = useRef<AbortController>();
const refetch = useCallback(
({ cancelRefetch = true }: QueryRefetchOptions = {}) => {
if (!cancelRefetch && abortControllerRef.current && !abortControllerRef.current.signal.aborted) {
return;
}
const controller = new AbortController();
abortControllerRef.current?.abort();
abortControllerRef.current = controller;
setIsFetching(true);
queryFnRef
.current({ queryKey: stableQueryKey, signal: controller.signal })
.finally(() => {
if (controller === abortControllerRef.current) {
abortControllerRef.current = undefined;
}
})
.then((newData) => {
if (!controller.signal.aborted) {
setData(newData);
setError(undefined);
setIsFetching(false);
}
})
.catch((newError) => {
if (!controller.signal.aborted) {
setError(newError);
setIsFetching(false);
}
});
},
[stableQueryKey],
);
useEffect(() => {
queryFnRef.current = queryFn;
});
// Clear data and error if the key changes
useEffect(() => {
setData(undefined);
setError(undefined);
}, [stableQueryKey]);
// Fetch on mount and re-fetch on update
useEffect(() => {
if (enabled) {
refetch();
}
}, [enabled, refetch]);
// Fetch on interval if set.
useEffect(() => {
if (enabled && refetchInterval > 0) {
const interval = setInterval(() => refetch({ cancelRefetch: false }), refetchInterval);
return () => clearInterval(interval);
}
}, [enabled, refetch, refetchInterval]);
// Fetch on reconnect
useEffect(() => {
if (enabled && refetchOnReconnect) {
const onOnline = (): void => refetch({ cancelRefetch: false });
window.addEventListener('online', onOnline);
return () => window.removeEventListener('online', onOnline);
}
}, [enabled, refetchOnReconnect, refetch]);
// Fetch on window focus
useEffect(() => {
if (enabled && refetchOnWindowFocus) {
const onFocus = (): void => refetch({ cancelRefetch: false });
window.addEventListener('focus', onFocus);
return () => window.removeEventListener('focus', onFocus);
}
}, [enabled, refetchOnWindowFocus, refetch]);
// Abort active query and prevent state changes after unmount
useEffect(() => {
return () => {
abortControllerRef.current?.abort();
};
}, []);
return { data, error, isFetching, refetch };
};
export type { QueryContext, QueryFn, QueryOptions, QueryResult };
export { useQuery, useStableQueryKey };