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