From 990089de06f925db8813f6e4d94d54072b33b5cd Mon Sep 17 00:00:00 2001 From: Alioth <610886764@qq.com> Date: Wed, 8 Nov 2023 10:52:53 +0800 Subject: [PATCH] feat: add config of retryInterval. --- src/fetch.ts | 8 +++++++- src/parse.ts | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/fetch.ts b/src/fetch.ts index 162ea45..0a62c98 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -49,6 +49,11 @@ export interface FetchEventSourceInit extends RequestInit { */ openWhenHidden?: boolean; + /** + * The interval (in milliseconds) to wait before retrying the connection. + */ + retryInterval?: number; + /** The Fetch function to use. Defaults to window.fetch */ fetch?: typeof fetch; } @@ -61,6 +66,7 @@ export function fetchEventSource(input: RequestInfo, { onclose, onerror, openWhenHidden, + retryInterval: inputRetryInterval, fetch: inputFetch, ...rest }: FetchEventSourceInit) { @@ -83,7 +89,7 @@ export function fetchEventSource(input: RequestInfo, { document.addEventListener('visibilitychange', onVisibilityChange); } - let retryInterval = DefaultRetryInterval; + let retryInterval = inputRetryInterval || DefaultRetryInterval; let retryTimer = 0; function dispose() { document.removeEventListener('visibilitychange', onVisibilityChange); diff --git a/src/parse.ts b/src/parse.ts index c13e3c4..67e1eb7 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -21,7 +21,7 @@ export interface EventSourceMessage { */ export async function getBytes(stream: ReadableStream, onChunk: (arr: Uint8Array) => void) { const reader = stream.getReader(); - let result: ReadableStreamDefaultReadResult; + let result: ReadableStreamReadResult; while (!(result = await reader.read()).done) { onChunk(result.value); }