Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jan 21, 2021
1 parent a1515fa commit 988cf8d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 45 deletions.
34 changes: 21 additions & 13 deletions hot/lazy-compilation-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,28 @@ if (!module.hot) {
}

var urlBase = decodeURIComponent(__resourceQuery.slice(1));
exports.keepAlive = function (key) {
exports.keepAlive = function (options) {
var data = options.data;
var onError = options.onError;
var response;
require("http")
.request(
urlBase + key,
{
agent: false,
headers: { accept: "text/event-stream" }
},
function (res) {
response = res;
}
)
.end();
var request = require("http").request(
urlBase + data,
{
agent: false,
headers: { accept: "text/event-stream" }
},
function (res) {
response = res;
response.on("error", errorHandler);
}
);
function errorHandler(err) {
err.message =
"Problem communicating active modules to the server: " + err.message;
onError(err);
}
request.on("error", errorHandler);
request.end();
return function () {
response.destroy();
};
Expand Down
45 changes: 36 additions & 9 deletions hot/lazy-compilation-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,56 @@ if (typeof EventSource !== "function" || !module.hot) {
var urlBase = decodeURIComponent(__resourceQuery.slice(1));
var activeEventSource;
var activeKeys = new Map();
var errorHandlers = new Set();

var updateEventSource = function updateEventSource() {
if (activeEventSource) activeEventSource.close();
activeEventSource = new EventSource(
urlBase + Array.from(activeKeys.keys()).join("@")
);
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;
}
};

exports.keepAlive = function (key) {
var value = activeKeys.get(key) || 0;
activeKeys.set(key, value + 1);
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);
if (value === 0) {
updateEventSource();
}

return function () {
errorHandlers.delete(onError);
setTimeout(function () {
var value = activeKeys.get(key);
var value = activeKeys.get(data);
if (value === 1) {
activeKeys.delete(key);
activeKeys.delete(data);
updateEventSource();
} else {
activeKeys.set(key, value - 1);
activeKeys.set(data, value - 1);
}
}, 1000);
};
Expand Down
17 changes: 10 additions & 7 deletions lib/hmr/LazyCompilationPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ class LazyCompilationProxyModule extends Module {
runtimeRequirements
})}`,
`var data = ${JSON.stringify(this.data)};`,
`var dispose = client.keepAlive(data, ${JSON.stringify(
`var dispose = client.keepAlive({ data, active: ${JSON.stringify(
!!block
)}, module);`
)}, module, onError });`
]);
let source;
if (block) {
Expand All @@ -203,18 +203,21 @@ class LazyCompilationProxyModule extends Module {
message: "import()",
runtimeRequirements
})};`,
"if (module.hot.data && module.hot.data.resolveSelf) module.hot.data.resolveSelf(module.exports);"
"if (module.hot.data && module.hot.data.resolveSelf) module.hot.data.resolveSelf(module.exports);",
"function onError() { /* ignore */ }",
keepActive
]);
} else {
source = Template.asString([
"module.hot.accept();",
"var resolveSelf;",
`module.exports = new Promise(function(resolve) { resolveSelf = resolve; });`,
"var resolveSelf, onError;",
`module.exports = new Promise(function(resolve, reject) { resolveSelf = resolve; onError = reject; });`,
"if (module.hot.data && module.hot.data.resolveSelf) module.hot.data.resolveSelf(module.exports);",
"module.hot.dispose(function(data) { data.resolveSelf = resolveSelf; dispose(data); });"
"module.hot.dispose(function(data) { data.resolveSelf = resolveSelf; dispose(data); });",
keepActive
]);
}
sources.set("javascript", new RawSource(keepActive + "\n\n" + source));
sources.set("javascript", new RawSource(source));
return {
sources,
runtimeRequirements
Expand Down
32 changes: 17 additions & 15 deletions test/helpers/EventSourceForNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@
module.exports = class EventSource {
constructor(url) {
this.response = undefined;
require("http")
.request(
url,
{
agent: false,
headers: { accept: "text/event-stream" }
},
res => {
this.response = res;
res.on("error", err => {
if (this.onerror) this.onerror(err);
});
}
)
.end();
const request = require("http").request(
url,
{
agent: false,
headers: { accept: "text/event-stream" }
},
res => {
this.response = res;
res.on("error", err => {
if (this.onerror) this.onerror(err);
});
}
);
request.on("error", err => {
if (this.onerror) this.onerror({ message: err });
});
request.end();
}

close() {
Expand Down
4 changes: 3 additions & 1 deletion test/hotCases/lazy-compilation/simple/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ it("should compile to lazy imported module", done => {
require("../../update")(done, true, () => {
promise.then(result => {
expect(result).toHaveProperty("default", 42);
done();
setTimeout(() => {
done();
}, 1000);
}, done);
})
);
Expand Down

0 comments on commit 988cf8d

Please sign in to comment.