-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
event-stream.ts
62 lines (51 loc) · 1.44 KB
/
event-stream.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
interface SendFunctionArgs {
/**
* @default "message"
*/
event?: string;
data: string;
}
interface SendFunction {
(args: SendFunctionArgs): void;
}
interface CleanupFunction {
(): void;
}
interface InitFunction {
(send: SendFunction): CleanupFunction;
}
/**
* A response holper to use Server Sent Events server-side
* @param signal The AbortSignal used to close the stream
* @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(signal: AbortSignal, init: InitFunction) {
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 = init(send);
let closed = false;
function close() {
if (closed) return;
cleanup();
closed = true;
signal.removeEventListener("abort", close);
controller.close();
}
signal.addEventListener("abort", close);
if (signal.aborted) return close();
},
});
return new Response(stream, {
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
},
});
}