Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/release-2.0.1'
- Loading branch information
Showing
5 changed files
with
502 additions
and
366 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,121 +1,151 @@ | ||
function FnQueue(tasks, callback, concurrecyLevel, stop) { | ||
this.tasks = tasks; | ||
this.callback = callback; | ||
this.callSequence = []; | ||
this.results = {}; | ||
this.gotError = false; | ||
this.concurrecyLevel = concurrecyLevel || 'auto'; | ||
this.runningNb = 0; | ||
!stop && this.callNextFunction(); | ||
var | ||
introspect = require('introspect'); | ||
|
||
function FnQueue(tasks, callback, concurrecyLevel, isStopped) { | ||
this.tasks = tasks; | ||
this.taskDependencies = {}; | ||
this.callback = callback; | ||
this.callSequence = []; | ||
this.results = {}; | ||
this.gotError = false; | ||
this.concurrecyLevel = concurrecyLevel || 'auto'; | ||
this.runningNb = 0; | ||
this.loadDependencies(); | ||
|
||
if (typeof this.concurrecyLevel !== 'string' && typeof this.concurrecyLevel !== 'number') { | ||
this.onTaskComplete(null, new Error('Invalid concurrecyLevel: ' + typeof this.concurrecyLevel + ', must be a Number or \'auto\'')); | ||
return; | ||
} else if (typeof this.concurrecyLevel === 'number' && this.concurrecyLevel < 1) { | ||
this.onTaskComplete(null, new Error('Invalid concurrecyLevel: ' + this.concurrecyLevel + ' < 1')); | ||
return; | ||
} else if (typeof this.concurrecyLevel === 'string' && this.concurrecyLevel !== 'auto') { | ||
this.onTaskComplete(null, new Error('Invalid concurrecyLevel: ' + this.concurrecyLevel + ' < 1')); | ||
return; | ||
} | ||
|
||
!isStopped && this.callNextFunction(); | ||
} | ||
|
||
FnQueue.verbose = function () { | ||
FnQueue.prototype.isVerbose = true; | ||
return FnQueue; | ||
FnQueue.prototype.isVerbose = true; | ||
return FnQueue; | ||
}; | ||
|
||
FnQueue.prototype.isVerbose = false; | ||
|
||
FnQueue.prototype.loadDependencies = function() { | ||
|
||
for(var taskName in this.tasks) { | ||
|
||
if (typeof this.tasks[taskName] !== 'function') { | ||
this.onTaskComplete(null, new Error('Invalid Function in list: ' + taskName + ' is not a function')); | ||
return; | ||
} | ||
|
||
if (this.tasks[taskName].length === 0) { | ||
this.onTaskComplete(null, new Error('Invalid Function in list: missing callback parameter in ' + taskName)); | ||
return; | ||
} | ||
|
||
this.taskDependencies[taskName] = introspect(this.tasks[taskName]).slice(0, -1); | ||
} | ||
}; | ||
|
||
FnQueue.prototype.onTaskComplete = function (taskName, err, result) { | ||
|
||
if (this.gotError) | ||
return; | ||
if (this.gotError) { | ||
return; | ||
} | ||
|
||
this.runningNb--; | ||
this.runningNb--; | ||
|
||
if (err) { | ||
this.gotError = true; | ||
this.callback && this.callback(err, this.getResults()); | ||
return; | ||
} | ||
if (err) { | ||
this.gotError = true; | ||
this.callback && this.callback(err, this.getResults()); | ||
return; | ||
} | ||
|
||
this.results[taskName] = { result: result }; | ||
this.callNextFunction(); | ||
this.results[taskName] = { result: result }; | ||
this.callNextFunction(); | ||
}; | ||
|
||
FnQueue.prototype.getResults = function () { | ||
var results = {}; | ||
for(var taskName in this.results) | ||
results[taskName] = this.results[taskName].result; | ||
return results; | ||
var results = {}; | ||
for(var taskName in this.results) { | ||
results[taskName] = this.results[taskName].result; | ||
} | ||
return results; | ||
}; | ||
|
||
FnQueue.prototype.callNextFunction = function () { | ||
|
||
var nextFunction, dependencies, finish = true, args; | ||
|
||
for(var taskName in this.tasks) { | ||
|
||
finish = false; | ||
var nextFunction, dependencies, finish = true, args; | ||
|
||
if (this.concurrecyLevel != 'auto' && this.runningNb >= this.concurrecyLevel) | ||
return; | ||
for(var taskName in this.tasks) { | ||
|
||
nextFunction = this.tasks[taskName]; | ||
finish = false; | ||
|
||
if (nextFunction.length === 0) { | ||
this.onTaskComplete(null, new Error('Invalid Function in chain: missing callback parameter')); | ||
return; | ||
} | ||
if (this.concurrecyLevel != 'auto' && this.runningNb >= this.concurrecyLevel) { | ||
return; | ||
} | ||
|
||
dependencies = this.getFunctionArguments(nextFunction).slice(0, -1); | ||
args = this.getDependencies(dependencies); | ||
nextFunction = this.tasks[taskName]; | ||
dependencies = this.taskDependencies[taskName]; | ||
args = this.getDependencies(dependencies); | ||
|
||
if (args) { | ||
args.push(this.onTaskComplete.bind(this, taskName)); | ||
if (args) { | ||
args.push(this.onTaskComplete.bind(this, taskName)); | ||
|
||
this.isVerbose && console.log(' - FnQueue: executing: ' + taskName + '(' + dependencies.join(',') + ') { ... }'); | ||
this.isVerbose && console.log(' - FnQueue: executing: ' + taskName + '(' + dependencies.join(',') + ') { ... }'); | ||
|
||
this.runningNb++; | ||
delete this.tasks[taskName]; | ||
this.callSequence.push(taskName); | ||
nextFunction.apply(null, args); | ||
this.runningNb++; | ||
delete this.tasks[taskName]; | ||
this.callSequence.push(taskName); | ||
nextFunction.apply(null, args); | ||
|
||
if (this.concurrecyLevel != 'auto' && this.runningNb >= this.concurrecyLevel) | ||
return; | ||
} | ||
} | ||
if (this.concurrecyLevel != 'auto' && this.runningNb >= this.concurrecyLevel) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
if (!finish && this.runningNb === 0) { | ||
this.onTaskComplete(null, new Error('Unresolvable dependencies: function "' + taskName + '" requires [' + dependencies.join(',') + ']')); | ||
return; | ||
} | ||
if (!finish && this.runningNb === 0) { | ||
this.onTaskComplete(null, new Error('Unresolvable dependencies: function "' + taskName + '" requires [' + dependencies.join(',') + ']')); | ||
return; | ||
} | ||
|
||
if (finish && !this.runningNb && this.callback) { | ||
this.callback(null, this.getResults()); | ||
this.destroy(); | ||
} | ||
if (finish && !this.runningNb && this.callback) { | ||
this.callback(null, this.getResults()); | ||
this.destroy(); | ||
} | ||
}; | ||
|
||
FnQueue.prototype.start = FnQueue.prototype.callNextFunction; | ||
|
||
FnQueue.prototype.getFunctionArguments = function (fn) { | ||
return (/^function.+\(([a-z0-9\n\r\t ,]*)\)/i).exec(fn.toString())[1].trim().split(/[ ,\n\r\t]+/); | ||
}; | ||
|
||
FnQueue.prototype.getDependencies = function (dependencies) { | ||
|
||
var args = []; | ||
var args = []; | ||
|
||
for(var i = 0; i < dependencies.length; i++) { | ||
for(var i = 0; i < dependencies.length; i++) { | ||
|
||
if (this.results[dependencies[i]] === undefined) { | ||
return false; | ||
} | ||
if (this.results[dependencies[i]] === undefined) { | ||
return false; | ||
} | ||
|
||
var arg = this.results[dependencies[i]]; | ||
var arg = this.results[dependencies[i]]; | ||
|
||
if (arg !== undefined) | ||
args.push(arg.result); | ||
} | ||
if (arg !== undefined) { | ||
args.push(arg.result); | ||
} | ||
} | ||
|
||
return args; | ||
return args; | ||
}; | ||
|
||
FnQueue.prototype.destroy = function () { | ||
delete this.results; | ||
delete this.callback; | ||
delete this.tasks; | ||
delete this.results; | ||
delete this.callback; | ||
delete this.tasks; | ||
}; | ||
|
||
module.exports = FnQueue; |
Oops, something went wrong.