Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

autoInject.js 6.3KB

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