Bug 1366531 - convert uses of defer to 'new Promise' in client/jsonview r=nchevobbe
authorOriol Brufau <oriol-bugzilla@hotmail.com>
Thu, 09 Nov 2017 00:07:44 +0100
changeset 444327 4c25a8edd5db8f8f2bfbbe51f1050ef48d495659
parent 444326 c58e3a20c6f151a1f45c593bf0b342cd9262d98c
child 444328 07b7f8df735ef181ac18371da35df93393e2aefe
push id1618
push userCallek@gmail.com
push dateThu, 11 Jan 2018 17:45:48 +0000
treeherdermozilla-release@882ca853e05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnchevobbe
bugs1366531
milestone58.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 1366531 - convert uses of defer to 'new Promise' in client/jsonview r=nchevobbe MozReview-Commit-ID: InGMTAQ0X2R
devtools/client/jsonview/test/head.js
--- a/devtools/client/jsonview/test/head.js
+++ b/devtools/client/jsonview/test/head.js
@@ -27,64 +27,57 @@ registerCleanupFunction(() => {
  * @param {String} url
  *   The url to be loaded in the new tab.
  * @param {Number} timeout [optional]
  *   The maximum number of milliseconds allowed before the initialization of the
  *   JSON Viewer once the tab has been loaded. If exceeded, the initialization
  *   will be considered to have failed, and the returned promise will be rejected.
  *   If this parameter is not passed or is negative, it will be ignored.
  */
-function addJsonViewTab(url, timeout = -1) {
+async function addJsonViewTab(url, timeout = -1) {
   info("Adding a new JSON tab with URL: '" + url + "'");
 
-  let deferred = defer();
-  addTab(url).then(tab => {
-    let browser = tab.linkedBrowser;
+  let tab = await addTab(url);
+  let browser = tab.linkedBrowser;
 
-    // Load devtools/shared/frame-script-utils.js
-    getFrameScript();
+  // Load devtools/shared/frame-script-utils.js
+  getFrameScript();
 
-    // Load frame script with helpers for JSON View tests.
-    let rootDir = getRootDirectory(gTestPath);
-    let frameScriptUrl = rootDir + "doc_frame_script.js";
-    browser.messageManager.loadFrameScript(frameScriptUrl, false);
+  // Load frame script with helpers for JSON View tests.
+  let rootDir = getRootDirectory(gTestPath);
+  let frameScriptUrl = rootDir + "doc_frame_script.js";
+  browser.messageManager.loadFrameScript(frameScriptUrl, false);
 
-    // Check if there is a JSONView object.
-    if (!content.window.wrappedJSObject.JSONView) {
-      deferred.reject("JSON Viewer did not load.");
-      return;
-    }
+  // Check if there is a JSONView object.
+  if (!content.window.wrappedJSObject.JSONView) {
+    throw new Error("JSON Viewer did not load.");
+  }
 
-    // Resolve if the JSONView is fully loaded or wait
-    // for an initialization event.
-    if (content.window.wrappedJSObject.JSONView.initialized) {
-      deferred.resolve(tab);
-    } else {
-      waitForContentMessage("Test:JsonView:JSONViewInitialized").then(() => {
-        deferred.resolve(tab);
-      });
-    }
+  // Resolve if the JSONView is fully loaded.
+  if (content.window.wrappedJSObject.JSONView.initialized) {
+    return tab;
+  }
+
+  // Otherwise wait for an initialization event, possibly with a time limit.
+  const onJSONViewInitialized =
+    waitForContentMessage("Test:JsonView:JSONViewInitialized")
+    .then(() => tab);
 
-    // Add a timeout.
-    if (timeout >= 0) {
-      new Promise(resolve => {
-        if (content.window.document.readyState === "complete") {
-          resolve();
-        } else {
-          waitForContentMessage("Test:JsonView:load").then(resolve);
-        }
-      }).then(() => {
-        setTimeout(() => {
-          deferred.reject("JSON Viewer did not load.");
-        }, timeout);
-      });
-    }
-  });
+  if (!(timeout >= 0)) {
+    return onJSONViewInitialized;
+  }
 
-  return deferred.promise;
+  if (content.window.document.readyState !== "complete") {
+    await waitForContentMessage("Test:JsonView:load");
+  }
+
+  let onTimeout = new Promise((_, reject) =>
+    setTimeout(() => reject(new Error("JSON Viewer did not load.")), timeout));
+
+  return Promise.race([onJSONViewInitialized, onTimeout]);
 }
 
 /**
  * Expanding a node in the JSON tree
  */
 function clickJsonNode(selector) {
   info("Expanding node: '" + selector + "'");
 
@@ -160,19 +153,17 @@ function sendString(str, selector) {
     selector: selector,
     str: str
   };
 
   return executeInContent("Test:JsonView:SendString", data);
 }
 
 function waitForTime(delay) {
-  let deferred = defer();
-  setTimeout(deferred.resolve, delay);
-  return deferred.promise;
+  return new Promise(resolve => setTimeout(resolve, delay));
 }
 
 function waitForFilter() {
   return executeInContent("Test:JsonView:WaitForFilter");
 }
 
 function normalizeNewLines(value) {
   return value.replace("(\r\n|\n)", "\n");