|
|
@ -11,17 +11,16 @@ function Executor () { |
|
|
|
|
|
|
|
|
|
|
|
// This queue will execute all commands, one-by-one in order
|
|
|
|
// This queue will execute all commands, one-by-one in order
|
|
|
|
this.queue = async.queue(function (task, cb) { |
|
|
|
this.queue = async.queue(function (task, cb) { |
|
|
|
var callback |
|
|
|
var newArguments = []; |
|
|
|
, lastArg = task.arguments[task.arguments.length - 1] |
|
|
|
|
|
|
|
, i, newArguments = [] |
|
|
|
|
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// task.arguments is an array-like object on which adding a new field doesn't work, so we transform it into a real array
|
|
|
|
// task.arguments is an array-like object on which adding a new field doesn't work, so we transform it into a real array
|
|
|
|
for (i = 0; i < task.arguments.length; i += 1) { newArguments.push(task.arguments[i]); } |
|
|
|
for (var i = 0; i < task.arguments.length; i += 1) { newArguments.push(task.arguments[i]); } |
|
|
|
|
|
|
|
var lastArg = task.arguments[task.arguments.length - 1]; |
|
|
|
|
|
|
|
|
|
|
|
// Always tell the queue task is complete. Execute callback if any was given.
|
|
|
|
// Always tell the queue task is complete. Execute callback if any was given.
|
|
|
|
if (typeof lastArg === 'function') { |
|
|
|
if (typeof lastArg === 'function') { |
|
|
|
callback = function () { |
|
|
|
// Callback was supplied
|
|
|
|
|
|
|
|
newArguments[newArguments.length - 1] = function () { |
|
|
|
if (typeof setImmediate === 'function') { |
|
|
|
if (typeof setImmediate === 'function') { |
|
|
|
setImmediate(cb); |
|
|
|
setImmediate(cb); |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -29,14 +28,12 @@ function Executor () { |
|
|
|
} |
|
|
|
} |
|
|
|
lastArg.apply(null, arguments); |
|
|
|
lastArg.apply(null, arguments); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
newArguments[newArguments.length - 1] = callback; |
|
|
|
|
|
|
|
} else if (!lastArg && task.arguments.length) { |
|
|
|
} else if (!lastArg && task.arguments.length) { |
|
|
|
callback = function () { cb(); }; |
|
|
|
// false/undefined/null supplied as callbback
|
|
|
|
newArguments[newArguments.length - 1] = callback; |
|
|
|
newArguments[newArguments.length - 1] = function () { cb(); }; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
callback = function () { cb(); }; |
|
|
|
// Nothing supplied as callback
|
|
|
|
newArguments.push(callback); |
|
|
|
newArguments.push(function () { cb(); }); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -51,7 +48,8 @@ function Executor () { |
|
|
|
* @param {Object} task |
|
|
|
* @param {Object} task |
|
|
|
* task.this - Object to use as this |
|
|
|
* task.this - Object to use as this |
|
|
|
* task.fn - Function to execute |
|
|
|
* task.fn - Function to execute |
|
|
|
* task.arguments - Array of arguments |
|
|
|
* task.arguments - Array of arguments, IMPORTANT: only the last argument may be a function (the callback) |
|
|
|
|
|
|
|
* and the last argument cannot be false/undefined/null |
|
|
|
* @param {Boolean} forceQueuing Optional (defaults to false) force executor to queue task even if it is not ready |
|
|
|
* @param {Boolean} forceQueuing Optional (defaults to false) force executor to queue task even if it is not ready |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
Executor.prototype.push = function (task, forceQueuing) { |
|
|
|
Executor.prototype.push = function (task, forceQueuing) { |
|
|
|