Bug 1251892: Fix EventManager cleanup code. r=rpl
authorKris Maglione <maglione.k@gmail.com>
Sat, 27 Feb 2016 14:53:09 -0800
changeset 322361 2dc35ef10107dba0e719c458cbc6bf4a6de70835
parent 322360 04634ec900b2fb94962733148ecdeac7ae98e0e2
child 322362 317bf83620561efe9ef92b321ac3fc2498321f3b
push id5913
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 16:57:49 +0000
treeherdermozilla-beta@dcaf0a6fa115 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrpl
bugs1251892
milestone47.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 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
@@ -470,45 +470,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);
@@ -522,17 +520,20 @@ EventManager.prototype = {
 
   fireWithoutClone(...args) {
     for (let callback of this.callbacks) {
       runSafeSyncWithoutClone(callback, ...args);
     }
   },
 
   close() {
-    this.unregister();
+    if (this.callbacks.size) {
+      this.unregister();
+    }
+    this.callbacks = null;
   },
 
   api() {
     return {
       addListener: callback => this.addListener(callback),
       removeListener: callback => this.removeListener(callback),
       hasListener: callback => this.hasListener(callback),
     };