feat: improve event connection handling and reconnection logic#28
Merged
Conversation
- Added `isEventIdAfter` function to compare event IDs for proper ordering. - Refactored reconnection logic to use a capped delay with jitter for better performance. - Introduced visibility and online event listeners to trigger reconnections. - Enhanced message handling to ensure only valid events are processed based on the last event ID. - Cleaned up event listeners on connection closure to prevent memory leaks.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves the frontend’s Server-Sent Events (SSE) connection robustness for run events by refining reconnection behavior and ensuring incoming events are processed in correct order.
Changes:
- Added
isEventIdAfterto drop duplicate/out-of-order events based onlastEventId. - Refactored reconnection backoff to a capped, jittered delay and added “reconnect now” triggers on visibility/online changes.
- Improved cleanup behavior when closing the connection (removing listeners / nulling handlers).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
100
to
103
| next.addEventListener("open", () => { | ||
| if (cancelled || terminal) return; | ||
| reconnectAttempt = 0; | ||
| setStatus("connected"); | ||
| }); |
Comment on lines
107
to
113
| next.onerror = () => { | ||
| if (cancelled || terminal) return; | ||
| // EventSource may fire onerror while still CONNECTING; only retry once closed. | ||
| if (next.readyState !== EventSource.CLOSED) return; | ||
| closeSource(); | ||
| scheduleReconnect(); | ||
| }; |
| if (typeof window !== "undefined") { | ||
| window.removeEventListener("online", onOnline); | ||
| } | ||
| setStatus("closed"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
isEventIdAfterfunction to compare event IDs for proper ordering.