Skip to content

Commit

Permalink
Use an EventEmitter instead of window.addEventListener
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottsj committed Nov 3, 2015
1 parent c6ccf9d commit 70f0124
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 28 deletions.
22 changes: 8 additions & 14 deletions hot/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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...");
Expand Down
2 changes: 2 additions & 0 deletions hot/emitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var EventEmitter = require("events");
module.exports = new EventEmitter();
22 changes: 8 additions & 14 deletions hot/only-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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...");
Expand Down

0 comments on commit 70f0124

Please sign in to comment.