EventStreamHandle
Open SSE stream: iterate events, read open metadata, and close early.
@defjs/core · Type
Signature
interface EventStreamHandle<TEvent = EventStreamMessage> extends AsyncIterable<TEvent>, AsyncDisposable { readonly open: EventStreamOpenInfo; readonly closed: Promise<EventStreamCloseInfo>; close(reason?: unknown): void; [Symbol.asyncDispose](): PromiseLike<void>; }
EventStreamMessage is the raw { id: string; event: string; data: string } message. Handles returned by an event-stream operation carry parsed EventStreamData.
open is the startup snapshot (EventStreamOpenInfo); after a reconnect it can change, so read stream.open again. closed resolves with an EventStreamCloseInfo when the stream ends.
Prefer await using stream = handle for owned scope cleanup. The disposer stops Defjs reading and reconnect work and waits for its reader lock to be released; it does not promise that a provider-controlled, stuck ReadableStream.cancel() promise will finish. Manual close() / closed remains available.
async function consume(handle: EventStreamHandle<unknown>): Promise<void> {
await using stream = handle;
for await (const event of stream) console.log(event);
}Structural implementations of EventStreamHandle must implement [Symbol.asyncDispose]() and return the same owned teardown path. This is a compile-time requirement for custom handles; code that only receives handles returned by Defjs has no additional runtime call requirement.
Members
Prop
Type
open EventStreamOpenInfoclosed Promise<EventStreamCloseInfo>close (reason?: unknown) => void[Symbol.asyncDispose] () => PromiseLike<void>