104 lines
3.7 KiB
JavaScript
104 lines
3.7 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
0 && (module.exports = {
|
|
addMessageListener: null,
|
|
connectHMR: null,
|
|
sendMessage: null
|
|
});
|
|
function _export(target, all) {
|
|
for(var name in all)Object.defineProperty(target, name, {
|
|
enumerable: true,
|
|
get: all[name]
|
|
});
|
|
}
|
|
_export(exports, {
|
|
addMessageListener: function() {
|
|
return addMessageListener;
|
|
},
|
|
connectHMR: function() {
|
|
return connectHMR;
|
|
},
|
|
sendMessage: function() {
|
|
return sendMessage;
|
|
}
|
|
});
|
|
const _hotreloadertypes = require("../../../../server/dev/hot-reloader-types");
|
|
const _getsocketurl = require("../utils/get-socket-url");
|
|
let source;
|
|
const eventCallbacks = [];
|
|
function addMessageListener(callback) {
|
|
eventCallbacks.push(callback);
|
|
}
|
|
function sendMessage(data) {
|
|
if (!source || source.readyState !== source.OPEN) return;
|
|
return source.send(data);
|
|
}
|
|
let reconnections = 0;
|
|
let reloading = false;
|
|
let serverSessionId = null;
|
|
function connectHMR(options) {
|
|
function init() {
|
|
if (source) source.close();
|
|
function handleOnline() {
|
|
reconnections = 0;
|
|
window.console.log('[HMR] connected');
|
|
}
|
|
function handleMessage(event) {
|
|
// While the page is reloading, don't respond to any more messages.
|
|
// On reconnect, the server may send an empty list of changes if it was restarted.
|
|
if (reloading) {
|
|
return;
|
|
}
|
|
// Coerce into HMR_ACTION_TYPES as that is the format.
|
|
const msg = JSON.parse(event.data);
|
|
if ('action' in msg && msg.action === _hotreloadertypes.HMR_ACTIONS_SENT_TO_BROWSER.TURBOPACK_CONNECTED) {
|
|
if (serverSessionId !== null && serverSessionId !== msg.data.sessionId) {
|
|
// Either the server's session id has changed and it's a new server, or
|
|
// it's been too long since we disconnected and we should reload the page.
|
|
// There could be 1) unhandled server errors and/or 2) stale content.
|
|
// Perform a hard reload of the page.
|
|
window.location.reload();
|
|
reloading = true;
|
|
return;
|
|
}
|
|
serverSessionId = msg.data.sessionId;
|
|
}
|
|
for (const eventCallback of eventCallbacks){
|
|
eventCallback(msg);
|
|
}
|
|
}
|
|
let timer;
|
|
function handleDisconnect() {
|
|
source.onerror = null;
|
|
source.onclose = null;
|
|
source.close();
|
|
reconnections++;
|
|
// After 25 reconnects we'll want to reload the page as it indicates the dev server is no longer running.
|
|
if (reconnections > 25) {
|
|
reloading = true;
|
|
window.location.reload();
|
|
return;
|
|
}
|
|
clearTimeout(timer);
|
|
// Try again after 5 seconds
|
|
timer = setTimeout(init, reconnections > 5 ? 5000 : 1000);
|
|
}
|
|
const url = (0, _getsocketurl.getSocketUrl)(options.assetPrefix);
|
|
source = new window.WebSocket("" + url + options.path);
|
|
source.onopen = handleOnline;
|
|
source.onerror = handleDisconnect;
|
|
source.onclose = handleDisconnect;
|
|
source.onmessage = handleMessage;
|
|
}
|
|
init();
|
|
}
|
|
|
|
if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
|
|
Object.defineProperty(exports.default, '__esModule', { value: true });
|
|
Object.assign(exports.default, exports);
|
|
module.exports = exports.default;
|
|
}
|
|
|
|
//# sourceMappingURL=websocket.js.map
|