3 Object.defineProperty(exports, "__esModule", {
6 exports.default = autoInject;
8 var _auto = require('./auto');
10 var _auto2 = _interopRequireDefault(_auto);
12 var _baseForOwn = require('lodash/_baseForOwn');
14 var _baseForOwn2 = _interopRequireDefault(_baseForOwn);
16 var _arrayMap = require('lodash/_arrayMap');
18 var _arrayMap2 = _interopRequireDefault(_arrayMap);
20 var _isArray = require('lodash/isArray');
22 var _isArray2 = _interopRequireDefault(_isArray);
24 var _trim = require('lodash/trim');
26 var _trim2 = _interopRequireDefault(_trim);
28 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30 var FN_ARGS = /^(function)?\s*[^\(]*\(\s*([^\)]*)\)/m;
31 var FN_ARG_SPLIT = /,/;
32 var FN_ARG = /(=.+)?(\s*)$/;
33 var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
35 function parseParams(func) {
36 func = func.toString().replace(STRIP_COMMENTS, '');
37 func = func.match(FN_ARGS)[2].replace(' ', '');
38 func = func ? func.split(FN_ARG_SPLIT) : [];
39 func = func.map(function (arg) {
40 return (0, _trim2.default)(arg.replace(FN_ARG, ''));
46 * A dependency-injected version of the [async.auto]{@link module:ControlFlow.auto} function. Dependent
47 * tasks are specified as parameters to the function, after the usual callback
48 * parameter, with the parameter names matching the names of the tasks it
49 * depends on. This can provide even more readable task graphs which can be
52 * If a final callback is specified, the task results are similarly injected,
53 * specified as named parameters after the initial error parameter.
55 * The autoInject function is purely syntactic sugar and its semantics are
56 * otherwise equivalent to [async.auto]{@link module:ControlFlow.auto}.
60 * @memberOf module:ControlFlow
62 * @see [async.auto]{@link module:ControlFlow.auto}
63 * @category Control Flow
64 * @param {Object} tasks - An object, each of whose properties is a function of
65 * the form 'func([dependencies...], callback). The object's key of a property
66 * serves as the name of the task defined by that property, i.e. can be used
67 * when specifying requirements for other tasks.
68 * * The `callback` parameter is a `callback(err, result)` which must be called
69 * when finished, passing an `error` (which can be `null`) and the result of
70 * the function's execution. The remaining parameters name other tasks on
71 * which the task is dependent, and the results from those tasks are the
72 * arguments of those parameters.
73 * @param {Function} [callback] - An optional callback which is called when all
74 * the tasks have been completed. It receives the `err` argument if any `tasks`
75 * pass an error to their callback, and a `results` object with any completed
76 * task results, similar to `auto`.
79 * // The example from `auto` can be rewritten as follows:
81 * get_data: function(callback) {
82 * // async code to get some data
83 * callback(null, 'data', 'converted to array');
85 * make_folder: function(callback) {
86 * // async code to create a directory to store a file in
87 * // this is run at the same time as getting the data
88 * callback(null, 'folder');
90 * write_file: function(get_data, make_folder, callback) {
91 * // once there is some data and the directory exists,
92 * // write the data to a file in the directory
93 * callback(null, 'filename');
95 * email_link: function(write_file, callback) {
96 * // once the file is written let's email a link to it...
97 * // write_file contains the filename returned by write_file.
98 * callback(null, {'file':write_file, 'email':'user@example.com'});
100 * }, function(err, results) {
101 * console.log('err = ', err);
102 * console.log('email_link = ', results.email_link);
105 * // If you are using a JS minifier that mangles parameter names, `autoInject`
106 * // will not work with plain functions, since the parameter names will be
107 * // collapsed to a single letter identifier. To work around this, you can
108 * // explicitly specify the names of the parameters your task function needs
109 * // in an array, similar to Angular.js dependency injection.
111 * // This still has an advantage over plain `auto`, since the results a task
112 * // depends on are still spread into arguments.
115 * write_file: ['get_data', 'make_folder', function(get_data, make_folder, callback) {
116 * callback(null, 'filename');
118 * email_link: ['write_file', function(write_file, callback) {
119 * callback(null, {'file':write_file, 'email':'user@example.com'});
122 * }, function(err, results) {
123 * console.log('err = ', err);
124 * console.log('email_link = ', results.email_link);
127 function autoInject(tasks, callback) {
130 (0, _baseForOwn2.default)(tasks, function (taskFn, key) {
133 if ((0, _isArray2.default)(taskFn)) {
134 params = taskFn.slice(0, -1);
135 taskFn = taskFn[taskFn.length - 1];
137 newTasks[key] = params.concat(params.length > 0 ? newTask : taskFn);
138 } else if (taskFn.length === 1) {
139 // no dependencies, use the function as-is
140 newTasks[key] = taskFn;
142 params = parseParams(taskFn);
143 if (taskFn.length === 0 && params.length === 0) {
144 throw new Error("autoInject task functions require explicit parameters.");
149 newTasks[key] = params.concat(newTask);
152 function newTask(results, taskCb) {
153 var newArgs = (0, _arrayMap2.default)(params, function (name) {
154 return results[name];
156 newArgs.push(taskCb);
157 taskFn.apply(null, newArgs);
161 (0, _auto2.default)(newTasks, callback);
163 module.exports = exports['default'];