Skip to content

Commit

Permalink
Merge pull request #818 from megawac/restParam
Browse files Browse the repository at this point in the history
Replace slice with restParam
  • Loading branch information
aearly committed Jul 1, 2015
2 parents 713cda9 + 8ed48ae commit c312a05
Showing 1 changed file with 74 additions and 85 deletions.
159 changes: 74 additions & 85 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,30 @@
}
}

function _baseSlice(arr, start) {
start = start || 0;
var index = -1;
var length = arr.length;

if (start) {
length -= start;
length = length < 0 ? 0 : length;
}
var result = Array(length);

while (++index < length) {
result[index] = arr[index + start];
}
return result;
// 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);
case 2: return func.call(this, arguments[0], arguments[1], 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);
};
}

function _withoutIndex(iterator) {
Expand Down Expand Up @@ -535,8 +544,7 @@

_arrayEach(keys, function (k) {
var task = _isArray(tasks[k]) ? tasks[k]: [tasks[k]];
function taskCallback(err) {
var args = _baseSlice(arguments, 1);
var taskCallback = _restParam(function(err, args) {
if (args.length <= 1) {
args = args[0];
}
Expand All @@ -552,7 +560,7 @@
results[k] = args;
async.setImmediate(taskComplete);
}
}
});
var requires = task.slice(0, Math.abs(task.length - 1)) || [];
// prevent dead-locks
var len = requires.length;
Expand Down Expand Up @@ -681,12 +689,11 @@
return callback();
}
function wrapIterator(iterator) {
return function (err) {
return _restParam(function (err, args) {
if (err) {
callback.apply(null, arguments);
callback.apply(null, [err].concat(args));
}
else {
var args = _baseSlice(arguments, 1);
var next = iterator.next();
if (next) {
args.push(wrapIterator(next));
Expand All @@ -696,7 +703,7 @@
}
ensureAsync(iterator).apply(null, args);
}
};
});
}
wrapIterator(async.iterator(tasks))();
};
Expand All @@ -706,14 +713,13 @@
var results = _isArrayLike(tasks) ? [] : {};

eachfn(tasks, function (task, key, callback) {
task(function (err) {
var args = _baseSlice(arguments, 1);
task(_restParam(function (err, args) {
if (args.length <= 1) {
args = args[0];
}
results[key] = args;
callback(err);
});
}));
}, function (err) {
callback(err, results);
});
Expand All @@ -732,14 +738,13 @@
var results = _isArrayLike(tasks) ? [] : {};

async.eachOfSeries(tasks, function (task, key, callback) {
task(function (err) {
var args = _baseSlice(arguments, 1);
task(_restParam(function (err, args) {
if (args.length <= 1) {
args = args[0];
}
results[key] = args;
callback(err);
});
}));
}, function (err) {
callback(err, results);
});
Expand All @@ -761,14 +766,13 @@
return makeCallback(0);
};

async.apply = function (fn) {
var args = _baseSlice(arguments, 1);
return function () {
async.apply = _restParam(function (fn, args) {
return _restParam(function (callArgs) {
return fn.apply(
null, args.concat(_baseSlice(arguments))
null, args.concat(callArgs)
);
};
};
});
});

function _concat(eachfn, arr, fn, callback) {
var result = [];
Expand Down Expand Up @@ -801,18 +805,17 @@

async.doWhilst = function (iterator, test, callback) {
callback = callback || noop;
iterator(function (err) {
iterator(_restParam(function (err, args) {
if (err) {
return callback(err);
}
var args = _baseSlice(arguments, 1);
if (test.apply(null, args)) {
async.doWhilst(iterator, test, callback);
}
else {
callback(null);
}
});
}));
};

async.until = function (test, iterator, callback) {
Expand All @@ -832,18 +835,17 @@

async.doUntil = function (iterator, test, callback) {
callback = callback || noop;
iterator(function (err) {
iterator(_restParam(function (err, args) {
if (err) {
return callback(err);
}
var args = _baseSlice(arguments, 1);
if (!test.apply(null, args)) {
async.doUntil(iterator, test, callback);
}
else {
callback(null);
}
});
}));
};

async.during = function (test, iterator, callback) {
Expand All @@ -868,11 +870,10 @@

async.doDuring = function (iterator, test, callback) {
callback = callback || noop;
iterator(function (err) {
iterator(_restParam(function (err, args) {
if (err) {
return callback(err);
}
var args = _baseSlice(arguments, 1);
args.push(function (err, truth) {
if (err) {
return callback(err);
Expand All @@ -885,7 +886,7 @@
}
});
test.apply(null, args);
});
}));
};

function _queue(worker, concurrency, payload) {
Expand Down Expand Up @@ -1084,10 +1085,8 @@
};

function _console_fn(name) {
return function (fn) {
var args = _baseSlice(arguments, 1);
fn.apply(null, args.concat([function (err) {
var args = _baseSlice(arguments, 1);
return _restParam(function (fn, args) {
fn.apply(null, args.concat([_restParam(function (err, args) {
if (typeof console !== 'undefined') {
if (err) {
if (console.error) {
Expand All @@ -1100,8 +1099,8 @@
});
}
}
}]));
};
})]));
});
}
async.log = _console_fn('log');
async.dir = _console_fn('dir');
Expand All @@ -1115,8 +1114,7 @@
hasher = hasher || function (x) {
return x;
};
function memoized() {
var args = _baseSlice(arguments);
var memoized = _restParam(function memoized(args) {
var callback = args.pop();
var key = hasher.apply(null, args);
if (key in memo) {
Expand All @@ -1129,16 +1127,16 @@
}
else {
queues[key] = [callback];
fn.apply(null, args.concat([function () {
memo[key] = _baseSlice(arguments);
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, arguments);
q[i].apply(null, args);
}
}]));
})]));
}
}
});
memoized.memo = memo;
memoized.unmemoized = fn;
return memoized;
Expand All @@ -1164,9 +1162,8 @@

async.seq = function (/* functions... */) {
var fns = arguments;
return function () {
return _restParam(function (args) {
var that = this;
var args = _baseSlice(arguments);

var callback = args.slice(-1)[0];
if (typeof callback == 'function') {
Expand All @@ -1176,50 +1173,44 @@
}

async.reduce(fns, args, function (newargs, fn, cb) {
fn.apply(that, newargs.concat([function () {
var err = arguments[0];
var nextargs = _baseSlice(arguments, 1);
fn.apply(that, newargs.concat([_restParam(function (err, nextargs) {
cb(err, nextargs);
}]));
})]));
},
function (err, results) {
callback.apply(that, [err].concat(results));
});
};
});
};

async.compose = function (/* functions... */) {
return async.seq.apply(null, Array.prototype.reverse.call(arguments));
};


function _applyEach(eachfn, fns /*args...*/) {
function go() {
var _applyEach = _restParam(function _applyEach(eachfn, fns, args) {
var go = _restParam(function(args) {
var that = this;
var args = _baseSlice(arguments);
var callback = args.pop();
return eachfn(fns, function (fn, _, cb) {
fn.apply(that, args.concat([cb]));
},
callback);
}
if (arguments.length > 2) {
var args = _baseSlice(arguments, 2);
});
if (args.length) {
return go.apply(this, args);
}
else {
return go;
}
}
});

async.applyEach = function (/*fns, args...*/) {
var args = _baseSlice(arguments);
async.applyEach = _restParam(function (args) {
return _applyEach.apply(null, [async.eachOf].concat(args));
};
async.applyEachSeries = function (/*fns, args...*/) {
var args = _baseSlice(arguments);
});
async.applyEachSeries = _restParam(function (args) {
return _applyEach.apply(null, [async.eachOfSeries].concat(args));
};
});


async.forever = function (fn, callback) {
Expand All @@ -1235,8 +1226,7 @@
};

function ensureAsync(fn) {
return function (/*...args, callback*/) {
var args = _baseSlice(arguments);
return _restParam(function (args) {
var callback = args.pop();
args.push(function () {
var innerArgs = arguments;
Expand All @@ -1251,22 +1241,21 @@
var sync = true;
fn.apply(this, args);
sync = false;
};
});
}

async.ensureAsync = ensureAsync;

async.constant = function constant(/*values...*/) {
var args = [null].concat(_baseSlice(arguments));
async.constant = _restParam(function(values) {
var args = [null].concat(values);
return function (callback) {
return callback.apply(this, args);
};
};
});

async.wrapSync =
async.asyncify = function asyncify(func) {
return function (/*args..., callback*/) {
var args = _baseSlice(arguments);
return _restParam(function (args) {
var callback = args.pop();
var result;
try {
Expand All @@ -1275,7 +1264,7 @@
return callback(e);
}
callback(null, result);
};
});
};

// Node.js
Expand Down

0 comments on commit c312a05

Please sign in to comment.