Bug 1521958 - Start listeners/observers at first use of Address Book API and only stop them at shutdown. r=mkmelin a=jorgk
authorGeoff Lankow <geoff@darktrojan.net>
Mon, 11 Feb 2019 14:12:35 +1300
changeset 34344 85ae1ae9eaa022dd07414a4749e7eaa47f3101bd
parent 34343 fbdbfa0e0fe379c3458212a84e8b58b0245d5863
child 34345 f3b778e7c52ffb688f3e3a574fa0ee9347ba2f4a
push id389
push userclokep@gmail.com
push dateMon, 18 Mar 2019 19:01:53 +0000
reviewersmkmelin, jorgk
bugs1521958
Bug 1521958 - Start listeners/observers at first use of Address Book API and only stop them at shutdown. r=mkmelin a=jorgk
mail/components/extensions/parent/ext-addressBook.js
--- a/mail/components/extensions/parent/ext-addressBook.js
+++ b/mail/components/extensions/parent/ext-addressBook.js
@@ -271,22 +271,30 @@ var addressBookCache = new class extends
   decrementListeners() {
     this.listenerCount--;
     if (this.listenerCount == 0) {
       MailServices.ab.removeAddressBookListener(this);
       Services.obs.removeObserver(this, "addrbook-contact-created");
       Services.obs.removeObserver(this, "addrbook-contact-updated");
       Services.obs.removeObserver(this, "addrbook-list-updated");
       Services.obs.removeObserver(this, "addrbook-list-member-added");
+
+      this.flush();
     }
   }
 };
 
 this.addressBook = class extends ExtensionAPI {
+  onShutdown() {
+    addressBookCache.decrementListeners();
+  }
+
   getAPI(context) {
+    addressBookCache.incrementListeners();
+
     return {
       addressBooks: {
         async openUI() {
           let topWindow = Services.wm.getMostRecentWindow(AB_WINDOW_TYPE);
           if (!topWindow) {
             // TODO: wait until window is loaded before resolving
             topWindow = Services.ww.openWindow(null, AB_WINDOW_URI, "_blank", "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar", null);
           }
@@ -322,52 +330,46 @@ this.addressBook = class extends Extensi
           context,
           name: "addressBooks.onCreated",
           register: fire => {
             let listener = (event, node) => {
               fire.sync(addressBookCache.convert(node));
             };
 
             addressBookCache.on("address-book-created", listener);
-            addressBookCache.incrementListeners();
             return () => {
               addressBookCache.off("address-book-created", listener);
-              addressBookCache.decrementListeners();
             };
           },
         }).api(),
         onUpdated: new EventManager({
           context,
           name: "addressBooks.onUpdated",
           register: fire => {
             let listener = (event, node) => {
               fire.sync(addressBookCache.convert(node));
             };
 
             addressBookCache.on("address-book-updated", listener);
-            addressBookCache.incrementListeners();
             return () => {
               addressBookCache.off("address-book-updated", listener);
-              addressBookCache.decrementListeners();
             };
           },
         }).api(),
         onDeleted: new EventManager({
           context,
           name: "addressBooks.onDeleted",
           register: fire => {
             let listener = (event, item) => {
               fire.sync(item.UID);
             };
 
             addressBookCache.on("address-book-deleted", listener);
-            addressBookCache.incrementListeners();
             return () => {
               addressBookCache.off("address-book-deleted", listener);
-              addressBookCache.decrementListeners();
             };
           },
         }).api(),
       },
       contacts: {
         list(parentId) {
           let parentNode = addressBookCache.findAddressBookById(parentId);
           return addressBookCache.convert(parentNode.contacts, false);
@@ -410,52 +412,46 @@ this.addressBook = class extends Extensi
           context,
           name: "contacts.onCreated",
           register: fire => {
             let listener = (event, node) => {
               fire.sync(addressBookCache.convert(node));
             };
 
             addressBookCache.on("contact-created", listener);
-            addressBookCache.incrementListeners();
             return () => {
               addressBookCache.off("contact-created", listener);
-              addressBookCache.decrementListeners();
             };
           },
         }).api(),
         onUpdated: new EventManager({
           context,
           name: "contacts.onUpdated",
           register: fire => {
             let listener = (event, node) => {
               fire.sync(addressBookCache.convert(node));
             };
 
             addressBookCache.on("contact-updated", listener);
-            addressBookCache.incrementListeners();
             return () => {
               addressBookCache.off("contact-updated", listener);
-              addressBookCache.decrementListeners();
             };
           },
         }).api(),
         onDeleted: new EventManager({
           context,
           name: "contacts.onDeleted",
           register: fire => {
             let listener = (event, parent, item) => {
               fire.sync(parent.UID, item.UID);
             };
 
             addressBookCache.on("contact-deleted", listener);
-            addressBookCache.incrementListeners();
             return () => {
               addressBookCache.off("contact-deleted", listener);
-              addressBookCache.decrementListeners();
             };
           },
         }).api(),
       },
       mailingLists: {
         list(parentId) {
           let parentNode = addressBookCache.findAddressBookById(parentId);
           return addressBookCache.convert(parentNode.mailingLists, false);
@@ -509,83 +505,73 @@ this.addressBook = class extends Extensi
           context,
           name: "mailingLists.onCreated",
           register: fire => {
             let listener = (event, node) => {
               fire.sync(addressBookCache.convert(node));
             };
 
             addressBookCache.on("mailing-list-created", listener);
-            addressBookCache.incrementListeners();
             return () => {
               addressBookCache.off("mailing-list-created", listener);
-              addressBookCache.decrementListeners();
             };
           },
         }).api(),
         onUpdated: new EventManager({
           context,
           name: "mailingLists.onUpdated",
           register: fire => {
             let listener = (event, node) => {
               fire.sync(addressBookCache.convert(node));
             };
 
             addressBookCache.on("mailing-list-updated", listener);
-            addressBookCache.incrementListeners();
             return () => {
               addressBookCache.off("mailing-list-updated", listener);
-              addressBookCache.decrementListeners();
             };
           },
         }).api(),
         onDeleted: new EventManager({
           context,
           name: "mailingLists.onDeleted",
           register: fire => {
             let listener = (event, parent, item) => {
               fire.sync(parent.UID, item.UID);
             };
 
             addressBookCache.on("mailing-list-deleted", listener);
-            addressBookCache.incrementListeners();
             return () => {
               addressBookCache.off("mailing-list-deleted", listener);
-              addressBookCache.decrementListeners();
             };
           },
         }).api(),
         onMemberAdded: new EventManager({
           context,
           name: "mailingLists.onMemberAdded",
           register: fire => {
             let listener = (event, node) => {
               fire.sync(addressBookCache.convert(node));
             };
 
             addressBookCache.on("mailing-list-member-added", listener);
-            addressBookCache.incrementListeners();
             return () => {
               addressBookCache.off("mailing-list-member-added", listener);
-              addressBookCache.decrementListeners();
             };
           },
         }).api(),
         onMemberRemoved: new EventManager({
           context,
           name: "mailingLists.onMemberRemoved",
           register: fire => {
             let listener = (event, parent, item) => {
               fire.sync(parent.UID, item.UID);
             };
 
             addressBookCache.on("mailing-list-member-removed", listener);
-            addressBookCache.incrementListeners();
             return () => {
               addressBookCache.off("mailing-list-member-removed", listener);
-              addressBookCache.decrementListeners();
             };
           },
         }).api(),
       },
     };
   }
 };