webpack/hot/lazy-compilation-web.js

68 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-01-21 23:32:49 +08:00
/* global __resourceQuery */
"use strict";
if (typeof EventSource !== "function" || !module.hot) {
throw new Error(
"Environment doesn't support lazy compilation (requires EventSource and Hot Module Replacement enabled)"
);
}
var urlBase = decodeURIComponent(__resourceQuery.slice(1));
var activeEventSource;
var activeKeys = new Map();
2021-01-22 04:05:23 +08:00
var errorHandlers = new Set();
2021-01-21 23:32:49 +08:00
var updateEventSource = function updateEventSource() {
if (activeEventSource) activeEventSource.close();
2021-01-22 04:05:23 +08:00
if (activeKeys.size) {
activeEventSource = new EventSource(
urlBase + Array.from(activeKeys.keys()).join("@")
);
activeEventSource.onerror = function (event) {
errorHandlers.forEach(function (onError) {
onError(
new Error(
"Problem communicating active modules to the server: " +
event.message +
" " +
event.filename +
":" +
event.lineno +
":" +
event.colno +
" " +
event.error
)
);
});
};
} else {
activeEventSource = undefined;
}
2021-01-21 23:32:49 +08:00
};
2021-01-22 04:05:23 +08:00
exports.keepAlive = function (options) {
var data = options.data;
var onError = options.onError;
errorHandlers.add(onError);
var value = activeKeys.get(data) || 0;
activeKeys.set(data, value + 1);
2021-01-21 23:32:49 +08:00
if (value === 0) {
updateEventSource();
}
return function () {
2021-01-22 04:05:23 +08:00
errorHandlers.delete(onError);
2021-01-21 23:32:49 +08:00
setTimeout(function () {
2021-01-22 04:05:23 +08:00
var value = activeKeys.get(data);
2021-01-21 23:32:49 +08:00
if (value === 1) {
2021-01-22 04:05:23 +08:00
activeKeys.delete(data);
2021-01-21 23:32:49 +08:00
updateEventSource();
} else {
2021-01-22 04:05:23 +08:00
activeKeys.set(data, value - 1);
2021-01-21 23:32:49 +08:00
}
}, 1000);
};
};