bug 1523104: remote: emit events when targets connect and disconnect; r=ochameau
authorAndreas Tolfsen <ato@sny.no>
Thu, 07 Mar 2019 22:10:02 +0000
changeset 521129 0fa4b12c2f35
parent 521128 d9cce6c1e153
child 521130 932d90008fe2
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersochameau
bugs1523104
milestone67.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 1523104: remote: emit events when targets connect and disconnect; r=ochameau
remote/Targets.jsm
--- a/remote/Targets.jsm
+++ b/remote/Targets.jsm
@@ -1,49 +1,54 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
 var EXPORTED_SYMBOLS = ["Targets"];
 
+const {EventEmitter} = ChromeUtils.import("resource://gre/modules/EventEmitter.jsm");
 const {MessagePromise} = ChromeUtils.import("chrome://remote/content/Sync.jsm");
 const {Target} = ChromeUtils.import("chrome://remote/content/Target.jsm");
 
 class Targets {
   constructor() {
     // browser context ID -> Target<XULElement>
     this._targets = new Map();
+
+    EventEmitter.decorate(this);
   }
 
-  /** @param BrowserElement browser */
+  /** @param {BrowserElement} browser */
   async connect(browser) {
     // The tab may just have been created and not fully initialized yet.
     // Target class expects BrowserElement.browsingContext to be defined
     // whereas it is asynchronously set by the custom element class.
     // At least ensure that this property is set before instantiating the target.
     if (!browser.browsingContext) {
       await new MessagePromise(browser.messageManager, "Browser:Init");
     }
 
     const target = new Target(browser);
     target.connect();
     this._targets.set(target.id, target);
+    this.emit("connect", target);
   }
 
-  /** @param BrowserElement browser */
+  /** @param {BrowserElement} browser */
   disconnect(browser) {
     // Ignore the browsers that haven't had time to initialize.
     if (!browser.browsingContext) {
       return;
     }
 
     const target = this._targets.get(browser.browsingContext.id);
     if (target) {
+      this.emit("disconnect", target);
       target.disconnect();
       this._targets.delete(target.id);
     }
   }
 
   clear() {
     for (const target of this) {
       this.disconnect(target.browser);