@@ -49,7 +49,7 @@ export interface FetchEventSourceInit extends RequestInit {
4949 */
5050 openWhenHidden ?: boolean ;
5151
52- /** The Fetch function to use. Defaults to window. fetch */
52+ /** The Fetch function to use. Defaults to fetch */
5353 fetch ?: typeof fetch ;
5454}
5555
@@ -79,15 +79,17 @@ export function fetchEventSource(input: RequestInfo, {
7979 }
8080 }
8181
82- if ( ! openWhenHidden ) {
82+ if ( typeof window !== 'undefined' && ! openWhenHidden ) {
8383 document . addEventListener ( 'visibilitychange' , onVisibilityChange ) ;
8484 }
8585
8686 let retryInterval = DefaultRetryInterval ;
8787 let retryTimer = 0 ;
8888 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 ) ;
9193 curRequestController . abort ( ) ;
9294 }
9395
@@ -97,12 +99,12 @@ export function fetchEventSource(input: RequestInfo, {
9799 resolve ( ) ; // don't waste time constructing/logging errors
98100 } ) ;
99101
100- const fetch = inputFetch ?? window . fetch ;
102+ const fetchFn = inputFetch ?? fetch ;
101103 const onopen = inputOnOpen ?? defaultOnOpen ;
102104 async function create ( ) {
103105 curRequestController = new AbortController ( ) ;
104106 try {
105- const response = await fetch ( input , {
107+ const response = await fetchFn ( input , {
106108 ...rest ,
107109 headers,
108110 signal : curRequestController . signal ,
@@ -131,8 +133,8 @@ export function fetchEventSource(input: RequestInfo, {
131133 try {
132134 // check if we need to retry:
133135 const interval : any = onerror ?.( err ) ?? retryInterval ;
134- window . clearTimeout ( retryTimer ) ;
135- retryTimer = window . setTimeout ( create , interval ) ;
136+ clearTimeout ( retryTimer ) ;
137+ retryTimer = setTimeout ( create , interval ) ;
136138 } catch ( innerErr ) {
137139 // we should not retry anymore:
138140 dispose ( ) ;
0 commit comments