From bdcb08f7a707a82269bc64f40ea6c7fa882615bb Mon Sep 17 00:00:00 2001 From: Vali98 Date: Thu, 5 Dec 2024 13:14:32 +0800 Subject: [PATCH] fix: streams not closing properly --- constants/SSEFetch.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/constants/SSEFetch.ts b/constants/SSEFetch.ts index 68b6247..1bf39ec 100644 --- a/constants/SSEFetch.ts +++ b/constants/SSEFetch.ts @@ -13,9 +13,12 @@ export class SSEFetch { private onEvent = (data: string) => {} private onError = () => {} private onClose = () => {} + private closeStream = () => {} public abort() { this.abortController.abort() + this.closeStream() + this.closeStream = () => {} } public async start(values: SSEValues) { @@ -29,8 +32,8 @@ export class SSEFetch { ...body, }).then(async (res) => { if (res.status !== 200 || !res.body) return this.onError() + this.closeStream = res.body.cancel for await (const chunk of res.body) { - if (this.abortController.signal.aborted) break const data = this.decoder.decode(chunk) const output = parseSSE(data) output.forEach((item) => this.onEvent(item))