@@ -49,7 +49,7 @@ export interface FetchEventSourceInit extends RequestInit {
49
49
*/
50
50
openWhenHidden ?: boolean ;
51
51
52
- /** The Fetch function to use. Defaults to window. fetch */
52
+ /** The Fetch function to use. Defaults to fetch */
53
53
fetch ?: typeof fetch ;
54
54
}
55
55
@@ -79,15 +79,17 @@ export function fetchEventSource(input: RequestInfo, {
79
79
}
80
80
}
81
81
82
- if ( ! openWhenHidden ) {
82
+ if ( typeof window !== 'undefined' && ! openWhenHidden ) {
83
83
document . addEventListener ( 'visibilitychange' , onVisibilityChange ) ;
84
84
}
85
85
86
86
let retryInterval = DefaultRetryInterval ;
87
87
let retryTimer = 0 ;
88
88
function dispose ( ) {
89
- document . removeEventListener ( 'visibilitychange' , onVisibilityChange ) ;
90
- window . clearTimeout ( retryTimer ) ;
89
+ if ( typeof window !== 'undefined' && ! openWhenHidden ) {
90
+ document . removeEventListener ( 'visibilitychange' , onVisibilityChange ) ;
91
+ }
92
+ clearTimeout ( retryTimer ) ;
91
93
curRequestController . abort ( ) ;
92
94
}
93
95
@@ -97,12 +99,12 @@ export function fetchEventSource(input: RequestInfo, {
97
99
resolve ( ) ; // don't waste time constructing/logging errors
98
100
} ) ;
99
101
100
- const fetch = inputFetch ?? window . fetch ;
102
+ const fetchFn = inputFetch ?? fetch ;
101
103
const onopen = inputOnOpen ?? defaultOnOpen ;
102
104
async function create ( ) {
103
105
curRequestController = new AbortController ( ) ;
104
106
try {
105
- const response = await fetch ( input , {
107
+ const response = await fetchFn ( input , {
106
108
...rest ,
107
109
headers,
108
110
signal : curRequestController . signal ,
@@ -131,8 +133,8 @@ export function fetchEventSource(input: RequestInfo, {
131
133
try {
132
134
// check if we need to retry:
133
135
const interval : any = onerror ?.( err ) ?? retryInterval ;
134
- window . clearTimeout ( retryTimer ) ;
135
- retryTimer = window . setTimeout ( create , interval ) ;
136
+ clearTimeout ( retryTimer ) ;
137
+ retryTimer = setTimeout ( create , interval ) ;
136
138
} catch ( innerErr ) {
137
139
// we should not retry anymore:
138
140
dispose ( ) ;
0 commit comments