Skip to content

feat: improve event connection handling and reconnection logic#28

Merged
Emiyaaaaa merged 1 commit into
Emiyaaaaa:mainfrom
VectorPeak:fix/sse
Jun 15, 2026
Merged

feat: improve event connection handling and reconnection logic#28
Emiyaaaaa merged 1 commit into
Emiyaaaaa:mainfrom
VectorPeak:fix/sse

Conversation

@VectorPeak

Copy link
Copy Markdown
Contributor
  • 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.

- 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 isEventIdAfter to drop duplicate/out-of-order events based on lastEventId.
  • 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");
@Emiyaaaaa
Emiyaaaaa merged commit 4f96385 into Emiyaaaaa:main Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants