Bug 1644026 - Fix incremental address book search. r=rjl
authorGeoff Lankow <geoff@darktrojan.net>
Wed, 01 Jul 2020 14:45:06 +1200
changeset 30027 9092f30233339f894cbb1910a39f0c962e8a98f8
parent 30026 a37d8583cbd00751deff2559113854e37bd7a448
child 30028 926e7403d2a87e3bd461a02a0594aca7dab92980
push id17653
push usergeoff@darktrojan.net
push dateWed, 01 Jul 2020 03:41:23 +0000
treeherdercomm-central@926e7403d2a8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrjl
bugs1644026
Bug 1644026 - Fix incremental address book search. r=rjl
mailnews/addrbook/content/abView.js
--- a/mailnews/addrbook/content/abView.js
+++ b/mailnews/addrbook/content/abView.js
@@ -128,24 +128,20 @@ ABView.prototype = {
         Services.obs.removeObserver(this, topic);
       }
     }
   },
 
   // nsIAbDirSearchListener
 
   onSearchFoundCard(card) {
-    this._rowMap.push(new abViewCard(card));
+    // Instead of duplicating the insertion code below, just call it.
+    this.observe(card, "addrbook-contact-created", this.directory?.UID);
   },
-  onSearchFinished(result, errorMsg) {
-    this.sortBy(this.sortColumn, this.sortDirection, true);
-    if (this.listener) {
-      this.listener.onCountChanged(this.rowCount);
-    }
-  },
+  onSearchFinished(result, errorMsg) {},
 
   // nsIObserver
 
   observe(subject, topic, data) {
     if (this.directory && data && this.directory.UID != data) {
       return;
     }
 
@@ -165,32 +161,35 @@ ABView.prototype = {
       case "addrbook-list-member-added":
         if (!this.directory) {
           break;
         }
       // Falls through.
       case "addrbook-contact-created":
         let viewCard = new abViewCard(subject);
         let sortText = viewCard.getText(this.sortColumn);
-        let added = false;
-        for (let i = 0; !added && i < this._rowMap.length; i++) {
+        let addIndex = null;
+        for (let i = 0; addIndex === null && i < this._rowMap.length; i++) {
           let comparison = this.collator.compare(
             sortText,
             this._rowMap[i].getText(this.sortColumn)
           );
           if (
             (comparison < 0 && this.sortDirection == "ascending") ||
             (comparison >= 0 && this.sortDirection == "descending")
           ) {
-            this._rowMap.splice(i, 0, viewCard);
-            added = true;
+            addIndex = i;
           }
         }
-        if (!added) {
-          this._rowMap.push(viewCard);
+        if (addIndex === null) {
+          addIndex = this._rowMap.length;
+        }
+        this._rowMap.splice(addIndex, 0, viewCard);
+        if (this.tree) {
+          this.tree.rowCountChanged(addIndex, 1);
         }
         if (this.listener) {
           this.listener.onCountChanged(this.rowCount);
         }
         break;
 
       case "addrbook-list-updated":
         if (!this.directory) {
@@ -215,16 +214,19 @@ ABView.prototype = {
         if (!this.directory) {
           break;
         }
       // Falls through.
       case "addrbook-contact-deleted":
         for (let i = this._rowMap.length - 1; i >= 0; i--) {
           if (this._rowMap[i].card.equals(subject)) {
             this._rowMap.splice(i, 1);
+            if (this.tree) {
+              this.tree.rowCountChanged(i, -1);
+            }
           }
         }
         if (this.listener) {
           this.listener.onCountChanged(this.rowCount);
         }
         break;
     }
   },