Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
fix: close eventSource on stream end (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT authored Oct 20, 2023
1 parent 697be72 commit 2cdfc8c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/async/createTsonAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { TsonAsyncOptions } from "./asyncTypes.js";
import {
createTsonParseAsync,
createTsonParseEventSource,
createTsonParseJsonStreamResponse,
} from "./deserializeAsync.js";
import {
createTsonSSEResponse,
createTsonSerializeJsonStreamResponse,
createTsonStreamAsync,
} from "./serializeAsync.js";

Expand All @@ -13,8 +15,10 @@ import {
* @internal
*/
export const createTsonAsync = (opts: TsonAsyncOptions) => ({
createEventSourceParser: createTsonParseEventSource(opts),
createEventSource: createTsonParseEventSource(opts),
fromJsonStreamResponse: createTsonParseJsonStreamResponse(opts),
parseJsonStream: createTsonParseAsync(opts),
stringifyJsonStream: createTsonStreamAsync(opts),
toJsonStreamResponse: createTsonSerializeJsonStreamResponse,
toSSEResponse: createTsonSSEResponse(opts),
});
5 changes: 1 addition & 4 deletions src/async/deserializeAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,14 @@ export function createTsonParseEventSource(opts: TsonAsyncOptions) {

signal?.addEventListener("abort", onAbort);

// eventSource.addEventListener("head", (e) => {
// controller.enqueue(JSON.parse(e.data));
// });

eventSource.onmessage = (msg) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
controller.enqueue(JSON.parse(msg.data));
};

eventSource.addEventListener("close", () => {
controller.close();
eventSource.close();
});

const iterable = readableStreamToAsyncIterable(stream);
Expand Down
2 changes: 1 addition & 1 deletion src/async/sse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ test("SSE response test", async () => {
{
// e2e
const ac = new AbortController();
const shape = await tson.createEventSourceParser<MockObj>(server.url, {
const shape = await tson.createEventSource<MockObj>(server.url, {
signal: ac.signal,
});

Expand Down

0 comments on commit 2cdfc8c

Please sign in to comment.