-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse-query.ts
189 lines (174 loc) · 5.82 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
import { useState, useRef, useCallback, useEffect } from "react";
import { equal } from "@wry/equality";
export type Variables = Record<string, any>;
const useMemoWhenEqual = <T>(value: T) => {
const ref = useRef(value);
if (!equal(ref.current, value)) {
ref.current = value;
}
return ref.current;
};
export interface QueryOptions<TData> {
skip?: boolean;
onCompleted?: (data: TData) => void;
onError?: (error: any) => void;
}
export interface QueryOptionsWithVariables<TData, TVariables extends Variables>
extends QueryOptions<TData> {
variables: TVariables;
}
export type BaseQueryResult<TData> = {
loading: boolean;
error: any;
data: TData | null;
previousData: TData | null;
};
export interface QueryResult<TData, TVariables extends Variables = Variables>
extends BaseQueryResult<TData> {
refetch: (
variables?: Partial<TVariables> | undefined
) => Promise<QueryResult<TData, TVariables>>;
}
/**
* Mirrors the functionality of
* [Apollo's useQuery hook](https://www.apollographql.com/docs/react/data/queries/#usequery-api),
* but with a "query" being any async function rather than GQL statement.
*/
function useQuery<TData>(
query: () => Promise<TData>,
options?: QueryOptions<TData>
): QueryResult<TData, never>;
function useQuery<TData, TVariables extends Variables>(
query: (variables: TVariables) => Promise<TData>,
options: QueryOptionsWithVariables<TData, TVariables>
): QueryResult<TData, TVariables>;
function useQuery<TData, TVariables extends Variables>(
query: (variables?: TVariables) => Promise<TData>,
options?: QueryOptionsWithVariables<TData, TVariables> | QueryOptions<TData>
): QueryResult<TData, TVariables> {
const { skip, onCompleted, onError } = options || {};
const data = useRef<TData | null>(null);
const previousData = useRef<TData | null>(null);
const loading = useRef<boolean>(!skip);
const error = useRef<any>(null);
const cancelLast = useRef<() => void>();
const [, forceUpdate] = useState(0);
const passedVariables =
options && "variables" in options ? options.variables : undefined;
const variables = useMemoWhenEqual(passedVariables);
const fetch: (
refetchVariables?: Partial<TVariables> | undefined,
internalOptions?: { skipPreviousData?: boolean }
) => Promise<QueryResult<TData, TVariables>> = useCallback(
async (
refetchVariables: Partial<TVariables> | undefined,
internalOptions
) => {
const mergedVariables = refetchVariables
? Object.assign({}, variables, refetchVariables)
: variables;
cancelLast.current?.();
let isLatest = true;
if (!internalOptions?.skipPreviousData) {
previousData.current = data.current;
data.current = null;
loading.current = true;
error.current = null;
}
cancelLast.current = () => {
isLatest = false;
};
return query(...(mergedVariables ? [mergedVariables] : []))
.then((response) => {
if (isLatest) {
data.current = response;
loading.current = false;
error.current = null;
forceUpdate((x) => x + 1);
onCompleted?.(response);
}
return {
loading: loading.current,
error: error.current,
data: data.current,
previousData: previousData.current,
refetch: (refetchVariables?: Partial<TVariables> | undefined) => {
const result = fetch(refetchVariables);
forceUpdate((x) => x + 1);
return result;
},
};
})
.catch((e) => {
if (isLatest) {
loading.current = false;
error.current = e;
forceUpdate((x) => x + 1);
onError?.(e);
}
return {
loading: loading.current,
error: e,
data: data.current,
previousData: previousData.current,
refetch: (refetchVariables?: Partial<TVariables> | undefined) => {
const result = fetch(refetchVariables);
forceUpdate((x) => x + 1);
return result;
},
};
});
},
[query, variables, onCompleted, onError]
);
const fetchChanged = useHasChanged(fetch);
const skipChanged = useHasChanged(skip);
const shouldRunFetch = (skipChanged || fetchChanged) && !skip;
const willRunFetch = useRef(false);
if (shouldRunFetch && !willRunFetch.current) {
previousData.current = data.current;
data.current = null;
loading.current = true;
error.current = null;
willRunFetch.current = true;
}
useEffect(() => {
// willRunFetch works around the double invocation of useEffect in React.StrictMode
if (shouldRunFetch && willRunFetch.current) {
fetch(undefined, { skipPreviousData: true });
willRunFetch.current = false;
}
});
const refetch = useCallback(
(refetchVariables?: Partial<TVariables> | undefined) => {
const result = fetch(refetchVariables);
forceUpdate((x) => x + 1);
return result;
},
[fetch]
);
return {
loading: loading.current,
error: error.current,
data: data.current,
previousData: previousData.current,
refetch,
};
}
// https://stackoverflow.com/a/68174829/1582783
const useHasChanged = <T>(value: T) => {
const prevValue = usePrevious<T>(value);
return prevValue !== value;
};
// Note: in React.StrictMode under React 18, a functional component is invoked twice.
// Both invocations will occure before resolving the useEffect.
// This means sequential calls to usePrevious in the same render (before useEffect has resolved) can all return true.
// We work around this by using `willRunFetch` above.
const usePrevious = <T>(value: T) => {
const ref = useRef<T>();
useEffect(() => {
ref.current = value;
});
return ref.current;
};
export { useQuery };