bug 1523104: remote: introduce MessagePromise sync primitive; r=ochameau
authorAndreas Tolfsen <ato@sny.no>
Thu, 21 Feb 2019 13:54:57 +0000
changeset 521106 0d1d92205068
parent 521105 c26cc4c7f021
child 521107 b075e49fca67
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: introduce MessagePromise sync primitive; r=ochameau Makes the code slightly more pleasant to read.
remote/Sync.jsm
remote/Targets.jsm
--- a/remote/Sync.jsm
+++ b/remote/Sync.jsm
@@ -2,16 +2,17 @@
  * 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 = [
   "DOMContentLoadedPromise",
   "EventPromise",
+  "MessagePromise",
 ];
 
 const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
 
 /**
  * Wait for a single event to be fired on a specific EventListener.
  *
  * The returned promise is guaranteed to not be called before the
@@ -69,8 +70,33 @@ function EventPromise(listener, type, op
 }
 
 function DOMContentLoadedPromise(window, options = {mozSystemGroup: true}) {
   if (window.document.readyState == "complete") {
     return Promise.resolve();
   }
   return new EventPromise(window, "DOMContentLoaded", options);
 }
+
+/**
+ * Awaits a single IPC message.
+ *
+ * @param {nsIMessageSender} target
+ * @param {string} name
+ *
+ * @return {Promise}
+ *
+ * @throws {TypeError}
+ *     If target is not an nsIMessageSender.
+ */
+function MessagePromise(target, name) {
+  if (!(target instanceof Ci.nsIMessageSender)) {
+    throw new TypeError();
+  }
+
+  return new Promise(resolve => {
+    const onMessage = (...args) => {
+      target.removeMessageListener(name, onMessage);
+      resolve(...args);
+    };
+    target.addMessageListener(name, onMessage);
+  });
+}
--- a/remote/Targets.jsm
+++ b/remote/Targets.jsm
@@ -1,53 +1,48 @@
 /* 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 {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();
   }
 
   /** @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 Promise(resolve => {
-        const onInit = () => {
-          browser.messageManager.removeMessageListener("Browser:Init", onInit);
-          resolve();
-        };
-        browser.messageManager.addMessageListener("Browser:Init", onInit);
-      });
+      await new MessagePromise(browser.messageManager, "Browser:Init");
     }
+
     const target = new Target(browser);
-
     target.connect();
     this._targets.set(target.id, target);
   }
 
   /** @param BrowserElement browser */
   disconnect(browser) {
     // Ignore the browsers that haven't had time to initialize.
     if (!browser.browsingContext) {
       return;
     }
-    let target = this._targets.get(browser.browsingContext.id);
 
+    const target = this._targets.get(browser.browsingContext.id);
     if (target) {
       target.disconnect();
       this._targets.delete(target.id);
     }
   }
 
   clear() {
     for (const target of this) {