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