Skip to content

Commit 70f0124

Browse files
committed
Use an EventEmitter instead of window.addEventListener
1 parent c6ccf9d commit 70f0124

3 files changed

Lines changed: 18 additions & 28 deletions

File tree

hot/dev-server.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
*/
55
/*globals window __webpack_hash__ */
66
if(module.hot) {
7-
var lastData;
7+
var lastHash;
88
var upToDate = function upToDate() {
9-
return lastData.indexOf(__webpack_hash__) >= 0;
9+
return lastHash.indexOf(__webpack_hash__) >= 0;
1010
};
1111
var check = function check() {
1212
module.hot.check(true).then(function(updatedModules) {
@@ -40,18 +40,12 @@ if(module.hot) {
4040
}
4141
});
4242
};
43-
var addEventListener = window.addEventListener ? function(eventName, listener) {
44-
window.addEventListener(eventName, listener, false);
45-
} : function(eventName, listener) {
46-
window.attachEvent("on" + eventName, listener);
47-
};
48-
addEventListener("message", function(event) {
49-
if(typeof event.data === "string" && event.data.indexOf("webpackHotUpdate") === 0) {
50-
lastData = event.data;
51-
if(!upToDate() && module.hot.status() === "idle") {
52-
console.log("[HMR] Checking for updates on the server...");
53-
check();
54-
}
43+
var hotEmitter = require("./emitter");
44+
hotEmitter.on("webpackHotUpdate", function(currentHash) {
45+
lastHash = currentHash;
46+
if(!upToDate() && module.hot.status() === "idle") {
47+
console.log("[HMR] Checking for updates on the server...");
48+
check();
5549
}
5650
});
5751
console.log("[HMR] Waiting for update signal from WDS...");

hot/emitter.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var EventEmitter = require("events");
2+
module.exports = new EventEmitter();

hot/only-dev-server.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
*/
55
/*globals window __webpack_hash__ */
66
if(module.hot) {
7-
var lastData;
7+
var lastHash;
88
var upToDate = function upToDate() {
9-
return lastData.indexOf(__webpack_hash__) >= 0;
9+
return lastHash.indexOf(__webpack_hash__) >= 0;
1010
};
1111
var check = function check() {
1212
module.hot.check().then(function(updatedModules) {
@@ -54,18 +54,12 @@ if(module.hot) {
5454
}
5555
});
5656
};
57-
var addEventListener = window.addEventListener ? function(eventName, listener) {
58-
window.addEventListener(eventName, listener, false);
59-
} : function(eventName, listener) {
60-
window.attachEvent("on" + eventName, listener);
61-
};
62-
addEventListener("message", function(event) {
63-
if(typeof event.data === "string" && event.data.indexOf("webpackHotUpdate") === 0) {
64-
lastData = event.data;
65-
if(!upToDate() && module.hot.status() === "idle") {
66-
console.log("[HMR] Checking for updates on the server...");
67-
check();
68-
}
57+
var hotEmitter = require("./emitter");
58+
hotEmitter.on("webpackHotUpdate", function(currentHash) {
59+
lastHash = currentHash;
60+
if(!upToDate() && module.hot.status() === "idle") {
61+
console.log("[HMR] Checking for updates on the server...");
62+
check();
6963
}
7064
});
7165
console.log("[HMR] Waiting for update signal from WDS...");

0 commit comments

Comments
 (0)