2017-12-10 21:51:33 +01:00
|
|
|
/*!
|
|
|
|
* async
|
|
|
|
* https://github.com/caolan/async
|
2017-04-20 03:09:25 +02:00
|
|
|
*
|
2017-12-10 21:51:33 +01:00
|
|
|
* Copyright 2010-2014 Caolan McMahon
|
|
|
|
* Released under the MIT license
|
2016-11-18 22:28:00 +01:00
|
|
|
*/
|
2017-12-10 21:51:33 +01:00
|
|
|
(function () {
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var async = {};
|
|
|
|
function noop() {}
|
|
|
|
function identity(v) {
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
function toBool(v) {
|
|
|
|
return !!v;
|
|
|
|
}
|
|
|
|
function notId(v) {
|
|
|
|
return !v;
|
2017-04-20 03:09:25 +02:00
|
|
|
}
|
2017-05-24 15:10:37 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
// global on the server, window in the browser
|
|
|
|
var previous_async;
|
2017-05-24 15:10:37 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
// Establish the root object, `window` (`self`) in the browser, `global`
|
|
|
|
// on the server, or `this` in some virtual machines. We use `self`
|
|
|
|
// instead of `window` for `WebWorker` support.
|
|
|
|
var root = typeof self === 'object' && self.self === self && self ||
|
|
|
|
typeof global === 'object' && global.global === global && global ||
|
|
|
|
this;
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
if (root != null) {
|
|
|
|
previous_async = root.async;
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.noConflict = function () {
|
|
|
|
root.async = previous_async;
|
|
|
|
return async;
|
2017-05-24 15:10:37 +02:00
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function only_once(fn) {
|
|
|
|
return function() {
|
|
|
|
if (fn === null) throw new Error("Callback was already called.");
|
|
|
|
fn.apply(this, arguments);
|
|
|
|
fn = null;
|
|
|
|
};
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _once(fn) {
|
|
|
|
return function() {
|
|
|
|
if (fn === null) return;
|
|
|
|
fn.apply(this, arguments);
|
|
|
|
fn = null;
|
|
|
|
};
|
|
|
|
}
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
//// cross-browser compatiblity functions ////
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var _toString = Object.prototype.toString;
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var _isArray = Array.isArray || function (obj) {
|
|
|
|
return _toString.call(obj) === '[object Array]';
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
// Ported from underscore.js isObject
|
|
|
|
var _isObject = function(obj) {
|
|
|
|
var type = typeof obj;
|
|
|
|
return type === 'function' || type === 'object' && !!obj;
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _isArrayLike(arr) {
|
|
|
|
return _isArray(arr) || (
|
|
|
|
// has a positive integer length property
|
|
|
|
typeof arr.length === "number" &&
|
|
|
|
arr.length >= 0 &&
|
|
|
|
arr.length % 1 === 0
|
|
|
|
);
|
|
|
|
}
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _arrayEach(arr, iterator) {
|
|
|
|
var index = -1,
|
|
|
|
length = arr.length;
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
while (++index < length) {
|
|
|
|
iterator(arr[index], index, arr);
|
|
|
|
}
|
2017-04-20 03:09:25 +02:00
|
|
|
}
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _map(arr, iterator) {
|
|
|
|
var index = -1,
|
|
|
|
length = arr.length,
|
|
|
|
result = Array(length);
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
while (++index < length) {
|
|
|
|
result[index] = iterator(arr[index], index, arr);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _range(count) {
|
|
|
|
return _map(Array(count), function (v, i) { return i; });
|
|
|
|
}
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _reduce(arr, iterator, memo) {
|
|
|
|
_arrayEach(arr, function (x, i, a) {
|
|
|
|
memo = iterator(memo, x, i, a);
|
|
|
|
});
|
|
|
|
return memo;
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _forEachOf(object, iterator) {
|
|
|
|
_arrayEach(_keys(object), function (key) {
|
|
|
|
iterator(object[key], key);
|
|
|
|
});
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _indexOf(arr, item) {
|
|
|
|
for (var i = 0; i < arr.length; i++) {
|
|
|
|
if (arr[i] === item) return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2017-04-20 03:09:25 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var _keys = Object.keys || function (obj) {
|
|
|
|
var keys = [];
|
|
|
|
for (var k in obj) {
|
|
|
|
if (obj.hasOwnProperty(k)) {
|
|
|
|
keys.push(k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return keys;
|
2016-11-18 22:28:00 +01:00
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _keyIterator(coll) {
|
|
|
|
var i = -1;
|
|
|
|
var len;
|
|
|
|
var keys;
|
|
|
|
if (_isArrayLike(coll)) {
|
|
|
|
len = coll.length;
|
|
|
|
return function next() {
|
|
|
|
i++;
|
|
|
|
return i < len ? i : null;
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
keys = _keys(coll);
|
|
|
|
len = keys.length;
|
|
|
|
return function next() {
|
|
|
|
i++;
|
|
|
|
return i < len ? keys[i] : null;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
// Similar to ES6's rest param (http://ariya.ofilabs.com/2013/03/es6-and-rest-parameter.html)
|
|
|
|
// This accumulates the arguments passed into an array, after a given index.
|
|
|
|
// From underscore.js (https://github.com/jashkenas/underscore/pull/2140).
|
|
|
|
function _restParam(func, startIndex) {
|
|
|
|
startIndex = startIndex == null ? func.length - 1 : +startIndex;
|
|
|
|
return function() {
|
|
|
|
var length = Math.max(arguments.length - startIndex, 0);
|
|
|
|
var rest = Array(length);
|
|
|
|
for (var index = 0; index < length; index++) {
|
|
|
|
rest[index] = arguments[index + startIndex];
|
|
|
|
}
|
|
|
|
switch (startIndex) {
|
|
|
|
case 0: return func.call(this, rest);
|
|
|
|
case 1: return func.call(this, arguments[0], rest);
|
|
|
|
}
|
|
|
|
// Currently unused but handle cases outside of the switch statement:
|
|
|
|
// var args = Array(startIndex + 1);
|
|
|
|
// for (index = 0; index < startIndex; index++) {
|
|
|
|
// args[index] = arguments[index];
|
|
|
|
// }
|
|
|
|
// args[startIndex] = rest;
|
|
|
|
// return func.apply(this, args);
|
|
|
|
};
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _withoutIndex(iterator) {
|
|
|
|
return function (value, index, callback) {
|
|
|
|
return iterator(value, callback);
|
|
|
|
};
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
//// exported async module functions ////
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
//// nextTick implementation with browser-compatible fallback ////
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
// capture the global reference to guard against fakeTimer mocks
|
|
|
|
var _setImmediate = typeof setImmediate === 'function' && setImmediate;
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var _delay = _setImmediate ? function(fn) {
|
|
|
|
// not a direct alias for IE10 compatibility
|
|
|
|
_setImmediate(fn);
|
|
|
|
} : function(fn) {
|
|
|
|
setTimeout(fn, 0);
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
if (typeof process === 'object' && typeof process.nextTick === 'function') {
|
|
|
|
async.nextTick = process.nextTick;
|
|
|
|
} else {
|
|
|
|
async.nextTick = _delay;
|
|
|
|
}
|
|
|
|
async.setImmediate = _setImmediate ? _delay : async.nextTick;
|
2016-11-03 01:33:53 +01:00
|
|
|
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.forEach =
|
|
|
|
async.each = function (arr, iterator, callback) {
|
|
|
|
return async.eachOf(arr, _withoutIndex(iterator), callback);
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.forEachSeries =
|
|
|
|
async.eachSeries = function (arr, iterator, callback) {
|
|
|
|
return async.eachOfSeries(arr, _withoutIndex(iterator), callback);
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.forEachLimit =
|
|
|
|
async.eachLimit = function (arr, limit, iterator, callback) {
|
|
|
|
return _eachOfLimit(limit)(arr, _withoutIndex(iterator), callback);
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.forEachOf =
|
|
|
|
async.eachOf = function (object, iterator, callback) {
|
|
|
|
callback = _once(callback || noop);
|
|
|
|
object = object || [];
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var iter = _keyIterator(object);
|
|
|
|
var key, completed = 0;
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
while ((key = iter()) != null) {
|
|
|
|
completed += 1;
|
|
|
|
iterator(object[key], key, only_once(done));
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
if (completed === 0) callback(null);
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function done(err) {
|
|
|
|
completed--;
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
// Check key is null in case iterator isn't exhausted
|
|
|
|
// and done resolved synchronously.
|
|
|
|
else if (key === null && completed <= 0) {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.forEachOfSeries =
|
|
|
|
async.eachOfSeries = function (obj, iterator, callback) {
|
|
|
|
callback = _once(callback || noop);
|
|
|
|
obj = obj || [];
|
|
|
|
var nextKey = _keyIterator(obj);
|
|
|
|
var key = nextKey();
|
|
|
|
function iterate() {
|
|
|
|
var sync = true;
|
|
|
|
if (key === null) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
iterator(obj[key], key, only_once(function (err) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
key = nextKey();
|
|
|
|
if (key === null) {
|
|
|
|
return callback(null);
|
|
|
|
} else {
|
|
|
|
if (sync) {
|
|
|
|
async.setImmediate(iterate);
|
|
|
|
} else {
|
|
|
|
iterate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
sync = false;
|
|
|
|
}
|
|
|
|
iterate();
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.forEachOfLimit =
|
|
|
|
async.eachOfLimit = function (obj, limit, iterator, callback) {
|
|
|
|
_eachOfLimit(limit)(obj, iterator, callback);
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _eachOfLimit(limit) {
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
return function (obj, iterator, callback) {
|
|
|
|
callback = _once(callback || noop);
|
|
|
|
obj = obj || [];
|
|
|
|
var nextKey = _keyIterator(obj);
|
|
|
|
if (limit <= 0) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
var done = false;
|
|
|
|
var running = 0;
|
|
|
|
var errored = false;
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
(function replenish () {
|
|
|
|
if (done && running <= 0) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
while (running < limit && !errored) {
|
|
|
|
var key = nextKey();
|
|
|
|
if (key === null) {
|
|
|
|
done = true;
|
|
|
|
if (running <= 0) {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
running += 1;
|
|
|
|
iterator(obj[key], key, only_once(function (err) {
|
|
|
|
running -= 1;
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
errored = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
replenish();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
};
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function doParallel(fn) {
|
|
|
|
return function (obj, iterator, callback) {
|
|
|
|
return fn(async.eachOf, obj, iterator, callback);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function doParallelLimit(fn) {
|
|
|
|
return function (obj, limit, iterator, callback) {
|
|
|
|
return fn(_eachOfLimit(limit), obj, iterator, callback);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function doSeries(fn) {
|
|
|
|
return function (obj, iterator, callback) {
|
|
|
|
return fn(async.eachOfSeries, obj, iterator, callback);
|
|
|
|
};
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _asyncMap(eachfn, arr, iterator, callback) {
|
|
|
|
callback = _once(callback || noop);
|
|
|
|
arr = arr || [];
|
|
|
|
var results = _isArrayLike(arr) ? [] : {};
|
|
|
|
eachfn(arr, function (value, index, callback) {
|
|
|
|
iterator(value, function (err, v) {
|
|
|
|
results[index] = v;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}, function (err) {
|
|
|
|
callback(err, results);
|
|
|
|
});
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.map = doParallel(_asyncMap);
|
|
|
|
async.mapSeries = doSeries(_asyncMap);
|
|
|
|
async.mapLimit = doParallelLimit(_asyncMap);
|
|
|
|
|
|
|
|
// reduce only has a series version, as doing reduce in parallel won't
|
|
|
|
// work in many situations.
|
|
|
|
async.inject =
|
|
|
|
async.foldl =
|
|
|
|
async.reduce = function (arr, memo, iterator, callback) {
|
|
|
|
async.eachOfSeries(arr, function (x, i, callback) {
|
|
|
|
iterator(memo, x, function (err, v) {
|
|
|
|
memo = v;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}, function (err) {
|
|
|
|
callback(err, memo);
|
|
|
|
});
|
|
|
|
};
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.foldr =
|
|
|
|
async.reduceRight = function (arr, memo, iterator, callback) {
|
|
|
|
var reversed = _map(arr, identity).reverse();
|
|
|
|
async.reduce(reversed, memo, iterator, callback);
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.transform = function (arr, memo, iterator, callback) {
|
|
|
|
if (arguments.length === 3) {
|
|
|
|
callback = iterator;
|
|
|
|
iterator = memo;
|
|
|
|
memo = _isArray(arr) ? [] : {};
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.eachOf(arr, function(v, k, cb) {
|
|
|
|
iterator(memo, v, k, cb);
|
|
|
|
}, function(err) {
|
|
|
|
callback(err, memo);
|
|
|
|
});
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _filter(eachfn, arr, iterator, callback) {
|
|
|
|
var results = [];
|
|
|
|
eachfn(arr, function (x, index, callback) {
|
|
|
|
iterator(x, function (v) {
|
|
|
|
if (v) {
|
|
|
|
results.push({index: index, value: x});
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}, function () {
|
|
|
|
callback(_map(results.sort(function (a, b) {
|
|
|
|
return a.index - b.index;
|
|
|
|
}), function (x) {
|
|
|
|
return x.value;
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.select =
|
|
|
|
async.filter = doParallel(_filter);
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.selectLimit =
|
|
|
|
async.filterLimit = doParallelLimit(_filter);
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.selectSeries =
|
|
|
|
async.filterSeries = doSeries(_filter);
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _reject(eachfn, arr, iterator, callback) {
|
|
|
|
_filter(eachfn, arr, function(value, cb) {
|
|
|
|
iterator(value, function(v) {
|
|
|
|
cb(!v);
|
|
|
|
});
|
|
|
|
}, callback);
|
2017-05-24 15:10:37 +02:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
async.reject = doParallel(_reject);
|
|
|
|
async.rejectLimit = doParallelLimit(_reject);
|
|
|
|
async.rejectSeries = doSeries(_reject);
|
|
|
|
|
|
|
|
function _createTester(eachfn, check, getResult) {
|
|
|
|
return function(arr, limit, iterator, cb) {
|
|
|
|
function done() {
|
|
|
|
if (cb) cb(getResult(false, void 0));
|
|
|
|
}
|
|
|
|
function iteratee(x, _, callback) {
|
|
|
|
if (!cb) return callback();
|
|
|
|
iterator(x, function (v) {
|
|
|
|
if (cb && check(v)) {
|
|
|
|
cb(getResult(true, x));
|
|
|
|
cb = iterator = false;
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (arguments.length > 3) {
|
|
|
|
eachfn(arr, limit, iteratee, done);
|
|
|
|
} else {
|
|
|
|
cb = iterator;
|
|
|
|
iterator = limit;
|
|
|
|
eachfn(arr, iteratee, done);
|
|
|
|
}
|
|
|
|
};
|
2017-05-24 15:10:37 +02:00
|
|
|
}
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.any =
|
|
|
|
async.some = _createTester(async.eachOf, toBool, identity);
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.someLimit = _createTester(async.eachOfLimit, toBool, identity);
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.all =
|
|
|
|
async.every = _createTester(async.eachOf, notId, notId);
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.everyLimit = _createTester(async.eachOfLimit, notId, notId);
|
|
|
|
|
|
|
|
function _findGetResult(v, x) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
async.detect = _createTester(async.eachOf, identity, _findGetResult);
|
|
|
|
async.detectSeries = _createTester(async.eachOfSeries, identity, _findGetResult);
|
|
|
|
async.detectLimit = _createTester(async.eachOfLimit, identity, _findGetResult);
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.sortBy = function (arr, iterator, callback) {
|
|
|
|
async.map(arr, function (x, callback) {
|
|
|
|
iterator(x, function (err, criteria) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
callback(null, {value: x, criteria: criteria});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, function (err, results) {
|
2016-11-18 22:28:00 +01:00
|
|
|
if (err) {
|
2017-12-10 21:51:33 +01:00
|
|
|
return callback(err);
|
2017-05-24 15:10:37 +02:00
|
|
|
}
|
|
|
|
else {
|
2017-12-10 21:51:33 +01:00
|
|
|
callback(null, _map(results.sort(comparator), function (x) {
|
|
|
|
return x.value;
|
|
|
|
}));
|
2016-11-18 22:28:00 +01:00
|
|
|
}
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
});
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function comparator(left, right) {
|
|
|
|
var a = left.criteria, b = right.criteria;
|
|
|
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
|
|
}
|
2016-11-18 22:28:00 +01:00
|
|
|
};
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.auto = function (tasks, concurrency, callback) {
|
|
|
|
if (typeof arguments[1] === 'function') {
|
|
|
|
// concurrency is optional, shift the args.
|
|
|
|
callback = concurrency;
|
|
|
|
concurrency = null;
|
|
|
|
}
|
|
|
|
callback = _once(callback || noop);
|
|
|
|
var keys = _keys(tasks);
|
|
|
|
var remainingTasks = keys.length;
|
|
|
|
if (!remainingTasks) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
if (!concurrency) {
|
|
|
|
concurrency = remainingTasks;
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var results = {};
|
|
|
|
var runningTasks = 0;
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var hasError = false;
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var listeners = [];
|
|
|
|
function addListener(fn) {
|
|
|
|
listeners.unshift(fn);
|
|
|
|
}
|
|
|
|
function removeListener(fn) {
|
|
|
|
var idx = _indexOf(listeners, fn);
|
|
|
|
if (idx >= 0) listeners.splice(idx, 1);
|
|
|
|
}
|
|
|
|
function taskComplete() {
|
|
|
|
remainingTasks--;
|
|
|
|
_arrayEach(listeners.slice(0), function (fn) {
|
|
|
|
fn();
|
|
|
|
});
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
addListener(function () {
|
|
|
|
if (!remainingTasks) {
|
|
|
|
callback(null, results);
|
|
|
|
}
|
|
|
|
});
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
_arrayEach(keys, function (k) {
|
|
|
|
if (hasError) return;
|
|
|
|
var task = _isArray(tasks[k]) ? tasks[k]: [tasks[k]];
|
|
|
|
var taskCallback = _restParam(function(err, args) {
|
|
|
|
runningTasks--;
|
|
|
|
if (args.length <= 1) {
|
|
|
|
args = args[0];
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
var safeResults = {};
|
|
|
|
_forEachOf(results, function(val, rkey) {
|
|
|
|
safeResults[rkey] = val;
|
|
|
|
});
|
|
|
|
safeResults[k] = args;
|
|
|
|
hasError = true;
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
callback(err, safeResults);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
results[k] = args;
|
|
|
|
async.setImmediate(taskComplete);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var requires = task.slice(0, task.length - 1);
|
|
|
|
// prevent dead-locks
|
|
|
|
var len = requires.length;
|
|
|
|
var dep;
|
|
|
|
while (len--) {
|
|
|
|
if (!(dep = tasks[requires[len]])) {
|
|
|
|
throw new Error('Has nonexistent dependency in ' + requires.join(', '));
|
|
|
|
}
|
|
|
|
if (_isArray(dep) && _indexOf(dep, k) >= 0) {
|
|
|
|
throw new Error('Has cyclic dependencies');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function ready() {
|
|
|
|
return runningTasks < concurrency && _reduce(requires, function (a, x) {
|
|
|
|
return (a && results.hasOwnProperty(x));
|
|
|
|
}, true) && !results.hasOwnProperty(k);
|
|
|
|
}
|
|
|
|
if (ready()) {
|
|
|
|
runningTasks++;
|
|
|
|
task[task.length - 1](taskCallback, results);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
addListener(listener);
|
|
|
|
}
|
|
|
|
function listener() {
|
|
|
|
if (ready()) {
|
|
|
|
runningTasks++;
|
|
|
|
removeListener(listener);
|
|
|
|
task[task.length - 1](taskCallback, results);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-11-18 22:28:00 +01:00
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.retry = function(times, task, callback) {
|
|
|
|
var DEFAULT_TIMES = 5;
|
|
|
|
var DEFAULT_INTERVAL = 0;
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var attempts = [];
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var opts = {
|
|
|
|
times: DEFAULT_TIMES,
|
|
|
|
interval: DEFAULT_INTERVAL
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function parseTimes(acc, t){
|
|
|
|
if(typeof t === 'number'){
|
|
|
|
acc.times = parseInt(t, 10) || DEFAULT_TIMES;
|
|
|
|
} else if(typeof t === 'object'){
|
|
|
|
acc.times = parseInt(t.times, 10) || DEFAULT_TIMES;
|
|
|
|
acc.interval = parseInt(t.interval, 10) || DEFAULT_INTERVAL;
|
|
|
|
} else {
|
|
|
|
throw new Error('Unsupported argument type for \'times\': ' + typeof t);
|
|
|
|
}
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var length = arguments.length;
|
|
|
|
if (length < 1 || length > 3) {
|
|
|
|
throw new Error('Invalid arguments - must be either (task), (task, callback), (times, task) or (times, task, callback)');
|
|
|
|
} else if (length <= 2 && typeof times === 'function') {
|
|
|
|
callback = task;
|
|
|
|
task = times;
|
|
|
|
}
|
|
|
|
if (typeof times !== 'function') {
|
|
|
|
parseTimes(opts, times);
|
|
|
|
}
|
|
|
|
opts.callback = callback;
|
|
|
|
opts.task = task;
|
|
|
|
|
|
|
|
function wrappedTask(wrappedCallback, wrappedResults) {
|
|
|
|
function retryAttempt(task, finalAttempt) {
|
|
|
|
return function(seriesCallback) {
|
|
|
|
task(function(err, result){
|
|
|
|
seriesCallback(!err || finalAttempt, {err: err, result: result});
|
|
|
|
}, wrappedResults);
|
|
|
|
};
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function retryInterval(interval){
|
|
|
|
return function(seriesCallback){
|
|
|
|
setTimeout(function(){
|
|
|
|
seriesCallback(null);
|
|
|
|
}, interval);
|
|
|
|
};
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
while (opts.times) {
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var finalAttempt = !(opts.times-=1);
|
|
|
|
attempts.push(retryAttempt(opts.task, finalAttempt));
|
|
|
|
if(!finalAttempt && opts.interval > 0){
|
|
|
|
attempts.push(retryInterval(opts.interval));
|
|
|
|
}
|
|
|
|
}
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.series(attempts, function(done, data){
|
|
|
|
data = data[data.length - 1];
|
|
|
|
(wrappedCallback || opts.callback)(data.err, data.result);
|
|
|
|
});
|
|
|
|
}
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
// If a callback is passed, run this as a controll flow
|
|
|
|
return opts.callback ? wrappedTask() : wrappedTask;
|
|
|
|
};
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.waterfall = function (tasks, callback) {
|
|
|
|
callback = _once(callback || noop);
|
|
|
|
if (!_isArray(tasks)) {
|
|
|
|
var err = new Error('First argument to waterfall must be an array of functions');
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
if (!tasks.length) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
function wrapIterator(iterator) {
|
|
|
|
return _restParam(function (err, args) {
|
|
|
|
if (err) {
|
|
|
|
callback.apply(null, [err].concat(args));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var next = iterator.next();
|
|
|
|
if (next) {
|
|
|
|
args.push(wrapIterator(next));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
args.push(callback);
|
|
|
|
}
|
|
|
|
ensureAsync(iterator).apply(null, args);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
wrapIterator(async.iterator(tasks))();
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _parallel(eachfn, tasks, callback) {
|
|
|
|
callback = callback || noop;
|
|
|
|
var results = _isArrayLike(tasks) ? [] : {};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
eachfn(tasks, function (task, key, callback) {
|
|
|
|
task(_restParam(function (err, args) {
|
|
|
|
if (args.length <= 1) {
|
|
|
|
args = args[0];
|
|
|
|
}
|
|
|
|
results[key] = args;
|
|
|
|
callback(err);
|
|
|
|
}));
|
|
|
|
}, function (err) {
|
|
|
|
callback(err, results);
|
|
|
|
});
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.parallel = function (tasks, callback) {
|
|
|
|
_parallel(async.eachOf, tasks, callback);
|
|
|
|
};
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.parallelLimit = function(tasks, limit, callback) {
|
|
|
|
_parallel(_eachOfLimit(limit), tasks, callback);
|
|
|
|
};
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.series = function(tasks, callback) {
|
|
|
|
_parallel(async.eachOfSeries, tasks, callback);
|
|
|
|
};
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.iterator = function (tasks) {
|
|
|
|
function makeCallback(index) {
|
|
|
|
function fn() {
|
|
|
|
if (tasks.length) {
|
|
|
|
tasks[index].apply(null, arguments);
|
|
|
|
}
|
|
|
|
return fn.next();
|
|
|
|
}
|
|
|
|
fn.next = function () {
|
|
|
|
return (index < tasks.length - 1) ? makeCallback(index + 1): null;
|
|
|
|
};
|
|
|
|
return fn;
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
return makeCallback(0);
|
|
|
|
};
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.apply = _restParam(function (fn, args) {
|
|
|
|
return _restParam(function (callArgs) {
|
|
|
|
return fn.apply(
|
|
|
|
null, args.concat(callArgs)
|
|
|
|
);
|
2016-10-10 03:43:44 +02:00
|
|
|
});
|
2016-11-03 01:33:53 +01:00
|
|
|
});
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _concat(eachfn, arr, fn, callback) {
|
|
|
|
var result = [];
|
|
|
|
eachfn(arr, function (x, index, cb) {
|
|
|
|
fn(x, function (err, y) {
|
|
|
|
result = result.concat(y || []);
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}, function (err) {
|
|
|
|
callback(err, result);
|
2016-11-03 01:33:53 +01:00
|
|
|
});
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
async.concat = doParallel(_concat);
|
|
|
|
async.concatSeries = doSeries(_concat);
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.whilst = function (test, iterator, callback) {
|
|
|
|
callback = callback || noop;
|
|
|
|
if (test()) {
|
|
|
|
var next = _restParam(function(err, args) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
} else if (test.apply(this, args)) {
|
|
|
|
iterator(next);
|
|
|
|
} else {
|
|
|
|
callback.apply(null, [null].concat(args));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
iterator(next);
|
|
|
|
} else {
|
|
|
|
callback(null);
|
2016-11-18 22:28:00 +01:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
};
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.doWhilst = function (iterator, test, callback) {
|
|
|
|
var calls = 0;
|
|
|
|
return async.whilst(function() {
|
|
|
|
return ++calls <= 1 || test.apply(this, arguments);
|
|
|
|
}, iterator, callback);
|
|
|
|
};
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.until = function (test, iterator, callback) {
|
|
|
|
return async.whilst(function() {
|
|
|
|
return !test.apply(this, arguments);
|
|
|
|
}, iterator, callback);
|
|
|
|
};
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.doUntil = function (iterator, test, callback) {
|
|
|
|
return async.doWhilst(iterator, function() {
|
|
|
|
return !test.apply(this, arguments);
|
|
|
|
}, callback);
|
|
|
|
};
|
2017-05-24 15:10:37 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.during = function (test, iterator, callback) {
|
|
|
|
callback = callback || noop;
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var next = _restParam(function(err, args) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
} else {
|
|
|
|
args.push(check);
|
|
|
|
test.apply(this, args);
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
var check = function(err, truth) {
|
2016-11-03 01:33:53 +01:00
|
|
|
if (err) {
|
2017-12-10 21:51:33 +01:00
|
|
|
callback(err);
|
|
|
|
} else if (truth) {
|
|
|
|
iterator(next);
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
test(check);
|
|
|
|
};
|
|
|
|
|
|
|
|
async.doDuring = function (iterator, test, callback) {
|
|
|
|
var calls = 0;
|
|
|
|
async.during(function(next) {
|
|
|
|
if (calls++ < 1) {
|
|
|
|
next(null, true);
|
2016-11-03 01:33:53 +01:00
|
|
|
} else {
|
2017-12-10 21:51:33 +01:00
|
|
|
test.apply(this, arguments);
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
}, iterator, callback);
|
|
|
|
};
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _queue(worker, concurrency, payload) {
|
|
|
|
if (concurrency == null) {
|
|
|
|
concurrency = 1;
|
2016-11-13 21:53:45 +01:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
else if(concurrency === 0) {
|
|
|
|
throw new Error('Concurrency must not be zero');
|
|
|
|
}
|
|
|
|
function _insert(q, data, pos, callback) {
|
|
|
|
if (callback != null && typeof callback !== "function") {
|
|
|
|
throw new Error("task callback must be a function");
|
|
|
|
}
|
|
|
|
q.started = true;
|
|
|
|
if (!_isArray(data)) {
|
|
|
|
data = [data];
|
|
|
|
}
|
|
|
|
if(data.length === 0 && q.idle()) {
|
|
|
|
// call drain immediately if there are no tasks
|
|
|
|
return async.setImmediate(function() {
|
|
|
|
q.drain();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_arrayEach(data, function(task) {
|
|
|
|
var item = {
|
|
|
|
data: task,
|
|
|
|
callback: callback || noop
|
|
|
|
};
|
|
|
|
|
|
|
|
if (pos) {
|
|
|
|
q.tasks.unshift(item);
|
|
|
|
} else {
|
|
|
|
q.tasks.push(item);
|
|
|
|
}
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
if (q.tasks.length === q.concurrency) {
|
|
|
|
q.saturated();
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
|
|
|
});
|
2017-12-10 21:51:33 +01:00
|
|
|
async.setImmediate(q.process);
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
function _next(q, tasks) {
|
|
|
|
return function(){
|
|
|
|
workers -= 1;
|
|
|
|
|
|
|
|
var removed = false;
|
|
|
|
var args = arguments;
|
|
|
|
_arrayEach(tasks, function (task) {
|
|
|
|
_arrayEach(workersList, function (worker, index) {
|
|
|
|
if (worker === task && !removed) {
|
|
|
|
workersList.splice(index, 1);
|
|
|
|
removed = true;
|
|
|
|
}
|
|
|
|
});
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
task.callback.apply(task, args);
|
|
|
|
});
|
|
|
|
if (q.tasks.length + workers === 0) {
|
|
|
|
q.drain();
|
|
|
|
}
|
|
|
|
q.process();
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var workers = 0;
|
|
|
|
var workersList = [];
|
|
|
|
var q = {
|
|
|
|
tasks: [],
|
|
|
|
concurrency: concurrency,
|
|
|
|
payload: payload,
|
|
|
|
saturated: noop,
|
|
|
|
empty: noop,
|
|
|
|
drain: noop,
|
|
|
|
started: false,
|
|
|
|
paused: false,
|
|
|
|
push: function (data, callback) {
|
|
|
|
_insert(q, data, false, callback);
|
|
|
|
},
|
|
|
|
kill: function () {
|
|
|
|
q.drain = noop;
|
|
|
|
q.tasks = [];
|
|
|
|
},
|
|
|
|
unshift: function (data, callback) {
|
|
|
|
_insert(q, data, true, callback);
|
|
|
|
},
|
|
|
|
process: function () {
|
|
|
|
while(!q.paused && workers < q.concurrency && q.tasks.length){
|
|
|
|
|
|
|
|
var tasks = q.payload ?
|
|
|
|
q.tasks.splice(0, q.payload) :
|
|
|
|
q.tasks.splice(0, q.tasks.length);
|
|
|
|
|
|
|
|
var data = _map(tasks, function (task) {
|
|
|
|
return task.data;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (q.tasks.length === 0) {
|
|
|
|
q.empty();
|
|
|
|
}
|
|
|
|
workers += 1;
|
|
|
|
workersList.push(tasks[0]);
|
|
|
|
var cb = only_once(_next(q, tasks));
|
|
|
|
worker(data, cb);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
length: function () {
|
|
|
|
return q.tasks.length;
|
|
|
|
},
|
|
|
|
running: function () {
|
|
|
|
return workers;
|
|
|
|
},
|
|
|
|
workersList: function () {
|
|
|
|
return workersList;
|
|
|
|
},
|
|
|
|
idle: function() {
|
|
|
|
return q.tasks.length + workers === 0;
|
|
|
|
},
|
|
|
|
pause: function () {
|
|
|
|
q.paused = true;
|
|
|
|
},
|
|
|
|
resume: function () {
|
|
|
|
if (q.paused === false) { return; }
|
|
|
|
q.paused = false;
|
|
|
|
var resumeCount = Math.min(q.concurrency, q.tasks.length);
|
|
|
|
// Need to call q.process once per concurrent
|
|
|
|
// worker to preserve full concurrency after pause
|
|
|
|
for (var w = 1; w <= resumeCount; w++) {
|
|
|
|
async.setImmediate(q.process);
|
|
|
|
}
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
};
|
|
|
|
return q;
|
2016-10-10 03:43:44 +02:00
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.queue = function (worker, concurrency) {
|
|
|
|
var q = _queue(function (items, cb) {
|
|
|
|
worker(items[0], cb);
|
|
|
|
}, concurrency, 1);
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
return q;
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.priorityQueue = function (worker, concurrency) {
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _compareTasks(a, b){
|
|
|
|
return a.priority - b.priority;
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _binarySearch(sequence, item, compare) {
|
|
|
|
var beg = -1,
|
|
|
|
end = sequence.length - 1;
|
|
|
|
while (beg < end) {
|
|
|
|
var mid = beg + ((end - beg + 1) >>> 1);
|
|
|
|
if (compare(item, sequence[mid]) >= 0) {
|
|
|
|
beg = mid;
|
|
|
|
} else {
|
|
|
|
end = mid - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return beg;
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _insert(q, data, priority, callback) {
|
|
|
|
if (callback != null && typeof callback !== "function") {
|
|
|
|
throw new Error("task callback must be a function");
|
|
|
|
}
|
|
|
|
q.started = true;
|
|
|
|
if (!_isArray(data)) {
|
|
|
|
data = [data];
|
|
|
|
}
|
|
|
|
if(data.length === 0) {
|
|
|
|
// call drain immediately if there are no tasks
|
|
|
|
return async.setImmediate(function() {
|
|
|
|
q.drain();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_arrayEach(data, function(task) {
|
|
|
|
var item = {
|
|
|
|
data: task,
|
|
|
|
priority: priority,
|
|
|
|
callback: typeof callback === 'function' ? callback : noop
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
q.tasks.splice(_binarySearch(q.tasks, item, _compareTasks) + 1, 0, item);
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
if (q.tasks.length === q.concurrency) {
|
|
|
|
q.saturated();
|
|
|
|
}
|
|
|
|
async.setImmediate(q.process);
|
|
|
|
});
|
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
// Start with a normal queue
|
|
|
|
var q = async.queue(worker, concurrency);
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
// Override push to accept second parameter representing priority
|
|
|
|
q.push = function (data, priority, callback) {
|
|
|
|
_insert(q, data, priority, callback);
|
|
|
|
};
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
// Remove unshift function
|
|
|
|
delete q.unshift;
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
return q;
|
|
|
|
};
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.cargo = function (worker, payload) {
|
|
|
|
return _queue(worker, 1, payload);
|
|
|
|
};
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _console_fn(name) {
|
|
|
|
return _restParam(function (fn, args) {
|
|
|
|
fn.apply(null, args.concat([_restParam(function (err, args) {
|
|
|
|
if (typeof console === 'object') {
|
|
|
|
if (err) {
|
|
|
|
if (console.error) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (console[name]) {
|
|
|
|
_arrayEach(args, function (x) {
|
|
|
|
console[name](x);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})]));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
async.log = _console_fn('log');
|
|
|
|
async.dir = _console_fn('dir');
|
|
|
|
/*async.info = _console_fn('info');
|
|
|
|
async.warn = _console_fn('warn');
|
|
|
|
async.error = _console_fn('error');*/
|
|
|
|
|
|
|
|
async.memoize = function (fn, hasher) {
|
|
|
|
var memo = {};
|
|
|
|
var queues = {};
|
|
|
|
var has = Object.prototype.hasOwnProperty;
|
|
|
|
hasher = hasher || identity;
|
|
|
|
var memoized = _restParam(function memoized(args) {
|
|
|
|
var callback = args.pop();
|
|
|
|
var key = hasher.apply(null, args);
|
|
|
|
if (has.call(memo, key)) {
|
|
|
|
async.setImmediate(function () {
|
|
|
|
callback.apply(null, memo[key]);
|
|
|
|
});
|
2016-11-18 22:28:00 +01:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
else if (has.call(queues, key)) {
|
|
|
|
queues[key].push(callback);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
queues[key] = [callback];
|
|
|
|
fn.apply(null, args.concat([_restParam(function (args) {
|
|
|
|
memo[key] = args;
|
|
|
|
var q = queues[key];
|
|
|
|
delete queues[key];
|
|
|
|
for (var i = 0, l = q.length; i < l; i++) {
|
|
|
|
q[i].apply(null, args);
|
|
|
|
}
|
|
|
|
})]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
memoized.memo = memo;
|
|
|
|
memoized.unmemoized = fn;
|
|
|
|
return memoized;
|
|
|
|
};
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.unmemoize = function (fn) {
|
|
|
|
return function () {
|
|
|
|
return (fn.unmemoized || fn).apply(null, arguments);
|
|
|
|
};
|
|
|
|
};
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _times(mapper) {
|
|
|
|
return function (count, iterator, callback) {
|
|
|
|
mapper(_range(count), iterator, callback);
|
|
|
|
};
|
2016-11-18 22:28:00 +01:00
|
|
|
}
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.times = _times(async.map);
|
|
|
|
async.timesSeries = _times(async.mapSeries);
|
|
|
|
async.timesLimit = function (count, limit, iterator, callback) {
|
|
|
|
return async.mapLimit(_range(count), limit, iterator, callback);
|
|
|
|
};
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.seq = function (/* functions... */) {
|
|
|
|
var fns = arguments;
|
|
|
|
return _restParam(function (args) {
|
|
|
|
var that = this;
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
var callback = args[args.length - 1];
|
|
|
|
if (typeof callback == 'function') {
|
|
|
|
args.pop();
|
2016-11-18 22:28:00 +01:00
|
|
|
} else {
|
2017-12-10 21:51:33 +01:00
|
|
|
callback = noop;
|
2016-11-18 22:28:00 +01:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.reduce(fns, args, function (newargs, fn, cb) {
|
|
|
|
fn.apply(that, newargs.concat([_restParam(function (err, nextargs) {
|
|
|
|
cb(err, nextargs);
|
|
|
|
})]));
|
|
|
|
},
|
|
|
|
function (err, results) {
|
|
|
|
callback.apply(that, [err].concat(results));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.compose = function (/* functions... */) {
|
|
|
|
return async.seq.apply(null, Array.prototype.reverse.call(arguments));
|
|
|
|
};
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2016-11-03 01:33:53 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function _applyEach(eachfn) {
|
|
|
|
return _restParam(function(fns, args) {
|
|
|
|
var go = _restParam(function(args) {
|
|
|
|
var that = this;
|
|
|
|
var callback = args.pop();
|
|
|
|
return eachfn(fns, function (fn, _, cb) {
|
|
|
|
fn.apply(that, args.concat([cb]));
|
|
|
|
},
|
|
|
|
callback);
|
|
|
|
});
|
|
|
|
if (args.length) {
|
|
|
|
return go.apply(this, args);
|
2017-04-20 03:09:25 +02:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
else {
|
|
|
|
return go;
|
2016-11-18 22:28:00 +01:00
|
|
|
}
|
|
|
|
});
|
2017-12-10 21:51:33 +01:00
|
|
|
}
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.applyEach = _applyEach(async.eachOf);
|
|
|
|
async.applyEachSeries = _applyEach(async.eachOfSeries);
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.forever = function (fn, callback) {
|
|
|
|
var done = only_once(callback || noop);
|
|
|
|
var task = ensureAsync(fn);
|
|
|
|
function next(err) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
2017-08-14 05:01:11 +02:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
task(next);
|
2017-08-14 05:01:11 +02:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
next();
|
2017-05-24 15:10:37 +02:00
|
|
|
};
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
function ensureAsync(fn) {
|
|
|
|
return _restParam(function (args) {
|
|
|
|
var callback = args.pop();
|
|
|
|
args.push(function () {
|
|
|
|
var innerArgs = arguments;
|
|
|
|
if (sync) {
|
|
|
|
async.setImmediate(function () {
|
|
|
|
callback.apply(null, innerArgs);
|
|
|
|
});
|
2016-11-13 21:53:45 +01:00
|
|
|
} else {
|
2017-12-10 21:51:33 +01:00
|
|
|
callback.apply(null, innerArgs);
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
2016-11-13 21:53:45 +01:00
|
|
|
});
|
2017-12-10 21:51:33 +01:00
|
|
|
var sync = true;
|
|
|
|
fn.apply(this, args);
|
|
|
|
sync = false;
|
2017-05-24 15:10:37 +02:00
|
|
|
});
|
2016-11-18 22:28:00 +01:00
|
|
|
}
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.ensureAsync = ensureAsync;
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.constant = _restParam(function(values) {
|
|
|
|
var args = [null].concat(values);
|
|
|
|
return function (callback) {
|
|
|
|
return callback.apply(this, args);
|
|
|
|
};
|
2017-04-20 03:09:25 +02:00
|
|
|
});
|
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
async.wrapSync =
|
|
|
|
async.asyncify = function asyncify(func) {
|
|
|
|
return _restParam(function (args) {
|
|
|
|
var callback = args.pop();
|
|
|
|
var result;
|
|
|
|
try {
|
|
|
|
result = func.apply(this, args);
|
|
|
|
} catch (e) {
|
|
|
|
return callback(e);
|
|
|
|
}
|
|
|
|
// if result is Promise object
|
|
|
|
if (_isObject(result) && typeof result.then === "function") {
|
|
|
|
result.then(function(value) {
|
|
|
|
callback(null, value);
|
|
|
|
})["catch"](function(err) {
|
|
|
|
callback(err.message ? err : new Error(err));
|
|
|
|
});
|
2016-11-18 22:28:00 +01:00
|
|
|
} else {
|
2017-12-10 21:51:33 +01:00
|
|
|
callback(null, result);
|
2016-11-18 22:28:00 +01:00
|
|
|
}
|
2016-11-03 01:33:53 +01:00
|
|
|
});
|
2017-12-10 21:51:33 +01:00
|
|
|
};
|
2016-11-18 22:28:00 +01:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
// Node.js
|
|
|
|
if (typeof module === 'object' && module.exports) {
|
|
|
|
module.exports = async;
|
2016-11-03 01:33:53 +01:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
// AMD / RequireJS
|
|
|
|
else if (typeof define === 'function' && define.amd) {
|
|
|
|
define([], function () {
|
|
|
|
return async;
|
2017-04-20 03:09:25 +02:00
|
|
|
});
|
2017-12-10 21:51:33 +01:00
|
|
|
}
|
|
|
|
// included directly via <script> tag
|
|
|
|
else {
|
|
|
|
root.async = async;
|
|
|
|
}
|
2017-04-20 03:09:25 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
}());
|