From 02379fae06aa849f4c3b3ec7f299b8f83a4dc47d Mon Sep 17 00:00:00 2001 From: AkashKarnatak Date: Wed, 17 Jan 2024 15:44:45 +0530 Subject: [PATCH 1/3] Use self instead of window. This ensures compatibility with background script in manifest v3 --- src/fetch.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/fetch.ts b/src/fetch.ts index 162ea45..043593a 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -87,7 +87,7 @@ export function fetchEventSource(input: RequestInfo, { let retryTimer = 0; function dispose() { document.removeEventListener('visibilitychange', onVisibilityChange); - window.clearTimeout(retryTimer); + self.clearTimeout(retryTimer); curRequestController.abort(); } @@ -97,7 +97,7 @@ export function fetchEventSource(input: RequestInfo, { resolve(); // don't waste time constructing/logging errors }); - const fetch = inputFetch ?? window.fetch; + const fetch = inputFetch ?? self.fetch; const onopen = inputOnOpen ?? defaultOnOpen; async function create() { curRequestController = new AbortController(); @@ -131,8 +131,8 @@ export function fetchEventSource(input: RequestInfo, { try { // check if we need to retry: const interval: any = onerror?.(err) ?? retryInterval; - window.clearTimeout(retryTimer); - retryTimer = window.setTimeout(create, interval); + self.clearTimeout(retryTimer); + retryTimer = self.setTimeout(create, interval); } catch (innerErr) { // we should not retry anymore: dispose(); From dc62c8996a9779f4ca117e6a0700841641b7f563 Mon Sep 17 00:00:00 2001 From: AkashKarnatak Date: Sat, 13 Apr 2024 11:13:36 +0530 Subject: [PATCH 2/3] Create visibilitychange listener only when document is defined --- src/fetch.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/fetch.ts b/src/fetch.ts index 043593a..e6dd4c7 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -65,6 +65,8 @@ export function fetchEventSource(input: RequestInfo, { ...rest }: FetchEventSourceInit) { return new Promise((resolve, reject) => { + // set openWhenHidden to false when document is not defined + openWhenHidden ||= !!self.document // make a copy of the input headers since we may modify it below: const headers = { ...inputHeaders }; if (!headers.accept) { @@ -86,7 +88,9 @@ export function fetchEventSource(input: RequestInfo, { let retryInterval = DefaultRetryInterval; let retryTimer = 0; function dispose() { - document.removeEventListener('visibilitychange', onVisibilityChange); + if (!openWhenHidden) { + document.removeEventListener('visibilitychange', onVisibilityChange); + } self.clearTimeout(retryTimer); curRequestController.abort(); } From d341cffeb59863156787e724fa4259067ebd61f4 Mon Sep 17 00:00:00 2001 From: AkashKarnatak Date: Sat, 13 Apr 2024 11:20:50 +0530 Subject: [PATCH 3/3] Fixed logical error --- src/fetch.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fetch.ts b/src/fetch.ts index e6dd4c7..186a972 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -65,8 +65,8 @@ export function fetchEventSource(input: RequestInfo, { ...rest }: FetchEventSourceInit) { return new Promise((resolve, reject) => { - // set openWhenHidden to false when document is not defined - openWhenHidden ||= !!self.document + // set openWhenHidden to true when document is not defined + openWhenHidden ||= !self.document // make a copy of the input headers since we may modify it below: const headers = { ...inputHeaders }; if (!headers.accept) {