Skip to content

Commit

Permalink
Clean up channel.onmessage when stream terminates
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlong committed Dec 5, 2024
1 parent e503a77 commit 3f9f336
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion plugins/http/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion plugins/http/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,11 @@ export async function fetch(
rid
})

const channel = new Channel<number[]>()

// Create ReadableStream from channel messages
const stream = new ReadableStream({
start(controller) {
const channel = new Channel<number[]>()
channel.onmessage = (arr) => {
const chunk = new Uint8Array(arr)

Expand All @@ -230,8 +231,12 @@ export async function fetch(
// If the promise fails, make sure the stream is closed
readPromise.catch((e) => {
console.error('error reading body', e)
channel.onmessage = () => {}
controller.error(e)
})
},
cancel() {
channel.onmessage = () => {}
}
})

Expand Down

0 comments on commit 3f9f336

Please sign in to comment.