Skip to content

Commit

Permalink
Apply update in status handler
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkirsz committed Nov 2, 2022
1 parent 33c264a commit 4dd5daa
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions packages/react-refresh-utils/internal/helpers.ts
Expand Up @@ -144,38 +144,41 @@ function scheduleUpdate() {
if (isUpdateScheduled) {
return
}
isUpdateScheduled = true

function canApplyUpdate(status: ModuleHotStatus) {
return status === 'idle'
}

let applyUpdate = canApplyUpdate(module.hot.status())

const statusHandler = (status) => {
applyUpdate = canApplyUpdate(status)
function applyUpdate() {
isUpdateScheduled = false
try {
RefreshRuntime.performReactRefresh()
} catch (err) {
console.warn(
'Warning: Failed to re-render. We will retry on the next Fast Refresh event.\n' +
err
)
}
}

module.hot.addStatusHandler(statusHandler)
if (canApplyUpdate(module.hot.status())) {
// Apply update on the next tick.
Promise.resolve().then(() => {
applyUpdate()
})
return
}

isUpdateScheduled = true
Promise.resolve().then(() => {
isUpdateScheduled = false
module.hot.removeStatusHandler(statusHandler)

if (applyUpdate) {
try {
RefreshRuntime.performReactRefresh()
} catch (err) {
console.warn(
'Warning: Failed to re-render. We will retry on the next Fast Refresh event.\n' +
err
)
}
return
const statusHandler = (status) => {
if (canApplyUpdate(status)) {
module.hot.removeStatusHandler(statusHandler)
applyUpdate()
}
}

return scheduleUpdate()
})
// Apply update once the HMR runtime's status is idle.
module.hot.addStatusHandler(statusHandler)
}

// Needs to be compatible with IE11
Expand Down

0 comments on commit 4dd5daa

Please sign in to comment.