Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new eventStream API for async generators #336

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 42 additions & 16 deletions src/server/event-stream.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
interface SendFunctionArgs {
interface SendFunctionArgs<T = string> {
/**
* @default "message"
*/
event?: string;
data: string;
data: T;
}

interface SendFunction {
(args: SendFunctionArgs): void;
interface SendFunction<T = string> {
(args: SendFunctionArgs<T>): void;
}

interface CleanupFunction {
Expand All @@ -18,8 +18,17 @@ interface AbortFunction {
(): void;
}

interface InitFunction {
(send: SendFunction, abort: AbortFunction): CleanupFunction;
interface InitFunction<T = string> {
(send: SendFunction<T>, abort: AbortFunction): CleanupFunction;
}

interface HandleFunction<T = string> {
(send: SendFunction<T>, abort: AbortFunction): Promise<void> | void;
}

interface Options<T = string> {
cleanup: CleanupFunction;
handle: HandleFunction<T>;
}

/**
Expand All @@ -28,21 +37,33 @@ interface InitFunction {
* @param init The function that will be called to initialize the stream, here you can subscribe to your events
* @returns A Response object that can be returned from a loader
*/
export function eventStream(
export function eventStream<T = string>(
signal: AbortSignal,
init: InitFunction,
options: ResponseInit = {},
init: InitFunction<T>,
responseInit?: ResponseInit,
): Response;
export function eventStream<T = string>(
signal: AbortSignal,
options: Options<T>,
responseInit?: ResponseInit,
): Response;
export function eventStream<T = string>(
signal: AbortSignal,
initOrOptions: InitFunction<T> | Options<T>,
responseInit: ResponseInit = {},
) {
let stream = new ReadableStream({
start(controller) {
let encoder = new TextEncoder();

function send({ event = "message", data }: SendFunctionArgs) {
controller.enqueue(encoder.encode(`event: ${event}\n`));
controller.enqueue(encoder.encode(`data: ${data}\n\n`));
}
let cleanup =
typeof initOrOptions === "function"
? initOrOptions(send, close)
: initOrOptions.cleanup;

let cleanup = init(send, close);
if (typeof initOrOptions !== "function") {
initOrOptions.handle(send, close);
}

let closed = false;

Expand All @@ -57,10 +78,15 @@ export function eventStream(
signal.addEventListener("abort", close);

if (signal.aborted) return close();

async function send({ event = "message", data }: SendFunctionArgs<T>) {
controller.enqueue(encoder.encode(`event: ${event}\n`));
controller.enqueue(encoder.encode(`data: ${data}\n\n`));
}
},
});

let headers = new Headers(options.headers);
let headers = new Headers(responseInit.headers);

if (headers.has("Content-Type")) {
console.warn("Overriding Content-Type header to `text/event-stream`");
Expand All @@ -78,5 +104,5 @@ export function eventStream(
headers.set("Cache-Control", "no-cache");
headers.set("Connection", "keep-alive");

return new Response(stream, { headers });
return new Response(stream, { ...responseInit, headers });
}
Loading
Loading