Bug 1471812 - Remove defer usage in webconsole panel initialization; r=ochameau. draft
authorNicolas Chevobbe <nchevobbe@mozilla.com>
Wed, 27 Jun 2018 17:18:52 +0200
changeset 811767 84e01ed8756013f68b440cb890be00a32fc19eab
parent 811103 1c235a552c32ba6c97e6030c497c49f72c7d48a8
push id114413
push userbmo:nchevobbe@mozilla.com
push dateThu, 28 Jun 2018 08:01:19 +0000
reviewersochameau
bugs1471812
milestone63.0a1
Bug 1471812 - Remove defer usage in webconsole panel initialization; r=ochameau. This patch removes the use of defer and switches the open function to an async one, which makes things easier to follow. MozReview-Commit-ID: Iezrfs3NNBz
devtools/client/webconsole/panel.js
--- a/devtools/client/webconsole/panel.js
+++ b/devtools/client/webconsole/panel.js
@@ -1,19 +1,16 @@
 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set ft= javascript ts=2 et sw=2 tw=80: */
 /* 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";
 
-const promise = require("promise");
-const defer = require("devtools/shared/defer");
-
 loader.lazyRequireGetter(this, "HUDService", "devtools/client/webconsole/hudservice", true);
 loader.lazyGetter(this, "EventEmitter", () => require("devtools/shared/event-emitter"));
 
 /**
  * A DevToolPanel that controls the Web Console.
  */
 function WebConsolePanel(iframeWindow, toolbox) {
   this._frameWindow = iframeWindow;
@@ -36,70 +33,57 @@ WebConsolePanel.prototype = {
   },
 
   /**
    * Open is effectively an asynchronous constructor.
    *
    * @return object
    *         A promise that is resolved when the Web Console completes opening.
    */
-  open: function() {
-    const parentDoc = this._toolbox.doc;
-    const iframe = parentDoc.getElementById("toolbox-panel-iframe-webconsole");
+  open: async function() {
+    try {
+      const parentDoc = this._toolbox.doc;
+      const iframe = parentDoc.getElementById("toolbox-panel-iframe-webconsole");
+
+      // Make sure the iframe content window is ready.
+      const win = iframe.contentWindow;
+      const doc = win && win.document;
+      if (!doc || doc.readyState !== "complete") {
+        await new Promise(resolve => {
+          iframe.addEventListener("load", resolve, {capture: true, once: true});
+        });
+      }
+
+      // Local debugging needs to make the target remote.
+      if (!this.target.isRemote) {
+        await this.target.makeRemote();
+      }
 
-    // Make sure the iframe content window is ready.
-    const deferredIframe = defer();
-    let win, doc;
-    if ((win = iframe.contentWindow) &&
-        (doc = win.document) &&
-        doc.readyState == "complete") {
-      deferredIframe.resolve(null);
-    } else {
-      iframe.addEventListener("load", function() {
-        deferredIframe.resolve(null);
-      }, {capture: true, once: true});
-    }
+      const webConsoleUIWindow = iframe.contentWindow.wrappedJSObject;
+      const chromeWindow = iframe.ownerDocument.defaultView;
+
+      // Open the Web Console.
+      this.hud = await HUDService.openWebConsole(
+        this.target, webConsoleUIWindow, chromeWindow);
 
-    // Local debugging needs to make the target remote.
-    let promiseTarget;
-    if (!this.target.isRemote) {
-      promiseTarget = this.target.makeRemote();
-    } else {
-      promiseTarget = promise.resolve(this.target);
+      // Pipe 'reloaded' event from WebConsoleFrame to WebConsolePanel.
+      // These events are listened by the Toolbox.
+      this.hud.ui.on("reloaded", () => {
+        this.emit("reloaded");
+      });
+
+      this._isReady = true;
+      this.emit("ready");
+    } catch (e) {
+      const msg = "WebConsolePanel open failed. " + e.error + ": " + e.message;
+      dump(msg + "\n");
+      console.error(msg, e);
     }
 
-    // 1. Wait for the iframe to load.
-    // 2. Wait for the remote target.
-    // 3. Open the Web Console.
-    return deferredIframe.promise
-      .then(() => promiseTarget)
-      .then((target) => {
-        this._frameWindow._remoteTarget = target;
-
-        const webConsoleUIWindow = iframe.contentWindow.wrappedJSObject;
-        const chromeWindow = iframe.ownerDocument.defaultView;
-        return HUDService.openWebConsole(this.target, webConsoleUIWindow,
-                                         chromeWindow);
-      })
-      .then((webConsole) => {
-        this.hud = webConsole;
-        // Pipe 'reloaded' event from WebConsoleFrame to WebConsolePanel.
-        // These events are listened by the Toolbox.
-        this.hud.ui.on("reloaded", () => {
-          this.emit("reloaded");
-        });
-        this._isReady = true;
-        this.emit("ready");
-        return this;
-      }, (reason) => {
-        const msg = "WebConsolePanel open failed. " +
-                  reason.error + ": " + reason.message;
-        dump(msg + "\n");
-        console.error(msg, reason);
-      });
+    return this;
   },
 
   get target() {
     return this._toolbox.target;
   },
 
   _isReady: false,
   get isReady() {