Skip to content
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

Handle race conditions in MarkdownHooks #896

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,18 @@ export function MarkdownHooks(options) {

useEffect(
function () {
let cancelled = false
const file = createFile(options)
processor.run(processor.parse(file), file, function (error, tree) {
setError(error)
setTree(tree)
if (!cancelled) {
setError(error)
setTree(tree)
}
})

return () => {
cancelled = true
}
},
[
options.children,
Expand Down
21 changes: 21 additions & 0 deletions test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,27 @@ test('MarkdownHooks', async function (t) {
})
assert.equal(container.innerHTML, 'Error: rejected')
})

await t.test('should support `MarkdownHooks` rerenders', async function () {
const pluginA = deferPlugin()
const pluginB = deferPlugin()

const result = render(
<MarkdownHooks children={'a'} rehypePlugins={[pluginA.plugin]} />
)

result.rerender(
<MarkdownHooks children={'b'} rehypePlugins={[pluginB.plugin]} />
)

assert.equal(result.container.innerHTML, '')
pluginB.resolve()
pluginA.resolve()
await waitFor(() => {
assert.notEqual(result.container.innerHTML, '')
})
assert.equal(result.container.innerHTML, '<p>b</p>')
})
})

/**
Expand Down