Bug 867043 - Only send more contacts when the child needs them. r=reuben
authorGregor Wagner <anygregor@gmail.com>
Fri, 03 May 2013 09:17:34 -0700
changeset 141726 5140723575f55f434cf237aa1161159317a246da
parent 141725 08c582b1c486e9982fb3fc3023e77658da36a6cf
child 141727 69008b1fd6eba1ed5041c8d8796800ced916d1a3
push id2579
push userakeybl@mozilla.com
push dateMon, 24 Jun 2013 18:52:47 +0000
treeherdermozilla-beta@b69b7de8a05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersreuben
bugs867043
milestone23.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 867043 - Only send more contacts when the child needs them. r=reuben
dom/contacts/fallback/ContactDB.jsm
--- a/dom/contacts/fallback/ContactDB.jsm
+++ b/dom/contacts/fallback/ContactDB.jsm
@@ -11,91 +11,76 @@ function debug(s) { dump("-*- ContactDB 
 
 const Cu = Components.utils;
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/IndexedDBHelper.jsm");
 Cu.import("resource://gre/modules/PhoneNumberUtils.jsm");
-Cu.import("resource://gre/modules/Timer.jsm");
 
 const DB_NAME = "contacts";
 const DB_VERSION = 11;
 const STORE_NAME = "contacts";
 const SAVED_GETALL_STORE_NAME = "getallcache";
 const CHUNK_SIZE = 20;
-const CHUNK_INTERVAL = 500;
 const REVISION_STORE = "revision";
 const REVISION_KEY = "revision";
 
 function ContactDispatcher(aContacts, aFullContacts, aCallback, aNewTxn, aClearDispatcher) {
   let nextIndex = 0;
-  let interval;
-
-  function cancelTimeout() {
-    if (interval) {
-      clearTimeout(interval);
-      interval = null;
-    }
-  }
 
   let sendChunk;
   let count = 0;
   if (aFullContacts) {
     sendChunk = function() {
       try {
-        if (aContacts.length > 0) {
-          aCallback(aContacts.splice(0, CHUNK_SIZE));
-          interval = setTimeout(sendChunk, CHUNK_INTERVAL);
-        } else {
+        let chunk = aContacts.splice(0, CHUNK_SIZE);
+        if (chunk.length > 0) {
+          aCallback(chunk);
+        }
+        if (aContacts.length === 0) {
           aCallback(null);
-          cancelTimeout();
           aClearDispatcher();
         }
       } catch (e) {
         aClearDispatcher();
       }
     }
   } else {
-    this.count = 0;
     sendChunk = function() {
       try {
+        let start = nextIndex;
+        nextIndex += CHUNK_SIZE;
         let chunk = [];
         aNewTxn("readonly", STORE_NAME, function(txn, store) {
-          for (let i = nextIndex; i < Math.min(nextIndex+CHUNK_SIZE, aContacts.length); ++i) {
+          for (let i = start; i < Math.min(start+CHUNK_SIZE, aContacts.length); ++i) {
             store.get(aContacts[i]).onsuccess = function(e) {
               chunk.push(e.target.result);
               count++;
               if (count === aContacts.length) {
-                aCallback(chunk)
+                aCallback(chunk);
                 aCallback(null);
-                cancelTimeout();
                 aClearDispatcher();
               } else if (chunk.length === CHUNK_SIZE) {
                 aCallback(chunk);
                 chunk.length = 0;
-                nextIndex += CHUNK_SIZE;
-                interval = setTimeout(sendChunk, CHUNK_INTERVAL);
               }
             }
           }
         });
       } catch (e) {
         aClearDispatcher();
       }
     }
   }
 
-  sendChunk(0);
-
   return {
     sendNow: function() {
-      cancelTimeout();
-      interval = setTimeout(sendChunk, 0);
+      sendChunk();
     }
   };
 }
 
 this.ContactDB = function ContactDB(aGlobal) {
   if (DEBUG) debug("Constructor");
   this._global = aGlobal;
 }
@@ -722,16 +707,17 @@ ContactDB.prototype = {
       // In that case, we receive the full contacts since we already have them
       // in memory to create the cache. This allows us to avoid accessing the
       // object store again.
       if (aCachedResults && aCachedResults.length > 0) {
         let newTxnFn = this.newTxn.bind(this);
         let clearDispatcherFn = this._clearDispatcher.bind(this, aCursorId);
         this._dispatcher[aCursorId] = new ContactDispatcher(aCachedResults, aFullContacts,
                                                             aSuccessCb, newTxnFn, clearDispatcherFn);
+        this._dispatcher[aCursorId].sendNow();
       } else { // no contacts
         if (DEBUG) debug("query returned no contacts");
         aSuccessCb(null);
       }
     }.bind(this));
   },
 
   getRevision: function CDB_getRevision(aSuccessCb) {