Clean up Javascript (#225)

Clean up Javascript (#225)

  • REFACTOR: Make vars consistent

  • REFACTOR: gotData is unnecessary

  • REFACTOR: undefined is a weird variable name

  • REFACTOR: Reduce global usage

  • REFACTOR: remove noConflict

It never worked because it was relying on a variable that was never set properly. We’re moving to some kind of module definition anyway which should make this unnecessary.

  • Use exports like other defines
diff --git a/README.md b/README.md
index 10f0bdb..9cc5b5b 100644
--- a/README.md
+++ b/README.md
@@ -341,8 +341,6 @@ enableChunkedEncoding|true|Allows streaming of message bus data over the HTTP co
 
 `MessageBus.status()` : Returns status (started, paused, stopped)
 
-`MessageBus.noConflict()` : Removes MessageBus from the global namespace by replacing it with whatever was present before MessageBus was loaded. Returns a reference to the MessageBus object.
-
 `MessageBus.diagnostics()` : Returns a log that may be used for diagnostics on the status of message bus.
 
 #### Ruby
diff --git a/assets/message-bus.js b/assets/message-bus.js
index 5b87051..cdc9b23 100644
--- a/assets/message-bus.js
+++ b/assets/message-bus.js
@@ -1,55 +1,38 @@
 /*jshint bitwise: false*/
-(function(global, document, undefined) {
+(function(exports, document, jQuery) {
   "use strict";
-  var previousMessageBus = global.MessageBus;
 
   // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
-  var callbacks,
-    clientId,
-    failCount,
-    shouldLongPoll,
-    queue,
-    responseCallbacks,
-    uniqueId,
-    baseUrl;
-  var me,
-    started,
-    stopped,
-    longPoller,
-    pollTimeout,
-    paused,
-    later,
-    jQuery,
-    interval,
-    chunkedBackoff;
-  var delayPollTimeout;
-
-  var ajaxInProgress = false;
-
-  uniqueId = function() {
+  var uniqueId = function() {
     return "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(/[xy]/g, function(c) {
-      var r, v;
-      r = (Math.random() * 16) | 0;
-      v = c === "x" ? r : (r & 0x3) | 0x8;
+      var r = (Math.random() * 16) | 0;
+      var v = c === "x" ? r : (r & 0x3) | 0x8;
       return v.toString(16);
     });
   };
 
-  clientId = uniqueId();
-  responseCallbacks = {};
-  callbacks = [];
-  queue = [];
-  interval = null;
-  failCount = 0;
-  baseUrl = "/";
-  paused = false;
-  later = [];
-  chunkedBackoff = 0;
-  jQuery = global.jQuery;
-  var hiddenProperty;
-
-  (function() {
+  var me;
+  var delayPollTimeout;
+  var ajaxInProgress = false;
+  var started = false;
+  var clientId = uniqueId();
+  var callbacks = [];
+  var queue = [];
+  var interval = null;
+  var failCount = 0;
+  var baseUrl = "/";
+  var paused = false;
+  var later = [];
+  var chunkedBackoff = 0;
+  var stopped;
+  var pollTimeout = null;
+  var totalAjaxFailures = 0;
+  var totalAjaxCalls = 0;
+  var lastAjax;
+
+  var isHidden = (function() {
     var prefixes = ["", "webkit", "ms", "moz"];
+    var hiddenProperty;
     for (var i = 0; i < prefixes.length; i++) {
       var prefix = prefixes[i];
       var check = prefix + (prefix === "" ? "hidden" : "Hidden");
@@ -57,15 +40,15 @@
         hiddenProperty = check;
       }
     }
-  })();
 
-  var isHidden = function() {
-    if (hiddenProperty !== undefined) {
-      return document[hiddenProperty];
-    } else {
-      return !document.hasFocus;
-    }
-  };
+    return function() {
+      if (hiddenProperty !== undefined) {
+        return document[hiddenProperty];
+      } else {
+        return !document.hasFocus;
+      }
+    };
+  })();
 
   var hasLocalStorage = (function() {
     try {
@@ -98,24 +81,19 @@
     return me.enableChunkedEncoding && hasonprogress;
   };
 
-  shouldLongPoll = function() {
+  var shouldLongPoll = function() {
     return (
       me.alwaysLongPoll ||
       (me.shouldLongPollCallback ? me.shouldLongPollCallback() : !isHidden())
     );
   };
 
-  var totalAjaxFailures = 0;
-  var totalAjaxCalls = 0;
-  var lastAjax;
-
   var processMessages = function(messages) {
     var gotData = false;
-    if (!messages) return false; // server unexpectedly closed connection
+    if ((!messages) || (messages.length === 0)) { return false; }
 
     for (var i = 0; i < messages.length; i++) {
       var message = messages[i];
-      gotData = true;
       for (var j = 0; j < callbacks.length; j++) {
         var callback = callbacks[j];
         if (callback.channel === message.channel) {
@@ -141,7 +119,7 @@
       }
     }
 
-    return gotData;
+    return true;
   };
 
   var reqSuccess = function(messages) {
@@ -158,7 +136,7 @@
     return false;
   };
 
-  longPoller = function(poll, data) {
+  var longPoller = function(poll, data) {
     if (ajaxInProgress) {
       // never allow concurrent ajax reqs
       return;
@@ -180,9 +158,7 @@
       chunked = false;
     }
 
-    var headers = {
-      "X-SILENCE-LOGGER": "true"
-    };
+    var headers = { "X-SILENCE-LOGGER": "true" };
     for (var name in me.headers) {
       headers[name] = me.headers[name];
     }
@@ -384,10 +360,6 @@
     baseUrl: baseUrl,
     headers: {},
     ajax: jQuery && jQuery.ajax,
-    noConflict: function() {
-      global.MessageBus = global.MessageBus.previousMessageBus;
-      return this;
-    },
     diagnostics: function() {
       console.log("Stopped: " + stopped + " Started: " + started);
       console.log("Current callbacks");
@@ -429,15 +401,11 @@
 
     // Start polling
     start: function() {
-      var poll;
-
       if (started) return;
       started = true;
       stopped = false;
 
-      poll = function() {
-        var data;
-
+      var poll = function() {
         if (stopped) {
           return;
         }
@@ -452,7 +420,7 @@
           return;
         }
 
-        data = {};
+        var data = {};
         for (var i = 0; i < callbacks.length; i++) {
           data[callbacks[i].channel] = callbacks[i].last_id;
         }
@@ -466,7 +434,7 @@
 
       // monitor visibility, issue a new long poll when the page shows
       if (document.addEventListener && "hidden" in document) {
-        me.visibilityEvent = global.document.addEventListener(
+        me.visibilityEvent = document.addEventListener(
           "visibilitychange",
           function() {
             if (!document.hidden && !me.longPoll && pollTimeout) {
@@ -532,7 +500,7 @@
       // TODO allow for globbing in the middle of a channel name
       // like /something/*/something
       // at the moment we only support globbing /something/*
-      var glob;
+      var glob = false;
       if (channel.indexOf("*", channel.length - 1) !== -1) {
         channel = channel.substr(0, channel.length - 1);
         glob = true;
@@ -567,5 +535,5 @@
       return removed;
     }
   };
-  global.MessageBus = me;
-})(window, document);
+  exports.MessageBus = me;
+})(window, document, window.jQuery);
diff --git a/spec/assets/message-bus.spec.js b/spec/assets/message-bus.spec.js
index 1ef9882..961f1fe 100644
--- a/spec/assets/message-bus.spec.js
+++ b/spec/assets/message-bus.spec.js
@@ -75,15 +75,6 @@ describe("Messagebus", function() {
     })
   });
 
-  it('removes itself from root namespace when noConflict is called', function(){
-    expect(window.MessageBus).not.toBeUndefined();
-    var mb = window.MessageBus;
-    expect(mb).toEqual(window.MessageBus.noConflict());
-    expect(window.MessageBus).toBeUndefined();
-    // reset it so afterEach has something to work on
-    window.MessageBus = mb;
-  });
-
   it('respects minPollInterval setting with defaults', function(){
     expect(MessageBus.minPollInterval).toEqual(100);
     MessageBus.minPollInterval = 1000;

GitHub sha: 1b1ea170

1 Like

This commit appears in #225 which was merged by eviltrout.