From 70f01242f11f88706f40086654de6819539dbad4 Mon Sep 17 00:00:00 2001 From: Spencer Elliott Date: Sat, 17 Oct 2015 17:46:42 -0400 Subject: [PATCH] Use an EventEmitter instead of `window.addEventListener` --- hot/dev-server.js | 22 ++++++++-------------- hot/emitter.js | 2 ++ hot/only-dev-server.js | 22 ++++++++-------------- 3 files changed, 18 insertions(+), 28 deletions(-) create mode 100644 hot/emitter.js diff --git a/hot/dev-server.js b/hot/dev-server.js index a84690346..48a151b0c 100644 --- a/hot/dev-server.js +++ b/hot/dev-server.js @@ -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..."); diff --git a/hot/emitter.js b/hot/emitter.js new file mode 100644 index 000000000..05e0fbe09 --- /dev/null +++ b/hot/emitter.js @@ -0,0 +1,2 @@ +var EventEmitter = require("events"); +module.exports = new EventEmitter(); diff --git a/hot/only-dev-server.js b/hot/only-dev-server.js index f2aff7aa5..30e00b78f 100644 --- a/hot/only-dev-server.js +++ b/hot/only-dev-server.js @@ -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...");