Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

autoInject.js 6.1KB

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