Bug 1350648 - Replace defer with new Promise in devtools/client/netmonitor directory. r=tromey
authorehmpee <powell.759@osu.edu>
Thu, 13 Apr 2017 11:51:31 -0400
changeset 353028 2c4240b6dff9
parent 353027 1ca47045ab08
child 353029 78b39a056304
push id40747
push userryanvm@gmail.com
push dateFri, 14 Apr 2017 01:47:32 +0000
treeherderautoland@78b39a056304 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstromey
bugs1350648
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 1350648 - Replace defer with new Promise in devtools/client/netmonitor directory. r=tromey
devtools/client/netmonitor/test/browser_net_throttle.js
devtools/client/netmonitor/test/head.js
--- a/devtools/client/netmonitor/test/browser_net_throttle.js
+++ b/devtools/client/netmonitor/test/browser_net_throttle.js
@@ -37,21 +37,21 @@ function* throttleTest(actuallyThrottle)
       downloadBPSMax: size,
       uploadBPSMean: 10000,
       uploadBPSMax: 10000,
     },
   };
   let client = NetMonitorController.webConsoleClient;
 
   info("sending throttle request");
-  let deferred = promise.defer();
-  client.setPreferences(request, response => {
-    deferred.resolve(response);
+  yield new Promise((resolve) => {
+    client.setPreferences(request, response => {
+      resolve(response);
+    });
   });
-  yield deferred.promise;
 
   let eventPromise = monitor.panelWin.once(EVENTS.RECEIVED_EVENT_TIMINGS);
   yield NetMonitorController.triggerActivity(ACTIVITY_TYPE.RELOAD.WITH_CACHE_DISABLED);
   yield eventPromise;
 
   let requestItem = getSortedRequests(gStore.getState()).get(0);
   const reportedOneSecond = requestItem.eventTimings.timings.receive > 1000;
   if (actuallyThrottle) {
--- a/devtools/client/netmonitor/test/head.js
+++ b/devtools/client/netmonitor/test/head.js
@@ -90,29 +90,29 @@ registerCleanupFunction(() => {
   info("finish() was called, cleaning up...");
 
   Services.prefs.setBoolPref("devtools.debugger.log", gEnableLogging);
   Services.prefs.setCharPref("devtools.netmonitor.filters", gDefaultFilters);
   Services.prefs.clearUserPref("devtools.cache.disabled");
 });
 
 function waitForNavigation(target) {
-  let deferred = promise.defer();
-  target.once("will-navigate", () => {
-    target.once("navigate", () => {
-      deferred.resolve();
+  return new Promise((resolve) => {
+    target.once("will-navigate", () => {
+      target.once("navigate", () => {
+        resolve();
+      });
     });
   });
-  return deferred.promise;
 }
 
 function reconfigureTab(target, options) {
-  let deferred = promise.defer();
-  target.activeTab.reconfigure(options, deferred.resolve);
-  return deferred.promise;
+  return new Promise((resolve) => {
+    target.activeTab.reconfigure(options, resolve);
+  });
 }
 
 function toggleCache(target, disabled) {
   let options = { cacheDisabled: disabled, performReload: true };
   let navigationFinished = waitForNavigation(target);
 
   // Disable the cache for any toolbox that it is opened from this point on.
   Services.prefs.setBoolPref("devtools.cache.disabled", disabled);
@@ -174,92 +174,92 @@ function teardown(monitor) {
 
     let onDestroyed = monitor.once("destroyed");
     yield removeTab(tab);
     yield onDestroyed;
   });
 }
 
 function waitForNetworkEvents(monitor, getRequests, postRequests = 0) {
-  let deferred = promise.defer();
-  let panel = monitor.panelWin;
-  let { windowRequire } = panel;
-  let { NetMonitorController } =
-    windowRequire("devtools/client/netmonitor/src/netmonitor-controller");
-  let progress = {};
-  let genericEvents = 0;
-  let postEvents = 0;
-  let awaitedEventsToListeners = [
-    ["UPDATING_REQUEST_HEADERS", onGenericEvent],
-    ["RECEIVED_REQUEST_HEADERS", onGenericEvent],
-    ["UPDATING_REQUEST_COOKIES", onGenericEvent],
-    ["RECEIVED_REQUEST_COOKIES", onGenericEvent],
-    ["UPDATING_REQUEST_POST_DATA", onPostEvent],
-    ["RECEIVED_REQUEST_POST_DATA", onPostEvent],
-    ["UPDATING_RESPONSE_HEADERS", onGenericEvent],
-    ["RECEIVED_RESPONSE_HEADERS", onGenericEvent],
-    ["UPDATING_RESPONSE_COOKIES", onGenericEvent],
-    ["RECEIVED_RESPONSE_COOKIES", onGenericEvent],
-    ["STARTED_RECEIVING_RESPONSE", onGenericEvent],
-    ["UPDATING_RESPONSE_CONTENT", onGenericEvent],
-    ["RECEIVED_RESPONSE_CONTENT", onGenericEvent],
-    ["UPDATING_EVENT_TIMINGS", onGenericEvent],
-    ["RECEIVED_EVENT_TIMINGS", onGenericEvent]
-  ];
+  return new Promise((resolve) => {
+    let panel = monitor.panelWin;
+    let { windowRequire } = panel;
+    let { NetMonitorController } =
+      windowRequire("devtools/client/netmonitor/src/netmonitor-controller");
+    let progress = {};
+    let genericEvents = 0;
+    let postEvents = 0;
+    let awaitedEventsToListeners = [
+      ["UPDATING_REQUEST_HEADERS", onGenericEvent],
+      ["RECEIVED_REQUEST_HEADERS", onGenericEvent],
+      ["UPDATING_REQUEST_COOKIES", onGenericEvent],
+      ["RECEIVED_REQUEST_COOKIES", onGenericEvent],
+      ["UPDATING_REQUEST_POST_DATA", onPostEvent],
+      ["RECEIVED_REQUEST_POST_DATA", onPostEvent],
+      ["UPDATING_RESPONSE_HEADERS", onGenericEvent],
+      ["RECEIVED_RESPONSE_HEADERS", onGenericEvent],
+      ["UPDATING_RESPONSE_COOKIES", onGenericEvent],
+      ["RECEIVED_RESPONSE_COOKIES", onGenericEvent],
+      ["STARTED_RECEIVING_RESPONSE", onGenericEvent],
+      ["UPDATING_RESPONSE_CONTENT", onGenericEvent],
+      ["RECEIVED_RESPONSE_CONTENT", onGenericEvent],
+      ["UPDATING_EVENT_TIMINGS", onGenericEvent],
+      ["RECEIVED_EVENT_TIMINGS", onGenericEvent]
+    ];
 
-  function initProgressForURL(url) {
-    if (progress[url]) {
-      return;
+    function initProgressForURL(url) {
+      if (progress[url]) {
+        return;
+      }
+      progress[url] = {};
+      awaitedEventsToListeners.forEach(function ([e]) {
+        progress[url][e] = 0;
+      });
     }
-    progress[url] = {};
-    awaitedEventsToListeners.forEach(function ([e]) {
-      progress[url][e] = 0;
-    });
-  }
 
-  function updateProgressForURL(url, event) {
-    initProgressForURL(url);
-    progress[url][Object.keys(EVENTS).find(e => EVENTS[e] == event)] = 1;
-  }
+    function updateProgressForURL(url, event) {
+      initProgressForURL(url);
+      progress[url][Object.keys(EVENTS).find(e => EVENTS[e] == event)] = 1;
+    }
 
-  function onGenericEvent(event, actor) {
-    genericEvents++;
-    maybeResolve(event, actor);
-  }
+    function onGenericEvent(event, actor) {
+      genericEvents++;
+      maybeResolve(event, actor);
+    }
 
-  function onPostEvent(event, actor) {
-    postEvents++;
-    maybeResolve(event, actor);
-  }
+    function onPostEvent(event, actor) {
+      postEvents++;
+      maybeResolve(event, actor);
+    }
 
-  function maybeResolve(event, actor) {
-    info("> Network events progress: " +
-      genericEvents + "/" + ((getRequests + postRequests) * 13) + ", " +
-      postEvents + "/" + (postRequests * 2) + ", " +
-      "got " + event + " for " + actor);
+    function maybeResolve(event, actor) {
+      info("> Network events progress: " +
+        genericEvents + "/" + ((getRequests + postRequests) * 13) + ", " +
+        postEvents + "/" + (postRequests * 2) + ", " +
+        "got " + event + " for " + actor);
 
-    let networkInfo = NetMonitorController.webConsoleClient.getNetworkRequest(actor);
-    let url = networkInfo.request.url;
-    updateProgressForURL(url, event);
+      let networkInfo = NetMonitorController.webConsoleClient.getNetworkRequest(actor);
+      let url = networkInfo.request.url;
+      updateProgressForURL(url, event);
 
-    // Uncomment this to get a detailed progress logging (when debugging a test)
-    // info("> Current state: " + JSON.stringify(progress, null, 2));
+      // Uncomment this to get a detailed progress logging (when debugging a test)
+      // info("> Current state: " + JSON.stringify(progress, null, 2));
 
-    // There are 15 updates which need to be fired for a request to be
-    // considered finished. The "requestPostData" packet isn't fired for
-    // non-POST requests.
-    if (genericEvents >= (getRequests + postRequests) * 13 &&
-        postEvents >= postRequests * 2) {
-      awaitedEventsToListeners.forEach(([e, l]) => panel.off(EVENTS[e], l));
-      executeSoon(deferred.resolve);
+      // There are 15 updates which need to be fired for a request to be
+      // considered finished. The "requestPostData" packet isn't fired for
+      // non-POST requests.
+      if (genericEvents >= (getRequests + postRequests) * 13 &&
+          postEvents >= postRequests * 2) {
+        awaitedEventsToListeners.forEach(([e, l]) => panel.off(EVENTS[e], l));
+        executeSoon(resolve);
+      }
     }
-  }
 
-  awaitedEventsToListeners.forEach(([e, l]) => panel.on(EVENTS[e], l));
-  return deferred.promise;
+    awaitedEventsToListeners.forEach(([e, l]) => panel.on(EVENTS[e], l));
+  });
 }
 
 function verifyRequestItemTarget(document, requestList, requestItem, method,
                                  url, data = {}) {
   info("> Verifying: " + method + " " + url + " " + data.toSource());
 
   let visibleIndex = requestList.indexOf(requestItem);
 
@@ -380,19 +380,19 @@ function verifyRequestItemTarget(documen
  * @param object subject
  *        The event emitter object that is being listened to.
  * @param string eventName
  *        The name of the event to listen to.
  * @return object
  *        Returns a promise that resolves upon firing of the event.
  */
 function waitFor(subject, eventName) {
-  let deferred = promise.defer();
-  subject.once(eventName, deferred.resolve);
-  return deferred.promise;
+  return new Promise((resolve) => {
+    subject.once(eventName, resolve);
+  });
 }
 
 /**
  * Tests if a button for a filter of given type is the only one checked.
  *
  * @param string filterType
  *        The type of the filter that should be the only one checked.
  */
@@ -495,15 +495,15 @@ function executeInContent(name, data = {
  * messagemanager is used).
  * @param {String} name The message name
  * @return {Promise} A promise that resolves to the response data when the
  * message has been received
  */
 function waitForContentMessage(name) {
   let mm = gBrowser.selectedBrowser.messageManager;
 
-  let def = promise.defer();
-  mm.addMessageListener(name, function onMessage(msg) {
-    mm.removeMessageListener(name, onMessage);
-    def.resolve(msg);
+  return new Promise((resolve) => {
+    mm.addMessageListener(name, function onMessage(msg) {
+      mm.removeMessageListener(name, onMessage);
+      resolve(msg);
+    });
   });
-  return def.promise;
 }