Bug 1251892: Fix EventManager cleanup code. r?rpl
MozReview-Commit-ID: FUHnELOAeGw
--- 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),
};