Use an EventEmitter instead of `window.addEventListener`

This commit is contained in:
Spencer Elliott 2015-10-17 17:46:42 -04:00
parent c6ccf9d3a0
commit 70f01242f1
3 changed files with 18 additions and 28 deletions

View File

@ -4,9 +4,9 @@
*/
/*globals window __webpack_hash__ */
if(module.hot) {
var lastData;
var lastHash;
var upToDate = function upToDate() {
return lastData.indexOf(__webpack_hash__) >= 0;
return lastHash.indexOf(__webpack_hash__) >= 0;
};
var check = function check() {
module.hot.check(true).then(function(updatedModules) {
@ -40,18 +40,12 @@ if(module.hot) {
}
});
};
var addEventListener = window.addEventListener ? function(eventName, listener) {
window.addEventListener(eventName, listener, false);
} : function(eventName, listener) {
window.attachEvent("on" + eventName, listener);
};
addEventListener("message", function(event) {
if(typeof event.data === "string" && event.data.indexOf("webpackHotUpdate") === 0) {
lastData = event.data;
if(!upToDate() && module.hot.status() === "idle") {
console.log("[HMR] Checking for updates on the server...");
check();
}
var hotEmitter = require("./emitter");
hotEmitter.on("webpackHotUpdate", function(currentHash) {
lastHash = currentHash;
if(!upToDate() && module.hot.status() === "idle") {
console.log("[HMR] Checking for updates on the server...");
check();
}
});
console.log("[HMR] Waiting for update signal from WDS...");

2
hot/emitter.js Normal file
View File

@ -0,0 +1,2 @@
var EventEmitter = require("events");
module.exports = new EventEmitter();

View File

@ -4,9 +4,9 @@
*/
/*globals window __webpack_hash__ */
if(module.hot) {
var lastData;
var lastHash;
var upToDate = function upToDate() {
return lastData.indexOf(__webpack_hash__) >= 0;
return lastHash.indexOf(__webpack_hash__) >= 0;
};
var check = function check() {
module.hot.check().then(function(updatedModules) {
@ -54,18 +54,12 @@ if(module.hot) {
}
});
};
var addEventListener = window.addEventListener ? function(eventName, listener) {
window.addEventListener(eventName, listener, false);
} : function(eventName, listener) {
window.attachEvent("on" + eventName, listener);
};
addEventListener("message", function(event) {
if(typeof event.data === "string" && event.data.indexOf("webpackHotUpdate") === 0) {
lastData = event.data;
if(!upToDate() && module.hot.status() === "idle") {
console.log("[HMR] Checking for updates on the server...");
check();
}
var hotEmitter = require("./emitter");
hotEmitter.on("webpackHotUpdate", function(currentHash) {
lastHash = currentHash;
if(!upToDate() && module.hot.status() === "idle") {
console.log("[HMR] Checking for updates on the server...");
check();
}
});
console.log("[HMR] Waiting for update signal from WDS...");