Bug 1366535 - convert uses of defer to new Promise in client/storage r=nchevobbe
authorMichael Ratcliffe <mratcliffe@mozilla.com>
Mon, 05 Jun 2017 21:58:23 +0100
changeset 410652 4a2407f32c27960136418b03e479c3703fd29e94
parent 410651 7df33990054eb90ee719d62fe125e01bd336bb84
child 410653 c6508a9f0fb5f1648b37f299be7f3e481cb26c31
push id7391
push usermtabara@mozilla.com
push dateMon, 12 Jun 2017 13:08:53 +0000
treeherdermozilla-beta@2191d7f87e2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnchevobbe
bugs1366535
milestone55.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 1366535 - convert uses of defer to new Promise in client/storage r=nchevobbe MozReview-Commit-ID: LXFr5hAlHZe
devtools/client/storage/test/head.js
--- a/devtools/client/storage/test/head.js
+++ b/devtools/client/storage/test/head.js
@@ -196,19 +196,20 @@ var openStoragePanel = Task.async(functi
  * Wait for the toolbox frame to receive focus after it loads
  *
  * @param toolbox {Toolbox}
  *
  * @return a promise that resolves when focus has been received
  */
 function waitForToolboxFrameFocus(toolbox) {
   info("Making sure that the toolbox's frame is focused");
-  let def = promise.defer();
-  waitForFocus(def.resolve, toolbox.win);
-  return def.promise;
+
+  return new Promise(resolve => {
+    waitForFocus(resolve, toolbox.win);
+  });
 }
 
 /**
  * Forces GC, CC and Shrinking GC to get rid of disconnected docshells and
  * windows.
  */
 function forceCollections() {
   Cu.forceGC();
@@ -269,28 +270,26 @@ function* finishTests() {
 
   Services.cookies.removeAll();
   forceCollections();
   finish();
 }
 
 // Sends a click event on the passed DOM node in an async manner
 function* click(node) {
-  let def = promise.defer();
-
   node.scrollIntoView();
 
-  // We need setTimeout here to allow any scrolling to complete before clicking
-  // the node.
-  setTimeout(() => {
-    node.click();
-    def.resolve();
-  }, 200);
-
-  return def;
+  return new Promise(resolve => {
+    // We need setTimeout here to allow any scrolling to complete before clicking
+    // the node.
+    setTimeout(() => {
+      node.click();
+      resolve();
+    }, 200);
+  });
 }
 
 /**
  * Recursively expand the variables view up to a given property.
  *
  * @param options
  *        Options for view expansion:
  *        - rootVariable: start from the given scope/variable/property.
@@ -301,45 +300,44 @@ function* click(node) {
  *         A promise that is resolved only when the last property in |expandTo|
  *         is found, and rejected otherwise. Resolution reason is always the
  *         last property - |nextSibling| in the example above. Rejection is
  *         always the last property that was found.
  */
 function variablesViewExpandTo(options) {
   let root = options.rootVariable;
   let expandTo = options.expandTo.split(".");
-  let lastDeferred = promise.defer();
 
-  function getNext(prop) {
-    let name = expandTo.shift();
-    let newProp = prop.get(name);
+  return new Promise((resolve, reject) => {
+    function getNext(prop) {
+      let name = expandTo.shift();
+      let newProp = prop.get(name);
 
-    if (expandTo.length > 0) {
-      ok(newProp, "found property " + name);
-      if (newProp && newProp.expand) {
-        newProp.expand();
-        getNext(newProp);
+      if (expandTo.length > 0) {
+        ok(newProp, "found property " + name);
+        if (newProp && newProp.expand) {
+          newProp.expand();
+          getNext(newProp);
+        } else {
+          reject(prop);
+        }
+      } else if (newProp) {
+        resolve(newProp);
       } else {
-        lastDeferred.reject(prop);
+        reject(prop);
       }
-    } else if (newProp) {
-      lastDeferred.resolve(newProp);
-    } else {
-      lastDeferred.reject(prop);
     }
-  }
 
-  if (root && root.expand) {
-    root.expand();
-    getNext(root);
-  } else {
-    lastDeferred.resolve(root);
-  }
-
-  return lastDeferred.promise;
+    if (root && root.expand) {
+      root.expand();
+      getNext(root);
+    } else {
+      resolve(root);
+    }
+  });
 }
 
 /**
  * Find variables or properties in a VariablesView instance.
  *
  * @param array ruleArray
  *        The array of rules you want to match. Each rule is an object with:
  *        - name (string|regexp): property name to match.
@@ -407,43 +405,42 @@ function findVariableViewProperties(rule
           let matcher = matchVariablesViewProperty(prop, rule);
           promises.push(matcher.then(onMatch.bind(null, prop, rule)));
         }
       }
     }
   }
 
   function processExpandRules(rules) {
-    let rule = rules.shift();
-    if (!rule) {
-      return promise.resolve(null);
-    }
+    return new Promise(resolve => {
+      let rule = rules.shift();
+      if (!rule) {
+        resolve(null);
+      }
 
-    let deferred = promise.defer();
-    let expandOptions = {
-      rootVariable: gUI.view.getScopeAtIndex(parsed ? 1 : 0),
-      expandTo: rule.name
-    };
+      let expandOptions = {
+        rootVariable: gUI.view.getScopeAtIndex(parsed ? 1 : 0),
+        expandTo: rule.name
+      };
 
-    variablesViewExpandTo(expandOptions).then(function onSuccess(prop) {
-      let name = rule.name;
-      let lastName = name.split(".").pop();
-      rule.name = lastName;
+      variablesViewExpandTo(expandOptions).then(function onSuccess(prop) {
+        let name = rule.name;
+        let lastName = name.split(".").pop();
+        rule.name = lastName;
 
-      let matched = matchVariablesViewProperty(prop, rule);
-      return matched.then(onMatch.bind(null, prop, rule)).then(function () {
-        rule.name = name;
+        let matched = matchVariablesViewProperty(prop, rule);
+        return matched.then(onMatch.bind(null, prop, rule)).then(function () {
+          rule.name = name;
+        });
+      }, function onFailure() {
+        resolve(null);
+      }).then(processExpandRules.bind(null, rules)).then(function () {
+        resolve(null);
       });
-    }, function onFailure() {
-      return promise.resolve(null);
-    }).then(processExpandRules.bind(null, rules)).then(function () {
-      deferred.resolve(null);
     });
-
-    return deferred.promise;
   }
 
   function onAllRulesMatched(rules) {
     for (let rule of rules) {
       let matched = rule.matchedProp;
       if (matched && !rule.dontMatch) {
         ok(true, "rule " + rule.name + " matched for property " + matched.name);
       } else if (matched && rule.dontMatch) {
@@ -540,48 +537,49 @@ function* selectTableItem(id) {
   let target = gPanelWindow.document.querySelector(selector);
 
   ok(target, "table item found with ids " + id);
 
   if (!target) {
     showAvailableIds();
   }
 
+  let updated = gUI.once("sidebar-updated");
+
   yield click(target);
-  yield gUI.once("sidebar-updated");
+  yield updated;
 }
 
 /**
  * Wait for eventName on target.
  * @param {Object} target An observable object that either supports on/off or
  * addEventListener/removeEventListener
  * @param {String} eventName
- * @param {Boolean} [useCapture] for addEventListener/removeEventListener
+ * @param {Boolean} useCapture Optional, for addEventListener/removeEventListener
  * @return A promise that resolves when the event has been handled
  */
 function once(target, eventName, useCapture = false) {
   info("Waiting for event: '" + eventName + "' on " + target + ".");
 
-  let deferred = promise.defer();
-
-  for (let [add, remove] of [
-    ["addEventListener", "removeEventListener"],
-    ["addListener", "removeListener"],
-    ["on", "off"]
-  ]) {
-    if ((add in target) && (remove in target)) {
-      target[add](eventName, function onEvent(...aArgs) {
-        target[remove](eventName, onEvent, useCapture);
-        deferred.resolve.apply(deferred, aArgs);
-      }, useCapture);
-      break;
+  return new Promise(resolve => {
+    for (let [add, remove] of [
+      ["addEventListener", "removeEventListener"],
+      ["addListener", "removeListener"],
+      ["on", "off"]
+    ]) {
+      if ((add in target) && (remove in target)) {
+        target[add](eventName, function onEvent(...aArgs) {
+          info("Got event: '" + eventName + "' on " + target + ".");
+          target[remove](eventName, onEvent, useCapture);
+          resolve(...aArgs);
+        }, useCapture);
+        break;
+      }
     }
-  }
-
-  return deferred.promise;
+  });
 }
 
 /**
  * Get values for a row.
  *
  * @param  {String}  id
  *         The uniqueId of the given row.
  * @param  {Boolean} includeHidden