Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita40ae2e

Browse files
authored
Fix race condition inMarkdownHooks
Running asynchronous code inside a `useEffect` causes race conditions.This is now handled correctly.ClosesGH-896.Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parentad7f37f commita40ae2e

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

‎lib/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,18 @@ export function MarkdownHooks(options) {
207207

208208
useEffect(
209209
function(){
210+
letcancelled=false
210211
constfile=createFile(options)
211212
processor.run(processor.parse(file),file,function(error,tree){
212-
setError(error)
213-
setTree(tree)
213+
if(!cancelled){
214+
setError(error)
215+
setTree(tree)
216+
}
214217
})
218+
219+
return()=>{
220+
cancelled=true
221+
}
215222
},
216223
[
217224
options.children,

‎test.jsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,27 @@ test('MarkdownHooks', async function (t) {
11651165
})
11661166
assert.equal(container.innerHTML,'Error: rejected')
11671167
})
1168+
1169+
awaitt.test('should support `MarkdownHooks` rerenders',asyncfunction(){
1170+
constpluginA=deferPlugin()
1171+
constpluginB=deferPlugin()
1172+
1173+
constresult=render(
1174+
<MarkdownHookschildren={'a'}rehypePlugins={[pluginA.plugin]}/>
1175+
)
1176+
1177+
result.rerender(
1178+
<MarkdownHookschildren={'b'}rehypePlugins={[pluginB.plugin]}/>
1179+
)
1180+
1181+
assert.equal(result.container.innerHTML,'')
1182+
pluginB.resolve()
1183+
pluginA.resolve()
1184+
awaitwaitFor(()=>{
1185+
assert.notEqual(result.container.innerHTML,'')
1186+
})
1187+
assert.equal(result.container.innerHTML,'<p>b</p>')
1188+
})
11681189
})
11691190

11701191
/**

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp