-
-
Notifications
You must be signed in to change notification settings - Fork 637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement stream.onAbort #1871
Conversation
Is this ready to review? |
Happy new year @yusukebe. |
Happy New Year! I've checked it and tried to use it in some environment. But is this expected behavior? Area.mp4The code: import { serve } from '@hono/node-server'
import { Hono } from '../../src'
import { streamText } from '../../src/helper/streaming'
export const app = new Hono()
app.get('/', async (c) => {
return streamText(c, async (stream) => {
stream.onAbort(async () => {
console.log('Aborted!')
await stream.close()
})
for (;;) {
stream.writeln('Hello World!')
console.log('Hello World!')
await new Promise((r) => setTimeout(r, 1000))
}
})
})
serve(app) |
@yusukebe in your example, the loop still goes on, but there is no logic to stop the loop after the stream is aborted. You could make it a while loop instead of a for loop, and check for a variable (e.g. |
So you are saying that it should be code like this? I understand! app.get('/', async (c) => {
let aborted = false
return streamText(c, async (stream) => {
stream.onAbort(async () => {
console.log('Aborted!')
aborted = true
})
while (!aborted) {
stream.writeln('Hello World!')
console.log('Hello World!')
await new Promise((r) => setTimeout(r, 1000))
}
})
}) |
@yusukebe yeah. Give it a try to make sure that works |
It works well! What do you think of this PR? Looks good for you? If @sor4chi can check again, I'd like to merge this into the next branch. |
@yusukebe So I'd like to leave it as it is since I think I can handle it by implementing it myself if I want to do it as @HeyITGuyFixIt has presented it. |
@yusukebe it looks good to me |
@sor4chi @HeyITGuyFixIt Thanks! Merge now. |
* feat: extend `StreamAPI` for response interception and adapt stream handler for it * chore: denoify
Author should do the followings, if applicable
yarn denoify
to generate files for Denorelated: #1795 #1770