Skip to content

Commit

Permalink
fix: streams not closing properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Vali-98 committed Dec 5, 2024
1 parent 657947d commit bdcb08f
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion constants/SSEFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))
Expand Down

0 comments on commit bdcb08f

Please sign in to comment.