Bug 681843 - Part 3: Move Utils.asyncChain to Async.chain. r=rnewman
authorPhilipp von Weitershausen <philipp@weitershausen.de>
Fri, 26 Aug 2011 10:25:42 -0700
changeset 76300 c1b060492b78e409a0d5835e203b105780faf95b
parent 76299 725bf4c716978a0fdcd9702fa1132408acb0aaa4
child 76301 5c5fa8d8009e64f3b41d178f27b6864bc29466e9
push id3
push userfelipc@gmail.com
push dateFri, 30 Sep 2011 20:09:13 +0000
reviewersrnewman
bugs681843
milestone9.0a1
Bug 681843 - Part 3: Move Utils.asyncChain to Async.chain. r=rnewman
services/sync/modules/async.js
services/sync/modules/jpakeclient.js
services/sync/modules/util.js
services/sync/tests/unit/test_async_chain.js
services/sync/tests/unit/test_resource_ua.js
services/sync/tests/unit/test_utils_asyncChain.js
services/sync/tests/unit/xpcshell.ini
--- a/services/sync/modules/async.js
+++ b/services/sync/modules/async.js
@@ -52,16 +52,46 @@ const REASON_ERROR = Ci.mozIStorageState
 Cu.import("resource://gre/modules/Services.jsm");
 
 /*
  * Helpers for various async operations.
  */
 let Async = {
 
   /**
+   * Execute an arbitrary number of asynchronous functions one after the
+   * other, passing the callback arguments on to the next one.  All functions
+   * must take a callback function as their last argument.  The 'this' object
+   * will be whatever chain()'s is.
+   * 
+   * @usage this._chain = Async.chain;
+   *        this._chain(this.foo, this.bar, this.baz)(args, for, foo)
+   * 
+   * This is equivalent to:
+   *
+   *   let self = this;
+   *   self.foo(args, for, foo, function (bars, args) {
+   *     self.bar(bars, args, function (baz, params) {
+   *       self.baz(baz, params);
+   *     });
+   *   });
+   */
+  chain: function chain() {
+    let funcs = Array.slice(arguments);
+    let thisObj = this;
+    return function callback() {
+      if (funcs.length) {
+        let args = Array.slice(arguments).concat(callback);
+        let f = funcs.shift();
+        f.apply(thisObj, args);
+      }
+    };
+  },
+
+  /**
    * Helpers for making asynchronous calls within a synchronous API possible.
    *
    * If you value your sanity, do not look closely at the following functions.
    */
 
   /**
    * Create a sync callback that remembers state, in particular whether it has
    * been called.
--- a/services/sync/modules/jpakeclient.js
+++ b/services/sync/modules/jpakeclient.js
@@ -137,17 +137,17 @@ function JPAKEClient(observer) {
 
   this._jpake = Cc["@mozilla.org/services-crypto/sync-jpake;1"]
                   .createInstance(Ci.nsISyncJPAKE);
 
   this._setClientID();
 }
 JPAKEClient.prototype = {
 
-  _chain: Utils.asyncChain,
+  _chain: Async.chain,
 
   /*
    * Public API
    */
 
   receiveNoPIN: function receiveNoPIN() {
     this._my_signerid = JPAKE_SIGNERID_RECEIVER;
     this._their_signerid = JPAKE_SIGNERID_SENDER;
--- a/services/sync/modules/util.js
+++ b/services/sync/modules/util.js
@@ -57,46 +57,16 @@ Cu.import("resource://gre/modules/NetUti
 Cu.import("resource://gre/modules/FileUtils.jsm");
 
 /*
  * Utility functions
  */
 
 let Utils = {
   /**
-   * Execute an arbitrary number of asynchronous functions one after the
-   * other, passing the callback arguments on to the next one.  All functions
-   * must take a callback function as their last argument.  The 'this' object
-   * will be whatever asyncChain's is.
-   * 
-   * @usage this._chain = Utils.asyncChain;
-   *        this._chain(this.foo, this.bar, this.baz)(args, for, foo)
-   * 
-   * This is equivalent to:
-   *
-   *   let self = this;
-   *   self.foo(args, for, foo, function (bars, args) {
-   *     self.bar(bars, args, function (baz, params) {
-   *       self.baz(baz, params);
-   *     });
-   *   });
-   */
-  asyncChain: function asyncChain() {
-    let funcs = Array.slice(arguments);
-    let thisObj = this;
-    return function callback() {
-      if (funcs.length) {
-        let args = Array.slice(arguments).concat(callback);
-        let f = funcs.shift();
-        f.apply(thisObj, args);
-      }
-    };
-  },
-
-  /**
    * Wrap a function to catch all exceptions and log them
    *
    * @usage MyObj._catch = Utils.catch;
    *        MyObj.foo = function() { this._catch(func)(); }
    *        
    * Optionally pass a function which will be called if an
    * exception occurs.
    */
rename from services/sync/tests/unit/test_utils_asyncChain.js
rename to services/sync/tests/unit/test_async_chain.js
--- a/services/sync/tests/unit/test_utils_asyncChain.js
+++ b/services/sync/tests/unit/test_async_chain.js
@@ -1,9 +1,9 @@
-Cu.import("resource://services-sync/util.js");
+Cu.import("resource://services-sync/async.js");
 
 function run_test() {
   _("Chain a few async methods, making sure the 'this' object is correct.");
 
   let methods = {
     save: function(x, callback) {
       this.x = x;
       callback(x);
@@ -13,15 +13,15 @@ function run_test() {
     },
     double: function(x, callback) {
       callback(x * 2);
     },
     neg: function(x, callback) {
       callback(-x);
     }
   };
-  methods.chain = Utils.asyncChain;
+  methods.chain = Async.chain;
 
   // ((1 + 1 + 1) * (-1) + 1) * 2 + 1 = -3
   methods.chain(methods.save, methods.addX, methods.addX, methods.neg,
                 methods.addX, methods.double, methods.addX, methods.save)(1);
   do_check_eq(methods.x, -3);
 }
--- a/services/sync/tests/unit/test_resource_ua.js
+++ b/services/sync/tests/unit/test_resource_ua.js
@@ -71,17 +71,17 @@ function test_resource_user_agent() {
     r.get(function (error, content) {
       _("User-Agent: " + ua);
       do_check_eq(ua, expectedUA + ".mobile");
       ua = "";
       next();
     });
   }
 
-  Utils.asyncChain(
+  Async.chain(
     test_fetchInfo,
     test_desktop_post,
     test_desktop_get,
     test_mobile_get,
     function (next) {
       server.stop(next);
     },
     do_test_finished)();
--- a/services/sync/tests/unit/xpcshell.ini
+++ b/services/sync/tests/unit/xpcshell.ini
@@ -1,14 +1,15 @@
 [DEFAULT]
 head = head_appinfo.js head_helpers.js head_http_server.js
 tail = 
 
 [test_Observers.js]
 [test_Preferences.js]
+[test_async_chain.js]
 [test_async_querySpinningly.js]
 [test_auth_manager.js]
 [test_bookmark_batch_fail.js]
 [test_bookmark_engine.js]
 [test_bookmark_legacy_microsummaries_support.js]
 [test_bookmark_livemarks.js]
 [test_bookmark_order.js]
 [test_bookmark_places_query_rewriting.js]
@@ -95,17 +96,16 @@ skip-if = (os == "mac" && debug) || os =
 skip-if = (os == "mac" && debug) || os == "android"
 [test_syncscheduler.js]
 [test_syncstoragerequest.js]
 [test_tab_engine.js]
 [test_tab_store.js]
 [test_tab_tracker.js]
 [test_tracker_addChanged.js]
 [test_upgrade_old_sync_key.js]
-[test_utils_asyncChain.js]
 [test_utils_atob.js]
 [test_utils_catch.js]
 [test_utils_deepCopy.js]
 [test_utils_deepEquals.js]
 [test_utils_deferGetSet.js]
 [test_utils_deriveKey.js]
 [test_utils_encodeBase32.js]
 [test_utils_ensureOneOpen.js]