WIP: Bug 1366532 - convert uses of "defer" to "new Promise" in client/performance draft
authorMichael Kohler <mkohler@picobudget.com>
Sun, 21 May 2017 18:59:05 +0200
changeset 582139 47a2022c1306784be02ae1241200ac01cea6fe61
parent 581473 8d60d0f825110cfb646ac31dc16dc011708bcf34
child 629682 d9fd822d4ccaf702b4bce98b4a96c14ad6779ab1
push id59985
push userbmo:me@michaelkohler.info
push dateSun, 21 May 2017 16:59:24 +0000
bugs1366532
milestone55.0a1
WIP: Bug 1366532 - convert uses of "defer" to "new Promise" in client/performance MozReview-Commit-ID: 48SKKPcuHGp
devtools/client/performance/components/test/head.js
devtools/client/performance/panel.js
devtools/client/performance/performance-controller.js
devtools/client/performance/test/browser_perf-recordings-io-03.js
devtools/client/performance/test/browser_perf-recordings-io-04.js
devtools/client/performance/test/browser_perf-recordings-io-06.js
devtools/client/performance/test/browser_timeline-waterfall-workers.js
--- a/devtools/client/performance/components/test/head.js
+++ b/devtools/client/performance/components/test/head.js
@@ -6,17 +6,16 @@
 /* global window, document, SimpleTest, requestAnimationFrame, is, ok */
 /* exported Cc, Ci, Cu, Cr, Assert, Task, TargetFactory, Toolbox, browserRequire,
    forceRender, setProps, dumpn, checkOptimizationHeader, checkOptimizationTree */
 let { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
 
 let { require } = Cu.import("resource://gre/modules/devtools/shared/Loader.jsm", {});
 let { Assert } = require("resource://testing-common/Assert.jsm");
 let { BrowserLoader } = Cu.import("resource://devtools/client/shared/browser-loader.js", {});
-let defer = require("devtools/shared/defer");
 let flags = require("devtools/shared/flags");
 let { Task } = require("devtools/shared/task");
 let { TargetFactory } = require("devtools/client/framework/target");
 let { Toolbox } = require("devtools/client/framework/toolbox");
 
 flags.testing = true;
 let { require: browserRequire } = BrowserLoader({
   baseURI: "resource://devtools/client/performance/",
@@ -36,25 +35,25 @@ SimpleTest.waitForExplicitFinish();
 
 function onNextAnimationFrame(fn) {
   return () =>
     requestAnimationFrame(() =>
       requestAnimationFrame(fn));
 }
 
 function setState(component, newState) {
-  let deferred = defer();
-  component.setState(newState, onNextAnimationFrame(deferred.resolve));
-  return deferred.promise;
+  return new Promise((resolve, reject) => {
+    component.setState(newState, onNextAnimationFrame(resolve));
+  });
 }
 
 function setProps(component, newState) {
-  let deferred = defer();
-  component.setProps(newState, onNextAnimationFrame(deferred.resolve));
-  return deferred.promise;
+  return new Promise((resolve, reject) => {
+    component.setProps(newState, onNextAnimationFrame(resolve));
+  });
 }
 
 function dumpn(msg) {
   dump(`PERFORMANCE-COMPONENT-TEST: ${msg}\n`);
 }
 
 /**
  * Default opts data for testing. First site has a simple IonType,
--- a/devtools/client/performance/panel.js
+++ b/devtools/client/performance/panel.js
@@ -23,57 +23,59 @@ exports.PerformancePanel = PerformancePa
 PerformancePanel.prototype = {
   /**
    * Open is effectively an asynchronous constructor.
    *
    * @return object
    *         A promise that is resolved when the Performance tool
    *         completes opening.
    */
-  open: Task.async(function* () {
+  open: function () {
     if (this._opening) {
       return this._opening;
     }
-    let deferred = promise.defer();
-    this._opening = deferred.promise;
+
+    const _open = async () => {
+      this.panelWin.gToolbox = this.toolbox;
+      this.panelWin.gTarget = this.target;
+      this._checkRecordingStatus = this._checkRecordingStatus.bind(this);
 
-    this.panelWin.gToolbox = this.toolbox;
-    this.panelWin.gTarget = this.target;
-    this._checkRecordingStatus = this._checkRecordingStatus.bind(this);
+      // Actor is already created in the toolbox; reuse
+      // the same front, and the toolbox will also initialize the front,
+      // but redo it here so we can hook into the same event to prevent race conditions
+      // in the case of the front still being in the process of opening.
+      let front = await this.panelWin.gToolbox.initPerformance();
 
-    // Actor is already created in the toolbox; reuse
-    // the same front, and the toolbox will also initialize the front,
-    // but redo it here so we can hook into the same event to prevent race conditions
-    // in the case of the front still being in the process of opening.
-    let front = yield this.panelWin.gToolbox.initPerformance();
+      // This should only happen if this is completely unsupported (when profiler
+      // does not exist), and in that case, the tool shouldn't be available,
+      // so let's ensure this assertion.
+      if (!front) {
+        console.error("No PerformanceFront found in toolbox.");
+      }
 
-    // This should only happen if this is completely unsupported (when profiler
-    // does not exist), and in that case, the tool shouldn't be available,
-    // so let's ensure this assertion.
-    if (!front) {
-      console.error("No PerformanceFront found in toolbox.");
-    }
+      this.panelWin.gFront = front;
+      let { PerformanceController, EVENTS } = this.panelWin;
+      PerformanceController.on(EVENTS.RECORDING_ADDED, this._checkRecordingStatus);
+      PerformanceController.on(EVENTS.RECORDING_STATE_CHANGE, this._checkRecordingStatus);
+      await this.panelWin.startupPerformance();
 
-    this.panelWin.gFront = front;
-    let { PerformanceController, EVENTS } = this.panelWin;
-    PerformanceController.on(EVENTS.RECORDING_ADDED, this._checkRecordingStatus);
-    PerformanceController.on(EVENTS.RECORDING_STATE_CHANGE, this._checkRecordingStatus);
-    yield this.panelWin.startupPerformance();
+      // Fire this once incase we have an in-progress recording (console profile)
+      // that caused this start up, and no state change yet, so we can highlight the
+      // tab if we need.
+      this._checkRecordingStatus();
 
-    // Fire this once incase we have an in-progress recording (console profile)
-    // that caused this start up, and no state change yet, so we can highlight the
-    // tab if we need.
-    this._checkRecordingStatus();
+      this.isReady = true;
+      this.emit("ready");
 
-    this.isReady = true;
-    this.emit("ready");
+      return this;
+    };
 
-    deferred.resolve(this);
+    this._opening = _open();
     return this._opening;
-  }),
+  },
 
   // DevToolPanel API
 
   get target() {
     return this.toolbox.target;
   },
 
   destroy: Task.async(function* () {
--- a/devtools/client/performance/performance-controller.js
+++ b/devtools/client/performance/performance-controller.js
@@ -529,26 +529,26 @@ var PerformanceController = {
   /**
    * Takes a PerformanceRecording and a state, and waits for
    * the event to be emitted from the front for that recording.
    *
    * @param {PerformanceRecordingFront} recording
    * @param {string} expectedState
    * @return {Promise}
    */
-  waitForStateChangeOnRecording: Task.async(function* (recording, expectedState) {
-    let deferred = promise.defer();
-    this.on(EVENTS.RECORDING_STATE_CHANGE, function handler(state, model) {
-      if (state === expectedState && model === recording) {
-        this.off(EVENTS.RECORDING_STATE_CHANGE, handler);
-        deferred.resolve();
-      }
+  waitForStateChangeOnRecording: function (recording, expectedState) {
+    return new Promise((resolve, reject) => {
+      this.on(EVENTS.RECORDING_STATE_CHANGE, function handler(state, model) {
+        if (state === expectedState && model === recording) {
+          this.off(EVENTS.RECORDING_STATE_CHANGE, handler);
+          resolve();
+        }
+      });
     });
-    yield deferred.promise;
-  }),
+  },
 
   /**
    * Called on init, sets an `e10s` attribute on the main view container with
    * "disabled" if e10s is possible on the platform and just not on, or "unsupported"
    * if e10s is not possible on the platform. If e10s is on, no attribute is set.
    */
   _setMultiprocessAttributes: function () {
     let { enabled, supported } = this.getMultiprocessStatus();
--- a/devtools/client/performance/test/browser_perf-recordings-io-03.js
+++ b/devtools/client/performance/test/browser_perf-recordings-io-03.js
@@ -33,24 +33,22 @@ var test = Task.async(function* () {
 function getUnicodeConverter() {
   let className = "@mozilla.org/intl/scriptableunicodeconverter";
   let converter = Cc[className].createInstance(Ci.nsIScriptableUnicodeConverter);
   converter.charset = "UTF-8";
   return converter;
 }
 
 function asyncCopy(data, file) {
-  let deferred = Promise.defer();
-
-  let string = JSON.stringify(data);
-  let inputStream = getUnicodeConverter().convertToInputStream(string);
-  let outputStream = FileUtils.openSafeFileOutputStream(file);
+  return new Promise((resolve, reject) => {
+    let string = JSON.stringify(data);
+    let inputStream = getUnicodeConverter().convertToInputStream(string);
+    let outputStream = FileUtils.openSafeFileOutputStream(file);
 
-  NetUtil.asyncCopy(inputStream, outputStream, status => {
-    if (!Components.isSuccessCode(status)) {
-      deferred.reject(new Error("Could not save data to file."));
-    }
-    deferred.resolve();
+    NetUtil.asyncCopy(inputStream, outputStream, status => {
+      if (!Components.isSuccessCode(status)) {
+        reject(new Error("Could not save data to file."));
+      }
+      resolve();
+    });
   });
-
-  return deferred.promise;
 }
 /* eslint-enable */
--- a/devtools/client/performance/test/browser_perf-recordings-io-04.js
+++ b/devtools/client/performance/test/browser_perf-recordings-io-04.js
@@ -155,24 +155,22 @@ var test = Task.async(function* () {
 function getUnicodeConverter() {
   let className = "@mozilla.org/intl/scriptableunicodeconverter";
   let converter = Cc[className].createInstance(Ci.nsIScriptableUnicodeConverter);
   converter.charset = "UTF-8";
   return converter;
 }
 
 function asyncCopy(data, file) {
-  let deferred = Promise.defer();
-
-  let string = JSON.stringify(data);
-  let inputStream = getUnicodeConverter().convertToInputStream(string);
-  let outputStream = FileUtils.openSafeFileOutputStream(file);
+  return new Promise((resolve, reject) => {
+    let string = JSON.stringify(data);
+    let inputStream = getUnicodeConverter().convertToInputStream(string);
+    let outputStream = FileUtils.openSafeFileOutputStream(file);
 
-  NetUtil.asyncCopy(inputStream, outputStream, status => {
-    if (!Components.isSuccessCode(status)) {
-      deferred.reject(new Error("Could not save data to file."));
-    }
-    deferred.resolve();
+    NetUtil.asyncCopy(inputStream, outputStream, status => {
+      if (!Components.isSuccessCode(status)) {
+        reject(new Error("Could not save data to file."));
+      }
+      resolve();
+    });
   });
-
-  return deferred.promise;
 }
 /* eslint-enable */
--- a/devtools/client/performance/test/browser_perf-recordings-io-06.js
+++ b/devtools/client/performance/test/browser_perf-recordings-io-06.js
@@ -119,24 +119,22 @@ var test = Task.async(function* () {
 function getUnicodeConverter() {
   let className = "@mozilla.org/intl/scriptableunicodeconverter";
   let converter = Cc[className].createInstance(Ci.nsIScriptableUnicodeConverter);
   converter.charset = "UTF-8";
   return converter;
 }
 
 function asyncCopy(data, file) {
-  let deferred = Promise.defer();
-
-  let string = JSON.stringify(data);
-  let inputStream = getUnicodeConverter().convertToInputStream(string);
-  let outputStream = FileUtils.openSafeFileOutputStream(file);
+  return new Promise((resolve, reject) => {
+    let string = JSON.stringify(data);
+    let inputStream = getUnicodeConverter().convertToInputStream(string);
+    let outputStream = FileUtils.openSafeFileOutputStream(file);
 
-  NetUtil.asyncCopy(inputStream, outputStream, status => {
-    if (!Components.isSuccessCode(status)) {
-      deferred.reject(new Error("Could not save data to file."));
-    }
-    deferred.resolve();
+    NetUtil.asyncCopy(inputStream, outputStream, status => {
+      if (!Components.isSuccessCode(status)) {
+        reject(new Error("Could not save data to file."));
+      }
+      resolve();
+    });
   });
-
-  return deferred.promise;
 }
 /* eslint-enable */
--- a/devtools/client/performance/test/browser_timeline-waterfall-workers.js
+++ b/devtools/client/performance/test/browser_timeline-waterfall-workers.js
@@ -68,30 +68,29 @@ function testWorkerMarkerUI(node) {
 }
 
 /**
  * Takes a string `script` and evaluates it directly in the content
  * in potentially a different process.
  */
 function evalInDebuggee(script) {
   let { generateUUID } = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
-  let deferred = Promise.defer();
 
-  if (!mm) {
-    throw new Error("`loadFrameScripts()` must be called when using MessageManager.");
-  }
-
-  let id = generateUUID().toString();
-  mm.sendAsyncMessage("devtools:test:eval", { script: script, id: id });
-  mm.addMessageListener("devtools:test:eval:response", handler);
-
-  function handler({ data }) {
-    if (id !== data.id) {
-      return;
+  return new Promise((resolve, reject) => {
+    if (!mm) {
+      throw new Error("`loadFrameScripts()` must be called when using MessageManager.");
     }
 
-    mm.removeMessageListener("devtools:test:eval:response", handler);
-    deferred.resolve(data.value);
-  }
+    let id = generateUUID().toString();
+    mm.sendAsyncMessage("devtools:test:eval", { script: script, id: id });
+    mm.addMessageListener("devtools:test:eval:response", handler);
 
-  return deferred.promise;
+    function handler({ data }) {
+      if (id !== data.id) {
+        return;
+      }
+
+      mm.removeMessageListener("devtools:test:eval:response", handler);
+      resolve(data.value);
+    }
+  });
 }
 /* eslint-enable */