|
|
import { |
|
|
isTerminalLoggingEnabled, |
|
|
logQueue, |
|
|
} from '../../../../next-devtools/userspace/app/forward-logs' |
|
|
import { |
|
|
HMR_ACTIONS_SENT_TO_BROWSER, |
|
|
type HMR_ACTION_TYPES, |
|
|
} from '../../../../server/dev/hot-reloader-types' |
|
|
import { getSocketUrl } from '../get-socket-url' |
|
|
|
|
|
let source: WebSocket |
|
|
|
|
|
type ActionCallback = (action: HMR_ACTION_TYPES) => void |
|
|
|
|
|
const eventCallbacks: Array<ActionCallback> = [] |
|
|
|
|
|
export function addMessageListener(callback: ActionCallback) { |
|
|
eventCallbacks.push(callback) |
|
|
} |
|
|
|
|
|
export function sendMessage(data: string) { |
|
|
if (!source || source.readyState !== source.OPEN) return |
|
|
return source.send(data) |
|
|
} |
|
|
|
|
|
let reconnections = 0 |
|
|
let reloading = false |
|
|
let serverSessionId: number | null = null |
|
|
|
|
|
export function connectHMR(options: { path: string; assetPrefix: string }) { |
|
|
function init() { |
|
|
if (source) source.close() |
|
|
|
|
|
function handleOnline() { |
|
|
if (isTerminalLoggingEnabled) { |
|
|
logQueue.onSocketReady(source) |
|
|
} |
|
|
reconnections = 0 |
|
|
window.console.log('[HMR] connected') |
|
|
} |
|
|
|
|
|
function handleMessage(event: MessageEvent<string>) { |
|
|
|
|
|
|
|
|
if (reloading) { |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
const msg: HMR_ACTION_TYPES = JSON.parse(event.data) |
|
|
|
|
|
if ( |
|
|
'action' in msg && |
|
|
msg.action === HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_CONNECTED |
|
|
) { |
|
|
if ( |
|
|
serverSessionId !== null && |
|
|
serverSessionId !== msg.data.sessionId |
|
|
) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
window.location.reload() |
|
|
|
|
|
reloading = true |
|
|
return |
|
|
} |
|
|
|
|
|
serverSessionId = msg.data.sessionId |
|
|
} |
|
|
|
|
|
for (const eventCallback of eventCallbacks) { |
|
|
eventCallback(msg) |
|
|
} |
|
|
} |
|
|
|
|
|
let timer: ReturnType<typeof setTimeout> |
|
|
function handleDisconnect() { |
|
|
source.onerror = null |
|
|
source.onclose = null |
|
|
source.close() |
|
|
reconnections++ |
|
|
|
|
|
if (reconnections > 25) { |
|
|
reloading = true |
|
|
window.location.reload() |
|
|
return |
|
|
} |
|
|
|
|
|
clearTimeout(timer) |
|
|
|
|
|
timer = setTimeout(init, reconnections > 5 ? 5000 : 1000) |
|
|
} |
|
|
|
|
|
const url = getSocketUrl(options.assetPrefix) |
|
|
|
|
|
source = new window.WebSocket(`${url}${options.path}`) |
|
|
source.onopen = handleOnline |
|
|
source.onerror = handleDisconnect |
|
|
source.onclose = handleDisconnect |
|
|
source.onmessage = handleMessage |
|
|
} |
|
|
|
|
|
init() |
|
|
} |
|
|
|