@@ -12,14 +12,22 @@ import {
12
12
const noop = ( ) => { }
13
13
14
14
const useAsync = ( arg1 , arg2 ) => {
15
- const options = typeof arg1 === "function" ? { ...arg2 , promiseFn : arg1 } : arg1
15
+ const options =
16
+ typeof arg1 === "function"
17
+ ? {
18
+ ...arg2 ,
19
+ promiseFn : arg1 ,
20
+ }
21
+ : arg1
16
22
17
23
const counter = useRef ( 0 )
18
24
const isMounted = useRef ( true )
19
25
const lastArgs = useRef ( undefined )
20
26
const lastOptions = useRef ( undefined )
21
27
const lastPromise = useRef ( neverSettle )
22
- const abortController = useRef ( { abort : noop } )
28
+ const abortController = useRef ( {
29
+ abort : noop ,
30
+ } )
23
31
24
32
const { devToolsDispatcher } = globalScope . __REACT_ASYNC__
25
33
const { reducer, dispatcher = devToolsDispatcher } = options
@@ -37,14 +45,23 @@ const useAsync = (arg1, arg2) => {
37
45
38
46
const { debugLabel } = options
39
47
const getMeta = useCallback (
40
- meta => ( { counter : counter . current , promise : lastPromise . current , debugLabel, ...meta } ) ,
48
+ meta => ( {
49
+ counter : counter . current ,
50
+ promise : lastPromise . current ,
51
+ debugLabel,
52
+ ...meta ,
53
+ } ) ,
41
54
[ debugLabel ]
42
55
)
43
56
44
57
const setData = useCallback (
45
58
( data , callback = noop ) => {
46
59
if ( isMounted . current ) {
47
- dispatch ( { type : actionTypes . fulfill , payload : data , meta : getMeta ( ) } )
60
+ dispatch ( {
61
+ type : actionTypes . fulfill ,
62
+ payload : data ,
63
+ meta : getMeta ( ) ,
64
+ } )
48
65
callback ( )
49
66
}
50
67
return data
@@ -55,7 +72,12 @@ const useAsync = (arg1, arg2) => {
55
72
const setError = useCallback (
56
73
( error , callback = noop ) => {
57
74
if ( isMounted . current ) {
58
- dispatch ( { type : actionTypes . reject , payload : error , error : true , meta : getMeta ( ) } )
75
+ dispatch ( {
76
+ type : actionTypes . reject ,
77
+ payload : error ,
78
+ error : true ,
79
+ meta : getMeta ( ) ,
80
+ } )
59
81
callback ( )
60
82
}
61
83
return error
@@ -83,7 +105,11 @@ const useAsync = (arg1, arg2) => {
83
105
return ( lastPromise . current = new Promise ( ( resolve , reject ) => {
84
106
if ( ! isMounted . current ) return
85
107
const executor = ( ) => promiseFn ( ) . then ( resolve , reject )
86
- dispatch ( { type : actionTypes . start , payload : executor , meta : getMeta ( ) } )
108
+ dispatch ( {
109
+ type : actionTypes . start ,
110
+ payload : executor ,
111
+ meta : getMeta ( ) ,
112
+ } )
87
113
} ) )
88
114
} ,
89
115
[ dispatch , getMeta ]
@@ -125,7 +151,11 @@ const useAsync = (arg1, arg2) => {
125
151
onCancel && onCancel ( )
126
152
counter . current ++
127
153
abortController . current . abort ( )
128
- isMounted . current && dispatch ( { type : actionTypes . cancel , meta : getMeta ( ) } )
154
+ isMounted . current &&
155
+ dispatch ( {
156
+ type : actionTypes . cancel ,
157
+ meta : getMeta ( ) ,
158
+ } )
129
159
} , [ onCancel , dispatch , getMeta ] )
130
160
131
161
/* These effects should only be triggered on changes to specific props */
@@ -183,31 +213,41 @@ const parseResponse = (accept, json) => res => {
183
213
return accept === "application/json" ? res . json ( ) : res
184
214
}
185
215
186
- const useAsyncFetch = ( resource , init , { defer, json, ...options } = { } ) => {
187
- const method = resource . method || ( init && init . method )
188
- const headers = resource . headers || ( init && init . headers ) || { }
216
+ const useAsyncFetch = ( input , init , { defer, json, ...options } = { } ) => {
217
+ const method = input . method || ( init && init . method )
218
+ const headers = input . headers || ( init && init . headers ) || { }
189
219
const accept = headers [ "Accept" ] || headers [ "accept" ] || ( headers . get && headers . get ( "accept" ) )
190
- const doFetch = ( resource , init ) =>
191
- globalScope . fetch ( resource , init ) . then ( parseResponse ( accept , json ) )
220
+ const doFetch = ( input , init ) => globalScope . fetch ( input , init ) . then ( parseResponse ( accept , json ) )
192
221
const isDefer =
193
222
typeof defer === "boolean" ? defer : [ "POST" , "PUT" , "PATCH" , "DELETE" ] . indexOf ( method ) !== - 1
194
223
const fn = isDefer ? "deferFn" : "promiseFn"
195
- const identity = JSON . stringify ( { resource, init, isDefer } )
224
+ const identity = JSON . stringify ( {
225
+ input,
226
+ init,
227
+ isDefer,
228
+ } )
196
229
const state = useAsync ( {
197
230
...options ,
198
231
[ fn ] : useCallback (
199
232
( arg1 , arg2 , arg3 ) => {
200
- const [ override , signal ] = isDefer ? [ arg1 [ 0 ] , arg3 . signal ] : [ undefined , arg2 . signal ]
201
- const isEvent = typeof override === "object" && "preventDefault" in override
202
- if ( ! override || isEvent ) {
203
- return doFetch ( resource , { signal, ...init } )
204
- }
205
- if ( typeof override === "function" ) {
206
- const { resource : runResource , ...runInit } = override ( { resource, signal, ...init } )
207
- return doFetch ( runResource || resource , { signal, ...runInit } )
233
+ const [ override , signal ] = arg3 ? [ arg1 [ 0 ] , arg3 . signal ] : [ undefined , arg2 . signal ]
234
+ if ( typeof override === "object" && "preventDefault" in override ) {
235
+ // Don't spread Events or SyntheticEvents
236
+ return doFetch ( input , {
237
+ signal,
238
+ ...init ,
239
+ } )
208
240
}
209
- const { resource : runResource , ...runInit } = override
210
- return doFetch ( runResource || resource , { signal, ...init , ...runInit } )
241
+ return typeof override === "function"
242
+ ? doFetch ( input , {
243
+ signal,
244
+ ...override ( init ) ,
245
+ } )
246
+ : doFetch ( input , {
247
+ signal,
248
+ ...init ,
249
+ ...override ,
250
+ } )
211
251
} ,
212
252
[ identity ] // eslint-disable-line react-hooks/exhaustive-deps
213
253
) ,
0 commit comments