Skip to content

Commit e95a542

Browse files
committed
Revert "Allow overriding the 'resource' argument of 'fetch' when invokin… (#150)"
This only reverts code changes to redo them manually in TS
1 parent e959418 commit e95a542

File tree

1 file changed

+63
-23
lines changed

1 file changed

+63
-23
lines changed

packages/react-async/src/useAsync.js

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,22 @@ import {
1212
const noop = () => {}
1313

1414
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
1622

1723
const counter = useRef(0)
1824
const isMounted = useRef(true)
1925
const lastArgs = useRef(undefined)
2026
const lastOptions = useRef(undefined)
2127
const lastPromise = useRef(neverSettle)
22-
const abortController = useRef({ abort: noop })
28+
const abortController = useRef({
29+
abort: noop,
30+
})
2331

2432
const { devToolsDispatcher } = globalScope.__REACT_ASYNC__
2533
const { reducer, dispatcher = devToolsDispatcher } = options
@@ -37,14 +45,23 @@ const useAsync = (arg1, arg2) => {
3745

3846
const { debugLabel } = options
3947
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+
}),
4154
[debugLabel]
4255
)
4356

4457
const setData = useCallback(
4558
(data, callback = noop) => {
4659
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+
})
4865
callback()
4966
}
5067
return data
@@ -55,7 +72,12 @@ const useAsync = (arg1, arg2) => {
5572
const setError = useCallback(
5673
(error, callback = noop) => {
5774
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+
})
5981
callback()
6082
}
6183
return error
@@ -83,7 +105,11 @@ const useAsync = (arg1, arg2) => {
83105
return (lastPromise.current = new Promise((resolve, reject) => {
84106
if (!isMounted.current) return
85107
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+
})
87113
}))
88114
},
89115
[dispatch, getMeta]
@@ -125,7 +151,11 @@ const useAsync = (arg1, arg2) => {
125151
onCancel && onCancel()
126152
counter.current++
127153
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+
})
129159
}, [onCancel, dispatch, getMeta])
130160

131161
/* These effects should only be triggered on changes to specific props */
@@ -183,31 +213,41 @@ const parseResponse = (accept, json) => res => {
183213
return accept === "application/json" ? res.json() : res
184214
}
185215

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) || {}
189219
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))
192221
const isDefer =
193222
typeof defer === "boolean" ? defer : ["POST", "PUT", "PATCH", "DELETE"].indexOf(method) !== -1
194223
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+
})
196229
const state = useAsync({
197230
...options,
198231
[fn]: useCallback(
199232
(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+
})
208240
}
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+
})
211251
},
212252
[identity] // eslint-disable-line react-hooks/exhaustive-deps
213253
),

0 commit comments

Comments
 (0)