Bug 1224545 - Remove async-utils/async method and use Task.async instead. r=jryans
authorPatrick Brosset <pbrosset@mozilla.com>
Fri, 13 Nov 2015 16:54:09 +0100
changeset 294555 b93bb7533917a55c5126bdeec5c610f2e4eaa202
parent 294554 b1b27945055fe1425ed51605a476781bf2288343
child 294556 a03d220894dd112363a0dd4bfab8e47645983da2
push id8824
push userraliiev@mozilla.com
push dateMon, 14 Dec 2015 20:18:56 +0000
treeherdermozilla-aurora@e2031358e2a6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjryans
bugs1224545
milestone45.0a1
Bug 1224545 - Remove async-utils/async method and use Task.async instead. r=jryans
devtools/server/actors/storage.js
devtools/shared/async-utils.js
devtools/shared/tests/unit/test_async-utils.js
--- a/devtools/server/actors/storage.js
+++ b/devtools/server/actors/storage.js
@@ -2,27 +2,27 @@
  * 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 {Cc, Ci} = require("chrome");
 const events = require("sdk/event/core");
 const protocol = require("devtools/server/protocol");
-const {async} = require("devtools/shared/async-utils");
 const {Arg, method, RetVal, types} = protocol;
 const {LongStringActor} = require("devtools/server/actors/string");
 const {DebuggerServer} = require("devtools/server/main");
 const Services = require("Services");
 const promise = require("promise");
 const {isWindowIncluded} = require("devtools/shared/layout/utils");
-const { setTimeout, clearTimeout } = require("sdk/timers");
+const {setTimeout, clearTimeout} = require("sdk/timers");
 
 loader.lazyImporter(this, "OS", "resource://gre/modules/osfile.jsm");
 loader.lazyImporter(this, "Sqlite", "resource://gre/modules/Sqlite.jsm");
+loader.lazyImporter(this, "Task", "resource://gre/modules/Task.jsm", "Task");
 
 var gTrackedMessageManager = new Map();
 
 // Maximum number of cookies/local storage key-value-pairs that can be sent
 // over the wire to the client in one request.
 const MAX_STORE_OBJECT_COUNT = 50;
 // Delay for the batch job that sends the accumulated update packets to the
 // client (ms).
@@ -240,17 +240,17 @@ StorageActors.defaults = function(typeNa
 
     /**
      * When a new window is added to the page. This generally means that a new
      * iframe is created, or the current window is completely reloaded.
      *
      * @param {window} window
      *        The window which was added.
      */
-    onWindowReady: async(function*(window) {
+    onWindowReady: Task.async(function*(window) {
       let host = this.getHostName(window.location);
       if (!this.hostVsStores.has(host)) {
         yield this.populateStoresForHost(host, window);
         let data = {};
         data[host] = this.getNamesForHost(host);
         this.storageActor.update("added", typeName, data);
       }
     }),
@@ -324,17 +324,17 @@ StorageActors.defaults = function(typeNa
      *
      * @return {object} An object containing following properties:
      *          - offset - The actual offset of the returned array. This might
      *                     be different from the requested offset if that was
      *                     invalid
      *          - total - The total number of entries possible.
      *          - data - The requested values.
      */
-    getStoreObjects: method(async(function*(host, names, options = {}) {
+    getStoreObjects: method(Task.async(function*(host, names, options = {}) {
       let offset = options.offset || 0;
       let size = options.size || MAX_STORE_OBJECT_COUNT;
       if (size > MAX_STORE_OBJECT_COUNT) {
         size = MAX_STORE_OBJECT_COUNT;
       }
       let sortOn = options.sortOn || "name";
 
       let toReturn = {
@@ -1138,25 +1138,25 @@ StorageActors.createActor({
   },
 
   /**
    * Purpose of this method is same as populateStoresForHosts but this is async.
    * This exact same operation cannot be performed in populateStoresForHosts
    * method, as that method is called in initialize method of the actor, which
    * cannot be asynchronous.
    */
-  preListStores: async(function*() {
+  preListStores: Task.async(function*() {
     this.hostVsStores = new Map();
 
     for (let host of this.hosts) {
       yield this.populateStoresForHost(host);
     }
   }),
 
-  populateStoresForHost: async(function*(host) {
+  populateStoresForHost: Task.async(function*(host) {
     let storeMap = new Map();
     let {names} = yield this.getDBNamesForHost(host);
 
     for (let name of names) {
       let metadata = yield this.getDBMetaData(host, name);
 
       indexedDBHelpers.patchMetadataMapsAndProtos(metadata);
       storeMap.set(name, metadata);
@@ -1287,17 +1287,17 @@ var indexedDBHelpers = {
     });
   },
 
   /**
    * Fetches and stores all the metadata information for the given database
    * `name` for the given `host`. The stored metadata information is of
    * `DatabaseMetadata` type.
    */
-  getDBMetaData: async(function*(host, name) {
+  getDBMetaData: Task.async(function*(host, name) {
     let request = this.openWithOrigin(host, name);
     let success = promise.defer();
 
     request.onsuccess = event => {
       let db = event.target.result;
 
       let dbData = new DatabaseMetadata(host, db);
       db.close();
@@ -1328,17 +1328,17 @@ var indexedDBHelpers = {
     }
 
     return require("indexedDB").openForPrincipal(principal, name);
   },
 
     /**
    * Fetches all the databases and their metadata for the given `host`.
    */
-  getDBNamesForHost: async(function*(host) {
+  getDBNamesForHost: Task.async(function*(host) {
     let sanitizedHost = this.getSanitizedHost(host);
     let directory = OS.Path.join(OS.Constants.Path.profileDir, "storage",
                                  "default", sanitizedHost, "idb");
 
     let exists = yield OS.File.exists(directory);
     if (!exists && host.startsWith("about:")) {
       // try for moz-safe-about directory
       sanitizedHost = this.getSanitizedHost("moz-safe-" + host);
@@ -1384,17 +1384,17 @@ var indexedDBHelpers = {
   getSanitizedHost: function(host) {
     return host.replace(ILLEGAL_CHAR_REGEX, "+");
   },
 
   /**
    * Retrieves the proper indexed db database name from the provided .sqlite
    * file location.
    */
-  getNameFromDatabaseFile: async(function*(path) {
+  getNameFromDatabaseFile: Task.async(function*(path) {
     let connection = null;
     let retryCount = 0;
 
     // Content pages might be having an open transaction for the same indexed db
     // which this sqlite file belongs to. In that case, sqlite.openConnection
     // will throw. Thus we retey for some time to see if lock is removed.
     while (!connection && retryCount++ < 25) {
       try {
@@ -1417,17 +1417,17 @@ var indexedDBHelpers = {
     let name = rows[0].getResultByName("name");
 
     yield connection.close();
 
     return name;
   }),
 
   getValuesForHost:
-  async(function*(host, name = "null", options, hostVsStores) {
+  Task.async(function*(host, name = "null", options, hostVsStores) {
     name = JSON.parse(name);
     if (!name || !name.length) {
       // This means that details about the db in this particular host are
       // requested.
       let dbs = [];
       if (hostVsStores.has(host)) {
         for (let [, db] of hostVsStores.get(host)) {
           indexedDBHelpers.patchMetadataMapsAndProtos(db);
@@ -1819,17 +1819,17 @@ var StorageActor = exports.StorageActor 
    * Lists the available hosts for all the registered storage types.
    *
    * @returns {object} An object containing with the following structure:
    *  - <storageType> : [{
    *      actor: <actorId>,
    *      host: <hostname>
    *    }]
    */
-  listStores: method(async(function*() {
+  listStores: method(Task.async(function*() {
     let toReturn = {};
 
     for (let [name, value] of this.childActorPool) {
       if (value.preListStores) {
         yield value.preListStores();
       }
       toReturn[name] = value;
     }
--- a/devtools/shared/async-utils.js
+++ b/devtools/shared/async-utils.js
@@ -13,30 +13,16 @@
  * See Task documentation at https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Task.jsm.
  */
 
 var {Cu} = require("chrome");
 var {Task} = require("resource://gre/modules/Task.jsm");
 var Promise = require("promise");
 
 /**
- * Create an async function from a generator function.
- *
- * @param Function func
- *        The generator function that to wrap as an async function.
- * @return Function
- *         The async function.
- */
-exports.async = function async(func) {
-  return function(...args) {
-    return Task.spawn(func.apply(this, args));
-  };
-};
-
-/**
  * Create an async function that only executes once per instance of an object.
  * Once called on a given object, the same promise will be returned for any
  * future calls for that object.
  *
  * @param Function func
  *        The generator function that to wrap as an async function.
  * @return Function
  *         The async function.
--- a/devtools/shared/tests/unit/test_async-utils.js
+++ b/devtools/shared/tests/unit/test_async-utils.js
@@ -7,26 +7,25 @@
 const {Task} = Cu.import("resource://gre/modules/Task.jsm", {});
 // |const| will not work because
 // it will make the Promise object immutable before assigning.
 // Using Object.defineProperty() instead.
 Object.defineProperty(this, "Promise", {
   value: require("promise"),
   writable: false, configurable: false
 });
-const {async, asyncOnce, promiseInvoke, promiseCall} = require("devtools/shared/async-utils");
+const {asyncOnce, promiseInvoke, promiseCall} = require("devtools/shared/async-utils");
 
 function run_test() {
   do_test_pending();
   Task.spawn(function*() {
-    for (let helper of [async, asyncOnce]) {
-      yield test_async_args(helper);
-      yield test_async_return(helper);
-      yield test_async_throw(helper);
-    }
+    yield test_async_args(asyncOnce);
+    yield test_async_return(asyncOnce);
+    yield test_async_throw(asyncOnce);
+
     yield test_async_once();
     yield test_async_invoke();
     do_test_finished();
   }).then(null, error => {
     do_throw(error);
   });
 }