Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+33
-10
@@ -29,34 +29,57 @@ interface InitFunction {
|
||||
* @returns A Response object that can be returned from a loader
|
||||
*/
|
||||
export function eventStream(signal: AbortSignal, init: InitFunction, options: ResponseInit = {}) {
|
||||
let stream = new ReadableStream({
|
||||
let dispose = () => undefined;
|
||||
const stream = new ReadableStream({
|
||||
start(controller) {
|
||||
let encoder = new TextEncoder();
|
||||
const encoder = new TextEncoder();
|
||||
let closed = false;
|
||||
let cleanup: CleanupFunction | undefined;
|
||||
let cleanupComplete = false;
|
||||
|
||||
function runCleanup() {
|
||||
if (!cleanup || cleanupComplete) return;
|
||||
cleanupComplete = true;
|
||||
cleanup();
|
||||
}
|
||||
|
||||
dispose = () => {
|
||||
if (closed) return;
|
||||
// Mark the stream closed before invoking user cleanup. A cleanup can
|
||||
// synchronously publish one final event, which must become a no-op.
|
||||
closed = true;
|
||||
signal.removeEventListener("abort", close);
|
||||
runCleanup();
|
||||
};
|
||||
|
||||
function send({ event = "message", data }: SendFunctionArgs) {
|
||||
if (closed) return;
|
||||
controller.enqueue(encoder.encode(`event: ${event}\n`));
|
||||
controller.enqueue(encoder.encode(`data: ${data}\n\n`));
|
||||
}
|
||||
|
||||
let cleanup = init(send, close);
|
||||
|
||||
let closed = false;
|
||||
|
||||
function close() {
|
||||
if (closed) return;
|
||||
cleanup();
|
||||
closed = true;
|
||||
signal.removeEventListener("abort", close);
|
||||
dispose();
|
||||
controller.close();
|
||||
}
|
||||
|
||||
signal.addEventListener("abort", close);
|
||||
cleanup = init(send, close);
|
||||
|
||||
// `init` may synchronously close the stream before returning cleanup.
|
||||
if (closed) runCleanup();
|
||||
|
||||
if (signal.aborted) return close();
|
||||
},
|
||||
cancel() {
|
||||
// Cancellation already closes the Web Stream controller. Only dispose
|
||||
// subscriptions/timers here; calling controller.close() would throw.
|
||||
dispose();
|
||||
},
|
||||
});
|
||||
|
||||
let headers = new Headers(options.headers);
|
||||
const headers = new Headers(options.headers);
|
||||
|
||||
if (headers.has("Content-Type")) {
|
||||
// eslint-disable-next-line no-console
|
||||
|
||||
Reference in new issue
Block a user