Switch site getter to async queries
authorEdward Lee <edilee@mozilla.com>
Sat, 14 Mar 2009 17:04:25 -0500
changeset 146 70b107b1c32d
parent 145 0034a8f9b9e5
child 147 f08b980a4c66
push id104
push useredward.lee@engineering.uiuc.edu
push dateSat, 14 Mar 2009 22:04:42 +0000
Switch site getter to async queries
chrome/content/tab.html
chrome/content/text.html
modules/sites.js
--- a/chrome/content/tab.html
+++ b/chrome/content/tab.html
@@ -261,17 +261,17 @@ function setupThumbs() {
           fadeIn("slow").
           appendTo(text);
       }
     };
     Feeds.get(row.id, onFeed);
   }
 
   // Create a thumbnail for the first several sites
-  Sites.get(maxThumbs).forEach(addThumb);
+  Sites.get(maxThumbs, function(sites) sites.forEach(addThumb));
 }
 
 let prefClosedTab = "about-tab-closed-tab";
 function _pushTab( tabName ){ Application.prefs.setValue( prefClosedTab, tabName ); }
 function _popTab() { Application.prefs.setValue( prefClosedTab, "" ); }
 function _peekTab(){ return Application.prefs.getValue( prefClosedTab, null ); }
 
 // This function setups the recently closed tabs list.
--- a/chrome/content/text.html
+++ b/chrome/content/text.html
@@ -232,17 +232,17 @@
     <input type="radio" class="feedMinus" checked="checked"/>
     <input type="radio" class="feedPlus" checked="checked"/>
   </li>
 </div>
 
 <script type="text/javascript;version=1.8">
 
 let maxSites = 10;
-Sites.get(maxSites).forEach( addSite );
+Sites.get(maxSites, function(sites) sites.forEach(addSite));
 
 function addSite( item ) {
 
   // Clone the template
   let el = $("#templates>.site").clone();
 
   // Store the item as part of the element
   el.data("site", item);
--- a/modules/sites.js
+++ b/modules/sites.js
@@ -40,19 +40,22 @@ const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cr = Components.results;
 const Cu = Components.utils;
 
 let cache = [];
 let removed = [];
 
 let Sites = {
-  get: function Sites_get(maxSites) {
+  get: function Sites_get(maxSites, onComplete) {
     // Helper to consistently return the right size of results
-    let ret = function Sites_getRet() cache.slice(0, maxSites);
+    let ret = function Sites_getRet() {
+      if (onComplete)
+        onComplete(cache.slice(0, maxSites));
+    };
 
     // Sanity check for injecting into SQL
     maxSites = parseInt(maxSites);
 
     // Use the cached results if we have enough
     if (maxSites <= cache.length)
       return ret();
 
@@ -73,25 +76,24 @@ let Sites = {
         "JOIN moz_favicons f ON f.id = s.favicon_id " +
         "WHERE favicon_id NOT NULL AND " + baseWhere + "AND " +
           "_id NOT IN (" + removed + ") " +
         "GROUP BY favicon_id " +
         "ORDER BY MAX(frecency) DESC " +
         "LIMIT " + numSites);
     let attrs = ["favicon", "fullTitle", "uri", "id"];
 
-    try {
-      // Reset the cache
-      cache = [];
-
+    let siteHandler = {
+    handleResult: function(results) {
+      let sql;
       // Fetch each result and store them in the cache
-      while (sql.executeStep()) {
+      while (sql = results.getNextRow()) {
         let row = {};
         for (let [index, attr] in Iterator(attrs))
-          row[attr] = sql.getString(index);
+          row[attr] = sql.getResultByIndex(index);
   
         row.iconSrc = Cc["@mozilla.org/browser/favicon-service;1"].
           getService(Ci.nsIFaviconService).
           getFaviconLinkForIcon(toURI(row.favicon)).spec;
 
         // Fix up the title by trimming subtitles, trailing splitters («·»–—•)
         let title = row.fullTitle.split(/[-.:<!>;,\|\/\u00ab\u00b7\u00bb\u2013\u2014\u2022]+( |$)/)[0];
         // Show the domain incase we don't have a title
@@ -99,28 +101,25 @@ let Sites = {
           title = row.uri.match(/:\/*(?:www.)?([^\/]+)/)[1];
         row.title = title;
 
         // Default show only 3 feeds
         row.feeds = 3;
   
         cache.push(row);
       }
-    }
-    catch(ex) {
-      Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).
-        logStringMessage(ex);
-    }
-    finally {
-      sql.reset();
-      sql.finalize();
-    }
+    },
 
-    // Just give the number of sites requested
-    return ret();
+    handleCompletion: ret,
+    handleError: function(error) Cu.reportError(error),
+    };
+
+    // Reset the cache then run the query
+    cache = [];
+    sql.executeAsync(siteHandler);
   },
 
   /**
    * Move an item at the original position to the new position causing other
    * items to get shifted if needed
    *
    * @param origPos
    *        Item's original position