Bug 1251892: Fix EventManager cleanup code. r?rpl draft
authorKris Maglione <maglione.k@gmail.com>
Sat, 27 Feb 2016 14:53:09 -0800
changeset 335169 452fa901b41d44bf2735b453a3bbb87ff58ff3b2
parent 335162 aba2c94b1bf8d1b0423205a8b45e442c4b35252d
child 515095 129058d801fb2889eb624175e3dff178f2d96d34
push id11745
push usermaglione.k@gmail.com
push dateSat, 27 Feb 2016 23:10:08 +0000
reviewersrpl
bugs1251892
milestone47.0a1
Bug 1251892: Fix EventManager cleanup code. r?rpl MozReview-Commit-ID: FUHnELOAeGw
toolkit/components/extensions/ExtensionUtils.jsm
--- a/toolkit/components/extensions/ExtensionUtils.jsm
+++ b/toolkit/components/extensions/ExtensionUtils.jsm
@@ -474,45 +474,43 @@ LocaleData.prototype = {
 // multiple listeners are registered. |register| should return an
 // unregister function that will unregister the listener.
 function EventManager(context, name, register) {
   this.context = context;
   this.name = name;
   this.register = register;
   this.unregister = null;
   this.callbacks = new Set();
-  this.registered = false;
 }
 
 EventManager.prototype = {
   addListener(callback) {
     if (typeof(callback) != "function") {
       dump(`Expected function\n${Error().stack}`);
       return;
     }
 
-    if (!this.registered) {
+    if (!this.callbacks.size) {
       this.context.callOnClose(this);
 
       let fireFunc = this.fire.bind(this);
       let fireWithoutClone = this.fireWithoutClone.bind(this);
       fireFunc.withoutClone = fireWithoutClone;
       this.unregister = this.register(fireFunc);
-      this.registered = true;
     }
     this.callbacks.add(callback);
   },
 
   removeListener(callback) {
-    if (!this.registered) {
+    if (!this.callbacks.size) {
       return;
     }
 
     this.callbacks.delete(callback);
-    if (this.callbacks.length == 0) {
+    if (this.callbacks.size == 0) {
       this.unregister();
 
       this.context.forgetOnClose(this);
     }
   },
 
   hasListener(callback) {
     return this.callbacks.has(callback);
@@ -526,17 +524,19 @@ EventManager.prototype = {
 
   fireWithoutClone(...args) {
     for (let callback of this.callbacks) {
       runSafeSyncWithoutClone(callback, ...args);
     }
   },
 
   close() {
-    this.unregister();
+    if (this.callbacks.size) {
+      this.unregister();
+    }
   },
 
   api() {
     return {
       addListener: callback => this.addListener(callback),
       removeListener: callback => this.removeListener(callback),
       hasListener: callback => this.hasListener(callback),
     };