root / HServer / 00.Server / 00.Program / node_modules / async / autoInject.js
이력 | 보기 | 이력해설 | 다운로드 (5.97 KB)
1 |
'use strict';
|
---|---|
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 |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
29 |
|
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; |
34 |
|
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, '')); |
41 |
}); |
42 |
return func;
|
43 |
} |
44 |
|
45 |
/**
|
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
|
50 |
* easier to maintain.
|
51 |
*
|
52 |
* If a final callback is specified, the task results are similarly injected,
|
53 |
* specified as named parameters after the initial error parameter.
|
54 |
*
|
55 |
* The autoInject function is purely syntactic sugar and its semantics are
|
56 |
* otherwise equivalent to [async.auto]{@link module:ControlFlow.auto}.
|
57 |
*
|
58 |
* @name autoInject
|
59 |
* @static
|
60 |
* @memberOf module:ControlFlow
|
61 |
* @method
|
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`.
|
77 |
* @example
|
78 |
*
|
79 |
* // The example from `auto` can be rewritten as follows:
|
80 |
* async.autoInject({
|
81 |
* get_data: function(callback) {
|
82 |
* // async code to get some data
|
83 |
* callback(null, 'data', 'converted to array');
|
84 |
* },
|
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');
|
89 |
* },
|
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');
|
94 |
* },
|
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'});
|
99 |
* }
|
100 |
* }, function(err, results) {
|
101 |
* console.log('err = ', err);
|
102 |
* console.log('email_link = ', results.email_link);
|
103 |
* });
|
104 |
*
|
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.
|
110 |
*
|
111 |
* // This still has an advantage over plain `auto`, since the results a task
|
112 |
* // depends on are still spread into arguments.
|
113 |
* async.autoInject({
|
114 |
* //...
|
115 |
* write_file: ['get_data', 'make_folder', function(get_data, make_folder, callback) {
|
116 |
* callback(null, 'filename');
|
117 |
* }],
|
118 |
* email_link: ['write_file', function(write_file, callback) {
|
119 |
* callback(null, {'file':write_file, 'email':'user@example.com'});
|
120 |
* }]
|
121 |
* //...
|
122 |
* }, function(err, results) {
|
123 |
* console.log('err = ', err);
|
124 |
* console.log('email_link = ', results.email_link);
|
125 |
* });
|
126 |
*/
|
127 |
function autoInject(tasks, callback) { |
128 |
var newTasks = {};
|
129 |
|
130 |
(0, _baseForOwn2.default)(tasks, function (taskFn, key) { |
131 |
var params;
|
132 |
|
133 |
if ((0, _isArray2.default)(taskFn)) { |
134 |
params = taskFn.slice(0, -1); |
135 |
taskFn = taskFn[taskFn.length - 1];
|
136 |
|
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; |
141 |
} else {
|
142 |
params = parseParams(taskFn); |
143 |
if (taskFn.length === 0 && params.length === 0) { |
144 |
throw new Error("autoInject task functions require explicit parameters."); |
145 |
} |
146 |
|
147 |
params.pop(); |
148 |
|
149 |
newTasks[key] = params.concat(newTask); |
150 |
} |
151 |
|
152 |
function newTask(results, taskCb) { |
153 |
var newArgs = (0, _arrayMap2.default)(params, function (name) { |
154 |
return results[name];
|
155 |
}); |
156 |
newArgs.push(taskCb); |
157 |
taskFn.apply(null, newArgs);
|
158 |
} |
159 |
}); |
160 |
|
161 |
(0, _auto2.default)(newTasks, callback); |
162 |
} |
163 |
module.exports = exports['default'];
|