changeset 72:0eab9a3ff12f

replaced my last commit w/ a serial xhr queue, so that the dashboard requests are serialized but other ones aren't.
author Atul Varma <avarma@mozilla.com>
date Sun, 25 Apr 2010 18:21:34 -0700
parents 4ec651cc606e
children 1cd66cabe153
files js/bugzilla.js js/modules/app.js js/modules/xhr.js main.html
diffstat 4 files changed, 71 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/js/bugzilla.js	Sun Apr 25 17:55:54 2010 -0700
+++ b/js/bugzilla.js	Sun Apr 25 18:21:34 2010 -0700
@@ -43,12 +43,12 @@
     return xhr;
   },
   getBug: function Bugzilla_getBug(id, cb) {
-    this.ajax({url: "/bug/" + id,
-               success: cb});
+    return this.ajax({url: "/bug/" + id,
+                      success: cb});
   },
   search: function Bugzilla_search(query, cb) {
-    this.ajax({url: "/bug",
-               data: query,
-               success: cb});
+    return this.ajax({url: "/bug",
+                      data: query,
+                      success: cb});
   }
 };
--- a/js/modules/app.js	Sun Apr 25 17:55:54 2010 -0700
+++ b/js/modules/app.js	Sun Apr 25 18:21:34 2010 -0700
@@ -42,49 +42,8 @@
   };
 };
 
-Require.modules["app/bugzilla-serial"] = function(exports, require) {
-  const EVENTS = ["abort", "error", "load"];
-
+Require.modules["app/bugzilla-auth"] = function(exports, require) {
   var myproto = require("bugzilla");
-  var active = null;
-  var queue = [];
-
-  function enqueue(bugzilla, options) {
-    queue.push({bugzilla: bugzilla, options: options});
-    if (!active)
-      activateNextInQueue();
-  }
-
-  function activateNextInQueue() {
-    if (queue.length) {
-      var entry = queue.splice(0, 1)[0];
-      var xhr = myproto.ajax.call(entry.bugzilla, entry.options);
-      EVENTS.forEach(function(name) {
-        xhr.addEventListener(name, onDone, false);
-      });
-      active = xhr;
-    } else
-      active = null;
-  }
-
-  function onDone(event) {
-    var xhr = event.target;
-    EVENTS.forEach(function(name) {
-      xhr.removeEventListener(name, onDone, false);
-    });
-    activateNextInQueue();
-  };
-
-  exports.Bugzilla = {
-    ajax: function ajax(options) {
-      enqueue(this, options);
-    },
-    __proto__: myproto
-  };
-};
-
-Require.modules["app/bugzilla-auth"] = function(exports, require) {
-  var myproto = require("app/bugzilla-serial").Bugzilla;
 
   exports.Bugzilla = {
     ajax: function ajax(options) {
@@ -424,6 +383,7 @@
   var dateUtils = require("date-utils");
   var bugzilla = require("app/bugzilla-auth").Bugzilla;
   var window = require("window");
+  var xhrQueue = require("xhr/queue").create();
 
   function sortByLastChanged(bugs) {
     var lctimes = {};
@@ -532,12 +492,16 @@
     
     $(selector).find("h2").addClass("loading");
     
-    bugzilla.search(newTerms,
-                    function(response) {
-                      cache.set(cacheKey, response.bugs);
-                      showBugs($(selector), response.bugs);
-                      $(selector).find("h2").removeClass("loading");
-                    });
+    xhrQueue.enqueue(
+      function() {
+        return bugzilla.search(
+          newTerms,
+          function(response) {
+            cache.set(cacheKey, response.bugs);
+            showBugs($(selector), response.bugs);
+            $(selector).find("h2").removeClass("loading");
+          });
+      });
   }
 
   function timeAgo(ms) {
@@ -555,6 +519,8 @@
   };
 
   function update(myUsername) {
+    xhrQueue.clear();
+
     report("#code-reviews", myUsername,
            {status: ["NEW", "UNCONFIRMED", "ASSIGNED", "REOPENED"],
             flag_DOT_requestee: myUsername});
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/js/modules/xhr.js	Sun Apr 25 18:21:34 2010 -0700
@@ -0,0 +1,50 @@
+Require.modules["xhr/queue"] = function(exports, require) {
+  function XMLHttpRequestQueue() {
+    const EVENTS = ["abort", "error", "load"];
+
+    var active = null;
+    var queue = [];
+
+    function activateNextInQueue() {
+      if (queue.length) {
+        var cb = queue.splice(0, 1)[0];
+        var xhr = cb();
+        if (!xhr)
+          throw new Error("enqueued callback did not return xhr");
+        EVENTS.forEach(function(name) {
+          xhr.addEventListener(name, onDone, false);
+        });
+        active = xhr;
+      }
+    }
+
+    function onDone(event) {
+      var xhr = event.target;
+      EVENTS.forEach(function(name) {
+        xhr.removeEventListener(name, onDone, false);
+      });
+      if (xhr == active) {
+        active = null;
+        activateNextInQueue();
+      }
+    };
+
+    this.enqueue = function enqueue(cb) {
+      queue.push(cb);
+      if (!active)
+        activateNextInQueue();
+    };
+
+    this.clear = function clear() {
+      queue.splice(0);
+      if (active) {
+        active.abort();
+        active = null;
+      }
+    };
+  }
+
+  exports.create = function create() {
+    return new XMLHttpRequestQueue();
+  };
+};
--- a/main.html	Sun Apr 25 17:55:54 2010 -0700
+++ b/main.html	Sun Apr 25 18:21:34 2010 -0700
@@ -123,7 +123,8 @@
   var moduleScriptNames = [
     "app",
     "date-utils",
-    "cache"
+    "cache",
+    "xhr"
   ];
 
   var moduleScripts = [];