0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-21 18:18:57 +01:00

fix(jsx): race condition in ErrorBoundary with event loop (#3343)

Fixes #3333
This commit is contained in:
Taku Amano 2024-08-31 16:46:32 +09:00 committed by GitHub
parent 040b0d41de
commit 07125309b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 2 deletions

View File

@ -191,6 +191,34 @@ describe('ErrorBoundary', () => {
})
})
describe('async : setTimeout', async () => {
const TimeoutSuccessComponent = async () => {
await new Promise((resolve) => setTimeout(resolve, 10))
return <div>OK</div>
}
const TimeoutErrorComponent = async () => {
await new Promise((resolve) => setTimeout(resolve, 0))
throw new Error('Error')
}
it('fallback', async () => {
const html = (
<>
<TimeoutSuccessComponent />
<ErrorBoundary fallback={<Fallback />}>
<TimeoutErrorComponent />
</ErrorBoundary>
</>
).toString()
expect((await resolveCallback(await html)).toString()).toEqual(
'<div>OK</div><div>Out Of Service</div>'
)
suspenseCounter--
})
})
describe('streaming', async () => {
const Component = async ({ error }: { error?: boolean }) => {
await new Promise((resolve) => setTimeout(resolve, 10))

View File

@ -93,13 +93,19 @@ d.replaceWith(c.content)
})(document)
</script>`
}
let error: unknown
const promiseAll = Promise.all(resArray).catch((e) => (error = e))
return raw(`<template id="E:${index}"></template><!--E:${index}-->`, [
({ phase, buffer, context }) => {
if (phase === HtmlEscapedCallbackPhase.BeforeStream) {
return
}
return Promise.all(resArray)
.then(async (htmlArray) => {
return promiseAll
.then(async (htmlArray: HtmlEscapedString[]) => {
if (error) {
throw error
}
htmlArray = htmlArray.flat()
const content = htmlArray.join('')
let html = buffer