Skip to content

Commit

Permalink
hackily try to load hmr'd modules in series
Browse files Browse the repository at this point in the history
  • Loading branch information
rashfael committed Nov 22, 2022
1 parent f40c18d commit 8a894ac
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ async function handleMessage(payload: HMRPayload) {
await Promise.all(
payload.updates.map(async (update): Promise<void> => {
if (update.type === 'js-update') {
return queueUpdate(fetchUpdate(update))
return queueUpdate(() => fetchUpdate(update))
}

// css-update
Expand Down Expand Up @@ -289,22 +289,24 @@ function hasErrorOverlay() {
}

let pending = false
let queued: Promise<(() => void) | undefined>[] = []
let queued: (() => Promise<(() => void) | undefined>)[] = []

/**
* buffer multiple hot updates triggered by the same src change
* so that they are invoked in the same order they were sent.
* (otherwise the order may be inconsistent because of the http request round trip)
*/
async function queueUpdate(p: Promise<(() => void) | undefined>) {
async function queueUpdate(p: () => Promise<(() => void) | undefined>) {
queued.push(p)
if (!pending) {
pending = true
await Promise.resolve()
pending = false
const loading = [...queued]
queued = []
;(await Promise.all(loading)).forEach((fn) => fn && fn())
for (const p of loading) {
(await p())?.();
}
}
}

Expand Down

0 comments on commit 8a894ac

Please sign in to comment.