@@ -2,6 +2,9 @@ import { useCallback, useDebugValue, useEffect, useMemo, useRef, useReducer } fr
2
2
import { actionTypes , init , dispatchMiddleware , reducer as asyncReducer } from "./reducer"
3
3
4
4
const noop = ( ) => { }
5
+ const root =
6
+ ( typeof self === "object" && self . self === self && self ) ||
7
+ ( typeof global === "object" && global . global === global && global )
5
8
6
9
const useAsync = ( arg1 , arg2 ) => {
7
10
const options = typeof arg1 === "function" ? { ...arg2 , promiseFn : arg1 } : arg1
@@ -12,7 +15,7 @@ const useAsync = (arg1, arg2) => {
12
15
const prevOptions = useRef ( undefined )
13
16
const abortController = useRef ( { abort : noop } )
14
17
15
- const { devToolsDispatcher } = window . __REACT_ASYNC__ || { }
18
+ const { devToolsDispatcher } = root . __REACT_ASYNC__ || { }
16
19
const { reducer, dispatcher = devToolsDispatcher } = options
17
20
const [ state , _dispatch ] = useReducer (
18
21
reducer ? ( state , action ) => reducer ( state , action , asyncReducer ) : asyncReducer ,
@@ -48,9 +51,9 @@ const useAsync = (arg1, arg2) => {
48
51
count === counter . current && setError ( error , ( ) => onReject && onReject ( error ) )
49
52
50
53
const start = promiseFn => {
51
- if ( "AbortController" in window ) {
54
+ if ( "AbortController" in root ) {
52
55
abortController . current . abort ( )
53
- abortController . current = new window . AbortController ( )
56
+ abortController . current = new root . AbortController ( )
54
57
}
55
58
counter . current ++
56
59
return new Promise ( ( resolve , reject ) => {
@@ -133,7 +136,7 @@ const useAsyncFetch = (input, init, { defer, json, ...options } = {}) => {
133
136
const method = input . method || ( init && init . method )
134
137
const headers = input . headers || ( init && init . headers ) || { }
135
138
const accept = headers [ "Accept" ] || headers [ "accept" ] || ( headers . get && headers . get ( "accept" ) )
136
- const doFetch = ( input , init ) => window . fetch ( input , init ) . then ( parseResponse ( accept , json ) )
139
+ const doFetch = ( input , init ) => root . fetch ( input , init ) . then ( parseResponse ( accept , json ) )
137
140
const isDefer = defer === true || ~ [ "POST" , "PUT" , "PATCH" , "DELETE" ] . indexOf ( method )
138
141
const fn = defer === false || ! isDefer ? "promiseFn" : "deferFn"
139
142
const state = useAsync ( {
0 commit comments