Skip to content

Commit c5a4e09

Browse files
committed
Restore Node.js (SSR) compatibility.
1 parent ed200a3 commit c5a4e09

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

packages/react-async-devtools/src/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ import { actionTypes, reducer } from "react-async"
33

44
import { Root, Range, Checkbox, Label, Small, Ol, Li, Button } from "./components"
55

6+
const root =
7+
(typeof self === "object" && self.self === self && self) ||
8+
(typeof global === "object" && global.global === global && global)
9+
610
const state = {
7-
intercept: window.sessionStorage.getItem("intercept") === "true",
8-
latency: window.sessionStorage.getItem("latency") || "0",
11+
intercept: root.sessionStorage.getItem("intercept") === "true",
12+
latency: root.sessionStorage.getItem("latency") || "0",
913
update: () => {},
1014
}
1115

12-
window.__REACT_ASYNC__ = window.__REACT_ASYNC__ || {}
13-
window.__REACT_ASYNC__.devToolsDispatcher = (action, dispatch) => {
16+
root.__REACT_ASYNC__ = root.__REACT_ASYNC__ || {}
17+
root.__REACT_ASYNC__.devToolsDispatcher = (action, dispatch) => {
1418
const run = () => {
1519
dispatch(action)
1620
state.update(action)
@@ -46,13 +50,13 @@ const DevTools = () => {
4650
}))
4751
}
4852
const updateLatency = event => {
49-
window.sessionStorage.setItem("latency", event.target.value)
53+
root.sessionStorage.setItem("latency", event.target.value)
5054
delay.current = event.target.value * 1000
5155
state.latency = event.target.value
5256
setLatency(event.target.value)
5357
}
5458
const updateIntercept = event => {
55-
window.sessionStorage.setItem("intercept", event.target.checked ? "true" : "false")
59+
root.sessionStorage.setItem("intercept", event.target.checked ? "true" : "false")
5660
state.intercept = event.target.checked
5761
intercept.current = event.target.checked
5862
setIntercept(event.target.checked)

packages/react-async/src/Async.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { Initial, Pending, Fulfilled, Rejected, Settled } from "./helpers"
33
import propTypes from "./propTypes"
44
import { actionTypes, init, dispatchMiddleware, reducer as asyncReducer } from "./reducer"
55

6+
const root =
7+
(typeof self === "object" && self.self === self && self) ||
8+
(typeof global === "object" && global.global === global && global)
9+
610
/**
711
* createInstance allows you to create instances of Async that are bound to a specific promise.
812
* A unique instance also uses its own React context for better nesting capability.
@@ -44,7 +48,7 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
4448
}
4549
this.debugLabel = props.debugLabel || defaultProps.debugLabel
4650

47-
const { devToolsDispatcher } = window.__REACT_ASYNC__ || {}
51+
const { devToolsDispatcher } = root.__REACT_ASYNC__ || {}
4852
const _reducer = props.reducer || defaultProps.reducer
4953
const _dispatcher = props.dispatcher || defaultProps.dispatcher || devToolsDispatcher
5054
const reducer = _reducer
@@ -100,9 +104,9 @@ export const createInstance = (defaultProps = {}, displayName = "Async") => {
100104
}
101105

102106
start(promiseFn) {
103-
if ("AbortController" in window) {
107+
if ("AbortController" in root) {
104108
this.abortController.abort()
105-
this.abortController = new window.AbortController()
109+
this.abortController = new root.AbortController()
106110
}
107111
this.counter++
108112
return new Promise((resolve, reject) => {

packages/react-async/src/useAsync.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { useCallback, useDebugValue, useEffect, useMemo, useRef, useReducer } fr
22
import { actionTypes, init, dispatchMiddleware, reducer as asyncReducer } from "./reducer"
33

44
const noop = () => {}
5+
const root =
6+
(typeof self === "object" && self.self === self && self) ||
7+
(typeof global === "object" && global.global === global && global)
58

69
const useAsync = (arg1, arg2) => {
710
const options = typeof arg1 === "function" ? { ...arg2, promiseFn: arg1 } : arg1
@@ -12,7 +15,7 @@ const useAsync = (arg1, arg2) => {
1215
const prevOptions = useRef(undefined)
1316
const abortController = useRef({ abort: noop })
1417

15-
const { devToolsDispatcher } = window.__REACT_ASYNC__ || {}
18+
const { devToolsDispatcher } = root.__REACT_ASYNC__ || {}
1619
const { reducer, dispatcher = devToolsDispatcher } = options
1720
const [state, _dispatch] = useReducer(
1821
reducer ? (state, action) => reducer(state, action, asyncReducer) : asyncReducer,
@@ -48,9 +51,9 @@ const useAsync = (arg1, arg2) => {
4851
count === counter.current && setError(error, () => onReject && onReject(error))
4952

5053
const start = promiseFn => {
51-
if ("AbortController" in window) {
54+
if ("AbortController" in root) {
5255
abortController.current.abort()
53-
abortController.current = new window.AbortController()
56+
abortController.current = new root.AbortController()
5457
}
5558
counter.current++
5659
return new Promise((resolve, reject) => {
@@ -133,7 +136,7 @@ const useAsyncFetch = (input, init, { defer, json, ...options } = {}) => {
133136
const method = input.method || (init && init.method)
134137
const headers = input.headers || (init && init.headers) || {}
135138
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))
137140
const isDefer = defer === true || ~["POST", "PUT", "PATCH", "DELETE"].indexOf(method)
138141
const fn = defer === false || !isDefer ? "promiseFn" : "deferFn"
139142
const state = useAsync({

0 commit comments

Comments
 (0)