// types — SSE-specific types for @kyneta/sse-transport. // // The SSE client's transport gate is the connection key: the server's first // event on the stream. Only once the client holds it can it POST, so only // then does it create its channel and open the handshake. // // DisconnectReason is SSE-specific: no "closed" variant (SSE doesn't have // close codes). import type { ClientLifecycleEvents } from "@kyneta/transport" import type { ConnectionKey } from "./connection-key.js" // Re-export transition types from @kyneta/machine export type { StateTransition, TransitionListener } from "@kyneta/machine" // --------------------------------------------------------------------------- // Disconnect reason // --------------------------------------------------------------------------- /** * Discriminated union describing why an SSE connection was lost. * * Unlike WebSocket's DisconnectReason, SSE has no * `{ type: "closed"; code; reason }`: SSE has no close codes. */ export type DisconnectReason = | { type: "intentional" } | { type: "error"; error: Error } /** * This exchange closed the channel: it refused the server, or the * handshake outlasted its `establishTimeout`. */ | { type: "refused" } | { type: "max-retries-exceeded"; attempts: number } // --------------------------------------------------------------------------- // Connection state (for client transport observability) // --------------------------------------------------------------------------- /** * All possible states of the SSE client. * * An attempt runs from `connecting` until the exchange establishes the * channel. `connected` holds the stream's key, with the channel's * handshake in flight. * * ``` * disconnected → connecting → connected → established * ↓ ↓ ↓ * reconnecting ← ─ ┴ ─ ─ ─ ─ ─ ─ ┘ * ↓ * connecting (retry) * ↓ * disconnected (max retries) * ``` */ export type SseClientState = | { status: "disconnected"; reason?: DisconnectReason } | { status: "connecting"; attempt: number } | { status: "connected"; attempt: number; key: ConnectionKey } | { status: "established"; key: ConnectionKey } | { status: "reconnecting"; attempt: number; nextAttemptMs: number } // --------------------------------------------------------------------------- // Client options // --------------------------------------------------------------------------- /** * Lifecycle event callbacks for the SSE client. */ export type SseClientLifecycleEvents = ClientLifecycleEvents< SseClientState, DisconnectReason >