Add initial_state SSE event for reconnect sync (Phase 3)
When a dashboard connects via SSE, it now receives the current terminal state for all active sessions. This allows dashboards to immediately display the full terminal content without waiting for new output.
This commit is contained in:
parent
88afb7249d
commit
43648f7d60
2 changed files with 20 additions and 1 deletions
|
|
@ -135,8 +135,26 @@ const server = Bun.serve<SessionData>({
|
||||||
start(controller) {
|
start(controller) {
|
||||||
ctrl = controller;
|
ctrl = controller;
|
||||||
sseClients.add(controller);
|
sseClients.add(controller);
|
||||||
// Send initial headers
|
// Send initial connection acknowledgment
|
||||||
controller.enqueue(": connected\n\n");
|
controller.enqueue(": connected\n\n");
|
||||||
|
|
||||||
|
// Send current terminal state for all active sessions
|
||||||
|
for (const [sessionId, termSession] of sessionTerminals.entries()) {
|
||||||
|
const session = getSession(sessionId);
|
||||||
|
if (!session || session.ended_at) continue;
|
||||||
|
|
||||||
|
// Serialize full terminal state as HTML
|
||||||
|
const html = serializeAsHTML(termSession);
|
||||||
|
|
||||||
|
// Send as initial_state event
|
||||||
|
const event: SSEEvent = {
|
||||||
|
type: "initial_state",
|
||||||
|
session_id: sessionId,
|
||||||
|
html,
|
||||||
|
};
|
||||||
|
const eventStr = `event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`;
|
||||||
|
controller.enqueue(eventStr);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
cancel() {
|
cancel() {
|
||||||
sseClients.delete(ctrl);
|
sseClients.delete(ctrl);
|
||||||
|
|
|
||||||
|
|
@ -157,6 +157,7 @@ export type ServerMessage =
|
||||||
// SSE events (Server -> Dashboard)
|
// SSE events (Server -> Dashboard)
|
||||||
|
|
||||||
export type SSEEvent =
|
export type SSEEvent =
|
||||||
|
| { type: "initial_state"; session_id: number; html: string }
|
||||||
| {
|
| {
|
||||||
type: "session_start";
|
type: "session_start";
|
||||||
session_id: number;
|
session_id: number;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue