123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- 'use strict';
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
-
- exports.default = function (tasks, concurrency, callback) {
- if (typeof concurrency === 'function') {
-
- callback = concurrency;
- concurrency = null;
- }
- callback = (0, _once2.default)(callback || _noop2.default);
- var keys = (0, _keys2.default)(tasks);
- var numTasks = keys.length;
- if (!numTasks) {
- return callback(null);
- }
- if (!concurrency) {
- concurrency = numTasks;
- }
-
- var results = {};
- var runningTasks = 0;
- var hasError = false;
-
- var listeners = Object.create(null);
-
- var readyTasks = [];
-
-
- var readyToCheck = [];
-
- var uncheckedDependencies = {};
-
- (0, _baseForOwn2.default)(tasks, function (task, key) {
- if (!(0, _isArray2.default)(task)) {
-
- enqueueTask(key, [task]);
- readyToCheck.push(key);
- return;
- }
-
- var dependencies = task.slice(0, task.length - 1);
- var remainingDependencies = dependencies.length;
- if (remainingDependencies === 0) {
- enqueueTask(key, task);
- readyToCheck.push(key);
- return;
- }
- uncheckedDependencies[key] = remainingDependencies;
-
- (0, _arrayEach2.default)(dependencies, function (dependencyName) {
- if (!tasks[dependencyName]) {
- throw new Error('async.auto task `' + key + '` has a non-existent dependency `' + dependencyName + '` in ' + dependencies.join(', '));
- }
- addListener(dependencyName, function () {
- remainingDependencies--;
- if (remainingDependencies === 0) {
- enqueueTask(key, task);
- }
- });
- });
- });
-
- checkForDeadlocks();
- processQueue();
-
- function enqueueTask(key, task) {
- readyTasks.push(function () {
- runTask(key, task);
- });
- }
-
- function processQueue() {
- if (readyTasks.length === 0 && runningTasks === 0) {
- return callback(null, results);
- }
- while (readyTasks.length && runningTasks < concurrency) {
- var run = readyTasks.shift();
- run();
- }
- }
-
- function addListener(taskName, fn) {
- var taskListeners = listeners[taskName];
- if (!taskListeners) {
- taskListeners = listeners[taskName] = [];
- }
-
- taskListeners.push(fn);
- }
-
- function taskComplete(taskName) {
- var taskListeners = listeners[taskName] || [];
- (0, _arrayEach2.default)(taskListeners, function (fn) {
- fn();
- });
- processQueue();
- }
-
- function runTask(key, task) {
- if (hasError) return;
-
- var taskCallback = (0, _onlyOnce2.default)(function (err, result) {
- runningTasks--;
- if (arguments.length > 2) {
- result = (0, _slice2.default)(arguments, 1);
- }
- if (err) {
- var safeResults = {};
- (0, _baseForOwn2.default)(results, function (val, rkey) {
- safeResults[rkey] = val;
- });
- safeResults[key] = result;
- hasError = true;
- listeners = Object.create(null);
-
- callback(err, safeResults);
- } else {
- results[key] = result;
- taskComplete(key);
- }
- });
-
- runningTasks++;
- var taskFn = (0, _wrapAsync2.default)(task[task.length - 1]);
- if (task.length > 1) {
- taskFn(results, taskCallback);
- } else {
- taskFn(taskCallback);
- }
- }
-
- function checkForDeadlocks() {
-
-
-
- var currentTask;
- var counter = 0;
- while (readyToCheck.length) {
- currentTask = readyToCheck.pop();
- counter++;
- (0, _arrayEach2.default)(getDependents(currentTask), function (dependent) {
- if (--uncheckedDependencies[dependent] === 0) {
- readyToCheck.push(dependent);
- }
- });
- }
-
- if (counter !== numTasks) {
- throw new Error('async.auto cannot execute tasks due to a recursive dependency');
- }
- }
-
- function getDependents(taskName) {
- var result = [];
- (0, _baseForOwn2.default)(tasks, function (task, key) {
- if ((0, _isArray2.default)(task) && (0, _baseIndexOf2.default)(task, taskName, 0) >= 0) {
- result.push(key);
- }
- });
- return result;
- }
- };
-
- var _arrayEach = require('lodash/_arrayEach');
-
- var _arrayEach2 = _interopRequireDefault(_arrayEach);
-
- var _baseForOwn = require('lodash/_baseForOwn');
-
- var _baseForOwn2 = _interopRequireDefault(_baseForOwn);
-
- var _baseIndexOf = require('lodash/_baseIndexOf');
-
- var _baseIndexOf2 = _interopRequireDefault(_baseIndexOf);
-
- var _isArray = require('lodash/isArray');
-
- var _isArray2 = _interopRequireDefault(_isArray);
-
- var _keys = require('lodash/keys');
-
- var _keys2 = _interopRequireDefault(_keys);
-
- var _noop = require('lodash/noop');
-
- var _noop2 = _interopRequireDefault(_noop);
-
- var _slice = require('./internal/slice');
-
- var _slice2 = _interopRequireDefault(_slice);
-
- var _once = require('./internal/once');
-
- var _once2 = _interopRequireDefault(_once);
-
- var _onlyOnce = require('./internal/onlyOnce');
-
- var _onlyOnce2 = _interopRequireDefault(_onlyOnce);
-
- var _wrapAsync = require('./internal/wrapAsync');
-
- var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
- module.exports = exports['default'];
-
|