diff --git a/devtools/server/actors/webbrowser.js b/devtools/server/actors/webbrowser.js index 933f342f56a0..6d7b7444b5f2 100644 --- a/devtools/server/actors/webbrowser.js +++ b/devtools/server/actors/webbrowser.js @@ -1089,6 +1089,10 @@ TabActor.prototype = { }, onListWorkers: function BTA_onListWorkers(aRequest) { + if (!this.attached) { + return { error: "wrongState" }; + } + if (this._workerActorList === null) { this._workerActorList = new WorkerActorList({ type: Ci.nsIWorkerDebugger.TYPE_DEDICATED, diff --git a/devtools/server/actors/worker.js b/devtools/server/actors/worker.js index 05b339ab292d..0be25e685a10 100644 --- a/devtools/server/actors/worker.js +++ b/devtools/server/actors/worker.js @@ -146,6 +146,7 @@ function WorkerActorList(options) { WorkerActorList.prototype = { getList: function () { + // Create a set of debuggers. let dbgs = new Set(); let e = wdm.getWorkerDebuggerEnumerator(); while (e.hasMoreElements()) { @@ -155,12 +156,14 @@ WorkerActorList.prototype = { } } + // Delete each actor for which we don't have a debugger. for (let [dbg, ] of this._actors) { if (!dbgs.has(dbg)) { this._actors.delete(dbg); } } + // Create an actor for each debugger for which we don't have one. for (let dbg of dbgs) { if (!this._actors.has(dbg)) { this._actors.set(dbg, new WorkerActor(dbg)); @@ -172,8 +175,12 @@ WorkerActorList.prototype = { actors.push(actor); } - this._mustNotify = true; - this._checkListening(); + if (!this._mustNotify) { + if (this._onListChanged !== null) { + wdm.addListener(this); + } + this._mustNotify = true; + } return Promise.resolve(actors); }, @@ -187,23 +194,24 @@ WorkerActorList.prototype = { throw new Error("onListChanged must be either a function or null."); } - this._onListChanged = onListChanged; - this._checkListening(); - }, - - _checkListening: function () { - if (this._onListChanged !== null && this._mustNotify) { - wdm.addListener(this); - } else { - wdm.removeListener(this); + if (this._mustNotify) { + if (this._onListChanged === null && onListChanged !== null) { + wdm.addListener(this); + } + if (this._onListChanged !== null && onListChanged === null) { + wdm.removeListener(this); + } } + this._onListChanged = onListChanged; }, _notifyListChanged: function () { - if (this._mustNotify) { - this._onListChanged(); - this._mustNotify = false; - } + this._onListChanged(); + + if (this._onListChanged !== null) { + wdm.removeListener(this); + } + this._mustNotify = false; }, onRegister: function (dbg) {