Skip to content

Commit

Permalink
reenable deduplication for some hmr events
Browse files Browse the repository at this point in the history
aggregate turbopack events
  • Loading branch information
sokra committed Sep 14, 2023
1 parent 4601bc2 commit 61b2132
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/next/src/server/dev/hot-reloader-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface ServerErrorAction {

interface TurboPackMessageAction {
type: HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_MESSAGE
data: TurbopackUpdate
data: TurbopackUpdate | TurbopackUpdate[]
}

interface BuildingAction {
Expand Down
40 changes: 25 additions & 15 deletions packages/next/src/server/lib/router-utils/setup-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ import {
ReloadPageAction,
TurboPackConnectedAction,
} from '../../dev/hot-reloader-types'
import type { Update as TurbopackUpdate } from '../../../build/swc'
import { debounce } from '../../utils'
import {
deleteAppClientCache,
Expand Down Expand Up @@ -227,7 +228,8 @@ async function startWatcher(opts: SetupOpts) {
let currentEntriesHandling = new Promise(
(resolve) => (currentEntriesHandlingResolve = resolve)
)
const hmrPayloads = new Map<string, HMR_ACTION_TYPES[]>()
const hmrPayloads = new Map<string, HMR_ACTION_TYPES>()
const turbopackUpdates: TurbopackUpdate[] = []
let hmrBuilding = false

const issues = new Map<string, Map<string, Issue>>()
Expand Down Expand Up @@ -362,12 +364,17 @@ async function startWatcher(opts: SetupOpts) {
hmrBuilding = false

if (errors.size === 0) {
for (const payloads of hmrPayloads.values()) {
for (const payload of payloads) {
hotReloader.send(payload)
}
for (const payload of hmrPayloads.values()) {
hotReloader.send(payload)
}
hmrPayloads.clear()
if (turbopackUpdates.length > 0) {
hotReloader.send({
type: HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_MESSAGE,
data: turbopackUpdates,
})
turbopackUpdates.length = 0
}
}
}, 2)

Expand All @@ -379,13 +386,19 @@ async function startWatcher(opts: SetupOpts) {
hotReloader.send({ action: HMR_ACTIONS_SENT_TO_BROWSER.BUILDING })
hmrBuilding = true
}
let k = `${key}:${id}`
let list = hmrPayloads.get(k)
if (!list) {
list = []
hmrPayloads.set(k, list)
hmrPayloads.set(`${key}:${id}`, payload)
sendHmrDebounce()
}

function sendTurbopackMessage(payload: TurbopackUpdate) {
// We've detected a change in some part of the graph. If nothing has
// been inserted into building yet, then this is the first change
// emitted, but their may be many more coming.
if (!hmrBuilding) {
hotReloader.send({ action: HMR_ACTIONS_SENT_TO_BROWSER.BUILDING })
hmrBuilding = true
}
list.push(payload)
turbopackUpdates.push(payload)
sendHmrDebounce()
}

Expand Down Expand Up @@ -845,10 +858,7 @@ async function startWatcher(opts: SetupOpts) {

for await (const data of subscription) {
processIssues(id, data)
sendHmr('hmr-event', id, {
type: HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_MESSAGE,
data,
})
sendTurbopackMessage(data)
}
}

Expand Down

0 comments on commit 61b2132

Please sign in to comment.