Skip to content

Commit

Permalink
fix(jsx): race condition in ErrorBoundary with event loop (#3343)
Browse files Browse the repository at this point in the history
Fixes #3333
  • Loading branch information
usualoma authored Aug 31, 2024
1 parent 040b0d4 commit 0712530
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/jsx/components.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 8 additions & 2 deletions src/jsx/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0712530

Please sign in to comment.