Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
flvndvd committed Jul 17, 2024
1 parent 0b63ba5 commit ca4d0b7
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions front/hooks/useEventSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ const stableEventSourceManager = {
remove(uniqueId: string) {
const source = this.sources.get(uniqueId);
if (source) {
console.log("Closing EventSource connection", uniqueId);
source.close();
this.sources.delete(uniqueId);
}
Expand Down Expand Up @@ -100,8 +99,6 @@ export function useEventSource(
}

source.onopen = () => {
console.log("EventSource connection opened", uniqueId);

// If connected, reset the reconnect attempts and clear the reconnect timeout.
reconnectAttempts.current = 0;
if (reconnectTimeoutRef.current) {
Expand All @@ -128,6 +125,15 @@ export function useEventSource(

reconnectAttempts.current++;

if (reconnectAttempts.current >= 10) {
console.log(
"Too many errors, not reconnecting. Please refresh the page."
);
setIsError(new Error("Too many errors, closing connection."));

return;
}

console.error(
`Connection error. Attempting to reconnect in ${RECONNECT_DELAY}ms`
);
Expand All @@ -136,8 +142,6 @@ export function useEventSource(
reconnectTimeoutRef.current = setTimeout(() => {
setReconnectCounter((c) => c + 1);
}, RECONNECT_DELAY);

setIsError(new Error("Connection error. Attempting to reconnect."));
};

return source;
Expand Down

0 comments on commit ca4d0b7

Please sign in to comment.