make summarize selection defer rapid repeated calls, r=davida, 496318
authorDavid Bienvenu <bienvenu@nventure.com>
Thu, 04 Jun 2009 17:07:58 -0700
changeset 2777 63dc0f6f9564c3af76ed1d507eb8c683b6e83424
parent 2776 7f60d259141f4acad2514fe2349b58bb9139081e
child 2778 063d4c9ed5ea24d94eaece15d4c80ac7dd6bcbd7
push id2250
push userbienvenu@nventure.com
push dateFri, 05 Jun 2009 00:08:01 +0000
treeherdercomm-central@63dc0f6f9564 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdavida, 496318
bugs496318
make summarize selection defer rapid repeated calls, r=davida, 496318
mail/base/content/threadPane.js
--- a/mail/base/content/threadPane.js
+++ b/mail/base/content/threadPane.js
@@ -86,17 +86,20 @@ function ThreadPaneOnClick(event)
           (event.ctrlKey || event.metaKey)) {
         gDBView.ExpandAndSelectThreadByIndex(row.value, true);
         event.stopPropagation();
       }
     }
 }
 
 function nsMsgDBViewCommandUpdater()
-{}
+{
+  _selectionSummarized: false;
+  _selectionTimeout: null;
+}
 
 nsMsgDBViewCommandUpdater.prototype = 
 {
   updateCommandStatus : function()
     {
       // the back end is smart and is only telling us to update command status
       // when the # of items in the selection has actually changed.
       UpdateMailToolbar("dbview driven, thread pane");
@@ -111,38 +114,72 @@ nsMsgDBViewCommandUpdater.prototype =
     goUpdateCommand("button_junk");
   },
 
   updateNextMessageAfterDelete : function()
   {
     SetNextMessageAfterDelete();
   },
 
-  summarizeSelection: function()
+ /**
+  * This method either handles the selection, or sets a timer to handle
+  * it once it stops changing.
+  */
+  showSummary: function(aThis, aSelCount)
   {
+    aThis._selectionSummarized = true;
     let selectedMsgUris = GetSelectedMessages();
-    if (!selectedMsgUris || (selectedMsgUris.length == 1)) {
-      pickMessagePane("singlemessage");
-      return false;
+    let selCount = selectedMsgUris ? selectedMsgUris.length : 0;
+    if (selCount < 2) {
+      aThis.summarizeSelection();
+      return;
     }
-
-    if (! gPrefBranch.getBoolPref("mail.operate_on_msgs_in_collapsed_threads")) {
-      ClearMessagePane();
-      return false;
+    if (selCount != aSelCount) {
+      clearTimeout(aThis._selectionTimeout);
+      aThis._selectionTimeout = setTimeout(aThis.showSummary, 100, aThis, selCount);
+      return;
     }
 
     let firstThreadId = messenger.msgHdrFromURI(selectedMsgUris[0]).threadId;
     for (let i = 1; i < selectedMsgUris.length; ++i)
     {
       let msgHdr = messenger.msgHdrFromURI(selectedMsgUris[i]);
-      if (msgHdr.threadId != firstThreadId) // at least more than one thread
-        return summarizeMultipleSelection(selectedMsgUris);
+      if (msgHdr.threadId != firstThreadId) { // at least more than one thread
+        summarizeMultipleSelection(selectedMsgUris);
+        return;
+      }
     }
     // must be just one thread.
     summarizeThread(selectedMsgUris);
+  },
+
+  summarizeSelection: function()
+  {
+    // First handle immediately the cases where we're not going to summarize.
+    let selectedMsgUris = GetSelectedMessages();
+    if (!selectedMsgUris || (selectedMsgUris.length == 1)) {
+      pickMessagePane("singlemessage");
+      this._selectionSummarized = false;
+      return false;
+    }
+
+    if (! gPrefBranch.getBoolPref("mail.operate_on_msgs_in_collapsed_threads")) {
+      ClearMessagePane();
+      this._selectionSummarized = false;
+      return false;
+    }
+
+    // If we are already summarized, let's make sure the selection count
+    // isn't changing rapidly, by checking again in 100 msec.
+    if (this._selectionSummarized) {
+      clearTimeout(this._selectionTimeout);
+      this._selectionTimeout = setTimeout(this.showSummary, 100, this, selectedMsgUris.length);
+      return true;
+    }
+    this.showSummary(this, selectedMsgUris.length);
     return true;
   },
 
   QueryInterface : function(iid)
    {
      if (iid.equals(Components.interfaces.nsIMsgDBViewCommandUpdater) ||
          iid.equals(Components.interfaces.nsISupports))
        return this;