-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathuseAsync.tsx
348 lines (318 loc) · 9.97 KB
/
useAsync.tsx
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import React, { useCallback, useDebugValue, useEffect, useMemo, useRef, useReducer } from "react"
import globalScope, { MockAbortController, noop } from "./globalScope"
import {
neverSettle,
ActionTypes,
init,
dispatchMiddleware,
reducer as asyncReducer,
} from "./reducer"
import {
AsyncAction,
AsyncOptions,
AsyncState,
AbstractState,
PromiseFn,
Meta,
AsyncInitial,
AsyncFulfilled,
AsyncPending,
AsyncRejected,
} from "./types"
/**
* Due to https://github.com/microsoft/web-build-tools/issues/1050, we need
* AbstractState imported in this file, even though it is only used implicitly.
* This _uses_ AbstractState so it is not accidentally removed by someone.
*/
declare type ImportWorkaround<T> =
| AbstractState<T>
| AsyncInitial<T>
| AsyncFulfilled<T>
| AsyncPending<T>
| AsyncRejected<T>
export interface FetchOptions<T> extends AsyncOptions<T> {
defer?: boolean
json?: boolean
}
function useAsync<T>(options: AsyncOptions<T>): AsyncState<T>
function useAsync<T>(promiseFn: PromiseFn<T>, options?: AsyncOptions<T>): AsyncState<T>
function useAsync<T>(arg1: AsyncOptions<T> | PromiseFn<T>, arg2?: AsyncOptions<T>): AsyncState<T> {
const options: AsyncOptions<T> =
typeof arg1 === "function"
? {
...arg2,
promiseFn: arg1,
}
: arg1
const counter = useRef(0)
const isMounted = useRef(true)
const lastArgs = useRef<any[] | undefined>(undefined)
const lastOptions = useRef<AsyncOptions<T>>(options)
const lastPromise = useRef<Promise<T>>(neverSettle)
const abortController = useRef<AbortController>(new MockAbortController())
const { devToolsDispatcher } = globalScope.__REACT_ASYNC__
const { reducer, dispatcher = devToolsDispatcher } = options
const [state, _dispatch] = useReducer(
reducer
? (state: AsyncState<T>, action: AsyncAction<T>) => reducer(state, action, asyncReducer)
: asyncReducer,
options,
init
)
const dispatch = useCallback(
dispatcher
? action => dispatcher(action, dispatchMiddleware(_dispatch), lastOptions.current)
: dispatchMiddleware(_dispatch),
[dispatcher]
)
const { debugLabel } = options
const getMeta: <M extends Meta = Meta>(meta?: M) => M = useCallback(
(meta?) =>
({
counter: counter.current,
promise: lastPromise.current,
debugLabel,
...meta,
} as any),
[debugLabel]
)
const setData = useCallback(
(data, callback = noop) => {
if (isMounted.current) {
dispatch({
type: ActionTypes.fulfill,
payload: data,
meta: getMeta(),
})
callback()
}
return data
},
[dispatch, getMeta]
)
const setError = useCallback(
(error, callback = noop) => {
if (isMounted.current) {
dispatch({
type: ActionTypes.reject,
payload: error,
error: true,
meta: getMeta(),
})
callback()
}
return error
},
[dispatch, getMeta]
)
const { onResolve, onReject } = options
const handleResolve = useCallback(
count => (data: T) =>
count === counter.current && setData(data, () => onResolve && onResolve(data)),
[setData, onResolve]
)
const handleReject = useCallback(
count => (err: Error) =>
count === counter.current && setError(err, () => onReject && onReject(err)),
[setError, onReject]
)
const start = useCallback(
promiseFn => {
if ("AbortController" in globalScope) {
abortController.current.abort()
abortController.current = new globalScope.AbortController!()
}
counter.current++
return (lastPromise.current = new Promise((resolve, reject) => {
if (!isMounted.current) return
const executor = () => promiseFn().then(resolve, reject)
dispatch({
type: ActionTypes.start,
payload: executor,
meta: getMeta(),
})
}))
},
[dispatch, getMeta]
)
const { promise, promiseFn, initialValue } = options
const load = useCallback(() => {
const isPreInitialized = initialValue && counter.current === 0
if (promise) {
start(() => promise)
.then(handleResolve(counter.current))
.catch(handleReject(counter.current))
} else if (promiseFn && !isPreInitialized) {
start(() => promiseFn(lastOptions.current, abortController.current))
.then(handleResolve(counter.current))
.catch(handleReject(counter.current))
}
}, [start, promise, promiseFn, initialValue, handleResolve, handleReject])
const { deferFn } = options
const run = useCallback(
(...args) => {
if (deferFn) {
lastArgs.current = args
start(() => deferFn(args, lastOptions.current, abortController.current))
.then(handleResolve(counter.current))
.catch(handleReject(counter.current))
}
},
[start, deferFn, handleResolve, handleReject]
)
const reload = useCallback(() => {
lastArgs.current ? run(...lastArgs.current) : load()
}, [run, load])
const { onCancel } = options
const cancel = useCallback(() => {
onCancel && onCancel()
counter.current++
abortController.current.abort()
isMounted.current &&
dispatch({
type: ActionTypes.cancel,
meta: getMeta(),
})
}, [onCancel, dispatch, getMeta])
/* These effects should only be triggered on changes to specific props */
/* eslint-disable react-hooks/exhaustive-deps */
const { watch, watchFn } = options
useEffect(() => {
if (watchFn && lastOptions.current && watchFn(options, lastOptions.current)) {
lastOptions.current = options
load()
}
})
useEffect(() => {
lastOptions.current = options
}, [options])
useEffect(() => {
if (counter.current) cancel()
if (promise || promiseFn) load()
}, [promise, promiseFn, watch])
useEffect(
() => {
isMounted.current = true
return () => {
isMounted.current = false
}
},
[]
)
useEffect(() => () => cancel(), [])
/* eslint-enable react-hooks/exhaustive-deps */
useDebugValue(state, ({ status }) => `[${counter.current}] ${status}`)
if (options.suspense && state.isPending && lastPromise.current !== neverSettle) {
// Rely on Suspense to handle the loading state
throw lastPromise.current
}
return useMemo(
() =>
({
...state,
run,
reload,
cancel,
setData,
setError,
} as AsyncState<T>),
[state, run, reload, cancel, setData, setError]
)
}
export class FetchError extends Error {
constructor(public response: Response) {
super(`${response.status} ${response.statusText}`)
/* istanbul ignore next */
if (Object.setPrototypeOf) {
// Not available in IE 10, but can be polyfilled
Object.setPrototypeOf(this, FetchError.prototype)
}
}
}
const parseResponse = (accept: undefined | string, json: undefined | boolean) => (
res: Response
) => {
if (!res.ok) return Promise.reject(new FetchError(res))
if (typeof json === "boolean") return json ? res.json() : res
return accept === "application/json" ? res.json() : res
}
type OverrideParams = { resource?: RequestInfo } & Partial<RequestInit>
interface FetchRun<T> extends Omit<AbstractState<T>, "run"> {
run(overrideParams: (params?: OverrideParams) => OverrideParams): void
run(overrideParams: OverrideParams): void
run(ignoredEvent: React.SyntheticEvent): void
run(ignoredEvent: Event): void
run(): void
}
type FetchRunArgs =
| [(params?: OverrideParams) => OverrideParams]
| [OverrideParams]
| [React.SyntheticEvent]
| [Event]
| []
function isEvent(e: FetchRunArgs[0]): e is Event | React.SyntheticEvent {
return typeof e === "object" && "preventDefault" in e
}
/**
*
* @param {RequestInfo} resource
* @param {RequestInit} init
* @param {FetchOptions} options
* @returns {AsyncState<T, FetchRun<T>>}
*/
function useAsyncFetch<T>(
resource: RequestInfo,
init: RequestInit,
{ defer, json, ...options }: FetchOptions<T> = {}
): AsyncState<T, FetchRun<T>> {
const method = (resource as Request).method || (init && init.method)
const headers: Headers & Record<string, any> =
(resource as Request).headers || (init && init.headers) || {}
const accept: string | undefined =
headers["Accept"] || headers["accept"] || (headers.get && headers.get("accept"))
const doFetch = (input: RequestInfo, init: RequestInit) =>
globalScope.fetch(input, init).then(parseResponse(accept, json))
const isDefer =
typeof defer === "boolean" ? defer : ["POST", "PUT", "PATCH", "DELETE"].indexOf(method!) !== -1
const fn = isDefer ? "deferFn" : "promiseFn"
const identity = JSON.stringify({
resource,
init,
isDefer,
})
const promiseFn = useCallback(
(_: AsyncOptions<T>, { signal }: AbortController) => {
return doFetch(resource, { signal, ...init })
},
[identity] // eslint-disable-line react-hooks/exhaustive-deps
)
const deferFn = useCallback(
function([override]: FetchRunArgs, _: AsyncOptions<T>, { signal }: AbortController) {
if (!override || isEvent(override)) {
return doFetch(resource, { signal, ...init })
}
if (typeof override === "function") {
const { resource: runResource, ...runInit } = override({ resource, signal, ...init })
return doFetch(runResource || resource, { signal, ...runInit })
}
const { resource: runResource, ...runInit } = override
return doFetch(runResource || resource, { signal, ...init, ...runInit })
},
[identity] // eslint-disable-line react-hooks/exhaustive-deps
)
const state = useAsync({
...options,
[fn]: isDefer ? deferFn : promiseFn,
})
useDebugValue(state, ({ counter, status }) => `[${counter}] ${status}`)
return state
}
const unsupported = () => {
throw new Error(
"useAsync requires React v16.8 or up. Upgrade your React version or use the <Async> component instead."
)
}
// @ts-ignore
export default useEffect ? useAsync : unsupported
// @ts-ignore
export const useFetch = useEffect ? useAsyncFetch : unsupported