summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Early <alexander.early@gmail.com>2016-03-09 13:07:33 -0800
committerAlex Early <alexander.early@gmail.com>2016-03-09 13:07:33 -0800
commitb5ea2b1cd0d01a45aa155663808f0cdf46ff1fe9 (patch)
tree301e8e0b2f165eba1d06ef32870294d775884b55
parent5aadb0da9d738a3cf715599c982a1ae29aff9797 (diff)
parentdf31042d689ba51e383209b2836ad971c11dee07 (diff)
downloadasync-b5ea2b1cd0d01a45aa155663808f0cdf46ff1fe9.tar.gz
Merge pull request #1055 from caolan/autoinject
autoInject
-rw-r--r--README.md70
-rw-r--r--lib/autoInject.js46
-rw-r--r--lib/index.js3
-rw-r--r--mocha_test/autoInject.js77
4 files changed, 195 insertions, 1 deletions
diff --git a/README.md b/README.md
index d527b42..b872bc8 100644
--- a/README.md
+++ b/README.md
@@ -219,6 +219,7 @@ Some functions are also available in the following forms:
* [`queue`](#queue), [`priorityQueue`](#priorityQueue)
* [`cargo`](#cargo)
* [`auto`](#auto)
+* [`autoInject`](#autoInject)
* [`retry`](#retry)
* [`iterator`](#iterator)
* [`times`](#times), `timesSeries`, `timesLimit`
@@ -1291,7 +1292,7 @@ methods:
* `saturated` - A callback that is called when the `queue.length()` hits the concurrency and further tasks will be queued.
* `empty` - A callback that is called when the last item from the `queue` is given to a `worker`.
* `drain` - A callback that is called when the last item from the `queue` has returned from the `worker`.
-* `idle()`, `pause()`, `resume()`, `kill()` - cargo inherits all of the same methods and event calbacks as [`queue`](#queue)
+* `idle()`, `pause()`, `resume()`, `kill()` - cargo inherits all of the same methods and event callbacks as [`queue`](#queue)
__Example__
@@ -1421,6 +1422,73 @@ function(err, results){
For a complicated series of `async` tasks, using the [`auto`](#auto) function makes adding new tasks much easier (and the code more readable).
---------------------------------------
+<a name="autoInject" />
+### autoInject(tasks, [callback])
+
+A dependency-injected version of the [`auto`](#auto) function. Dependent tasks are specified as parameters to the function, after the usual callback parameter, with the parameter names matching the names of the tasks it depends on. This can provide even more readable task graphs which can be easier to maintain.
+
+If a final callback is specified, the task results are similarly injected, specified as named parameters after the initial error parameter.
+
+The autoInject function is purely syntactic sugar and its semantics are otherwise equivalent to [`auto`](#auto).
+
+__Arguments__
+
+* `tasks` - An object, each of whose properties is a function of the form
+ 'func([dependencies...], callback). The object's key of a property serves as the name of the task defined by that property, i.e. can be used when specifying requirements for other tasks.
+ * The `callback` parameter is a `callback(err, result)` which must be called when finished, passing an `error` (which can be `null`) and the result of the function's execution. The remaining parameters name other tasks on which the task is dependent, and the results from those tasks are the arguments of those parameters.
+* `callback(err, [results...])` - An optional callback which is called when all the tasks have been completed. It receives the `err` argument if any `tasks` pass an error to their callback. The remaining parameters are task names whose results you are interested in. This callback will only be called when all tasks have finished or an error has occurred, and so do not not specify dependencies in the same way as `tasks` do. If an error occurs, no further `tasks` will be performed, and `results` will only be valid for those tasks which managed to complete.
+
+
+__Example__
+
+The example from [`auto`](#auto) can be rewritten as follows:
+
+```js
+async.autoInject({
+ get_data: function(callback){
+ // async code to get some data
+ callback(null, 'data', 'converted to array');
+ },
+ make_folder: function(callback){
+ // async code to create a directory to store a file in
+ // this is run at the same time as getting the data
+ callback(null, 'folder');
+ },
+ write_file: function(get_data, make_folder, callback){
+ // once there is some data and the directory exists,
+ // write the data to a file in the directory
+ callback(null, 'filename');
+ },
+ email_link: function(write_file, callback){
+ // once the file is written let's email a link to it...
+ // write_file contains the filename returned by write_file.
+ callback(null, {'file':write_file, 'email':'user@example.com'});
+ }
+}, function(err, email_link) {
+ console.log('err = ', err);
+ console.log('email_link = ', email_link);
+});
+```
+
+If you are using a JS minifier that mangles parameter names, `autoInject` will not work with plain functions, since the parameter names will be collapsed to a single letter identifier. To work around this, you can explicitly specify the names of the parameters your task function needs in an array, similar to Angular.js dependency injection.
+
+```js
+async.autoInject({
+ //...
+ write_file: ['get_data', 'make_folder', function(get_data, make_folder, callback){
+ callback(null, 'filename');
+ }],
+ email_link: ['write_file', function(write_file, callback){
+ callback(null, {'file':write_file, 'email':'user@example.com'});
+ }]
+ //...
+},
+```
+
+This still has an advantage over plain `auto`, since the results a task depends on are still spread into arguments.
+
+
+---------------------------------------
<a name="retry"></a>
### retry([opts = {times: 5, interval: 0}| 5], task, [callback])
diff --git a/lib/autoInject.js b/lib/autoInject.js
new file mode 100644
index 0000000..f84a4d9
--- /dev/null
+++ b/lib/autoInject.js
@@ -0,0 +1,46 @@
+import auto from './auto';
+import forOwn from 'lodash/forOwn';
+import arrayMap from 'lodash/_arrayMap';
+import clone from 'lodash/_baseClone';
+import isArray from 'lodash/isArray';
+
+var argsRegex = /^function\s*[^\(]*\(\s*([^\)]*)\)/m;
+
+function parseParams(func) {
+ return func.toString().match(argsRegex)[1].split(/\s*\,\s*/);
+}
+
+export default function autoInject(tasks, callback) {
+ var newTasks = {};
+
+ forOwn(tasks, function (taskFn, key) {
+ var params;
+
+ if (isArray(taskFn)) {
+ params = clone(taskFn);
+ taskFn = params.pop();
+
+ newTasks[key] = clone(params).concat(newTask);
+ } else if (taskFn.length === 0) {
+ throw new Error("autoInject task functions require explicit parameters.");
+ } else if (taskFn.length === 1) {
+ // no dependencies, use the function as-is
+ newTasks[key] = taskFn;
+ } else {
+ params = parseParams(taskFn);
+ params.pop();
+
+ newTasks[key] = clone(params).concat(newTask);
+ }
+
+ function newTask(results, taskCb) {
+ var newArgs = arrayMap(params, function (name) {
+ return results[name];
+ });
+ newArgs.push(taskCb);
+ taskFn.apply(null, newArgs);
+ }
+ });
+
+ auto(newTasks, callback);
+}
diff --git a/lib/index.js b/lib/index.js
index 300491d..3db4064 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -5,6 +5,7 @@ import applyEachSeries from './applyEachSeries';
import apply from './apply';
import asyncify from './asyncify';
import auto from './auto';
+import autoInject from './autoInject';
import cargo from './cargo';
import compose from './compose';
import concat from './concat';
@@ -71,6 +72,7 @@ export default {
apply: apply,
asyncify: asyncify,
auto: auto,
+ autoInject: autoInject,
cargo: cargo,
compose: compose,
concat: concat,
@@ -155,6 +157,7 @@ export {
apply as apply,
asyncify as asyncify,
auto as auto,
+ autoInject as autoInject,
cargo as cargo,
compose as compose,
concat as concat,
diff --git a/mocha_test/autoInject.js b/mocha_test/autoInject.js
new file mode 100644
index 0000000..53a3d17
--- /dev/null
+++ b/mocha_test/autoInject.js
@@ -0,0 +1,77 @@
+var async = require('../lib');
+var expect = require('chai').expect;
+var _ = require('lodash');
+
+describe('autoInject', function () {
+
+ it("basics", function (done) {
+ var callOrder = [];
+ async.autoInject({
+ task1: function(task2, callback){
+ expect(task2).to.equal(2);
+ setTimeout(function(){
+ callOrder.push('task1');
+ callback(null, 1);
+ }, 25);
+ },
+ task2: function(callback){
+ setTimeout(function(){
+ callOrder.push('task2');
+ callback(null, 2);
+ }, 50);
+ },
+ task3: function(task2, callback){
+ expect(task2).to.equal(2);
+ callOrder.push('task3');
+ callback(null, 3);
+ },
+ task4: function(task1, task2, callback){
+ expect(task1).to.equal(1);
+ expect(task2).to.equal(2);
+ callOrder.push('task4');
+ callback(null, 4);
+ },
+ task5: function(task2, callback){
+ expect(task2).to.equal(2);
+ setTimeout(function(){
+ callOrder.push('task5');
+ callback(null, 5);
+ }, 0);
+ },
+ task6: function(task2, callback){
+ expect(task2).to.equal(2);
+ callOrder.push('task6');
+ callback(null, 6);
+ }
+ },
+ function(err, results){
+ expect(results.task6).to.equal(6);
+ expect(callOrder).to.eql(['task2','task6','task3','task5','task1','task4']);
+ done();
+ });
+ });
+
+ it('should work with array tasks', function (done) {
+ var callOrder = [];
+
+ async.autoInject({
+ task1: function (cb) {
+ callOrder.push('task1');
+ cb(null, 1);
+ },
+ task2: ['task3', function (task3, cb) {
+ expect(task3).to.equal(3);
+ callOrder.push('task2');
+ cb(null, 2);
+ }],
+ task3: function (cb) {
+ callOrder.push('task3');
+ cb(null, 3);
+ }
+ }, function () {
+ expect(callOrder).to.eql(['task1','task3','task2']);
+ done();
+ });
+ });
+
+});