cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kam...@apache.org
Subject git commit: Add util func for chaining promises
Date Wed, 11 Jun 2014 22:01:09 GMT
Repository: cordova-lib
Updated Branches:
  refs/heads/master e057df008 -> df71b3d67


Add util func for chaining promises

In many places we construct chains of promises consisting of the same function
called repeatedly with different args (a kind of promise-land forEach loop).
This is usually implemented with reduce( func(), Q() ) which results in a deep
indentation christmas tree. This change adds a utility function
Q_chainmap(argsArray, func) to use in such cases.


Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/df71b3d6
Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/df71b3d6
Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/df71b3d6

Branch: refs/heads/master
Commit: df71b3d6758580e037e98376e9d6a436d4f9f7c3
Parents: e057df0
Author: Mark Koudritsky <kamrik@gmail.com>
Authored: Wed Jun 11 17:52:21 2014 -0400
Committer: Mark Koudritsky <kamrik@gmail.com>
Committed: Wed Jun 11 17:52:21 2014 -0400

----------------------------------------------------------------------
 cordova-lib/src/cordova/platform.js | 31 ++++++++++++++-----------------
 cordova-lib/src/cordova/util.js     | 14 +++++++++++++-
 2 files changed, 27 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/df71b3d6/cordova-lib/src/cordova/platform.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/platform.js b/cordova-lib/src/cordova/platform.js
index 5f19aca..bb64cc8 100644
--- a/cordova-lib/src/cordova/platform.js
+++ b/cordova-lib/src/cordova/platform.js
@@ -69,23 +69,20 @@ function add(hooks, projectRoot, targets, opts) {
     }
 
     return hooks.fire('before_platform_add', opts)
-    .then(function() {
-        return targets.reduce(function(soFar, t) {
-            return soFar.then(function() {
-                return lazy_load.based_on_config(projectRoot, t, opts)
-                .then(function(libDir) {
-                    var template = config_json.lib && config_json.lib[t] &&
config_json.lib[t].template || null;
-                    var copts = null;
-                    if ('spawnoutput' in opts) {
-                        copts = opts.spawnoutput;
-                    }
-                    return call_into_create(t, projectRoot, cfg, libDir, template, copts);
-                }, function(err) {
-                    throw new CordovaError('Unable to fetch platform ' + t + ': ' + err);
-                });
-            });
-        }, Q());
-    })
+    .then(cordova_util.Q_chainmap(targets, function(t) {
+        return lazy_load.based_on_config(projectRoot, t, opts)
+        .fail(function(err) {
+            throw new CordovaError('Unable to fetch platform ' + t + ': ' + err);
+        })
+        .then(function(libDir) {
+            var template = config_json.lib && config_json.lib[t] && config_json.lib[t].template
|| null;
+            var copts = null;
+            if ('spawnoutput' in opts) {
+                copts = opts.spawnoutput;
+            }
+            return call_into_create(t, projectRoot, cfg, libDir, template, copts);
+        });
+    }))
     .then(function() {
         return hooks.fire('after_platform_add', opts);
     });

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/df71b3d6/cordova-lib/src/cordova/util.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/util.js b/cordova-lib/src/cordova/util.js
index 279aa06..217d7f1 100644
--- a/cordova-lib/src/cordova/util.js
+++ b/cordova-lib/src/cordova/util.js
@@ -21,6 +21,7 @@
 
 var fs            = require('fs'),
     path          = require('path'),
+    Q             = require('q'),
     CordovaError  = require('../CordovaError'),
     shell         = require('shelljs');
 
@@ -45,7 +46,7 @@ exports.projectWww = projectWww;
 exports.projectConfig = projectConfig;
 exports.preProcessOptions = preProcessOptions;
 exports.addModuleProperty = addModuleProperty;
-
+exports.Q_chainmap = Q_chainmap;
 
 function isRootDir(dir) {
     if (fs.existsSync(path.join(dir, 'www'))) {
@@ -233,3 +234,14 @@ function addModuleProperty(module, symbol, modulePath, opt_wrap, opt_obj)
{
     }
 }
 
+// Given a function and an array of values, creates a chain of promises that
+// will sequentially execute func(args[i]).
+function Q_chainmap(args, func) {
+    return function(inValue) {
+        return args.reduce(function(soFar, arg) {
+            return soFar.then(function(val) {
+                return func(arg, val);
+            });
+        }, Q(inValue));
+    };
+}


Mime
View raw message