#Why?
This is yet another try to solve callback hell and 500+ LOC files. Split your codebase into small easy to test and maintain modules and feed it to Broom. It will run your code according
to each module dependencies. Broom will handle parallel and sequental execution of your modules where it needed.
#How can I install it?
If you use node 0.6.x
npm install broom@0.0.7
If you use node 0.8.x
npm install broom
#How can I use it?
assume you have directory structure like this:
yourapp.js
var Broom = require('broom').broom;
var flow = new Broom({
'fileName': 'index.js',
'dependenciesName': 'deps',
'entryName': 'onStart'
});
flow.setRootPath(__dirname);
flow.scan('./app/modules', function (err, done) {
if (!done) {
console.error('broom scan dirs completed with errors');
console.dir(err);
process.exit(1);
}
});
flow.setRootArgs({'request': request, 'models': models});
flow.run(function (err, data) {
data['/first'];
});
###module example
first/index.js
var MyFirstModule = function () {
this.deps = {
'main': '/',
'sec': '/second',
'third': '/third'
};
this.onStart = this.entryPoint.bind(this);
};
MyFirstModule.prototype.entryPoint = function (callback, data) {
callback(null, {});
};
module.exports = MyFirstModule;
second/index.js
var MySecondModule = function () {
this.deps = {
'main': '/'
};
this.onStart = this.entryPoint.bind(this);
};
MySecondModule.prototype.entryPoint = function (callback, data) {
setTimeout(function () {
callback(null, {});
}, 1000);
};
module.exports = MySecondModule;
third/index.js
var MyThirdModule = function () {
this.deps = {
'main': '/'
};
this.onStart = this.entryPoint.bind(this);
};
MyThirdModule.prototype.entryPoint = function (callback, data) {
callback(null, {});
};
module.exports = MyThirdModule;
Modules second and third will execute in parallel, and than first module will executed with results of second and third modules, after that final callback will be called with all results.
If any module pass error as it first parameter - Broom will stop and call final callback(passed to run function) with error
##Common tasks and code reusing
Common tasks like validating user etc. can be extracted to standalone broom module and augment any flow with mainFlow.extend(secondaryFlow);
.
Do not forget to run Broom.testTree after augmentation.
#catching errors API
###Broom.testTree()
validate all dependencies in execution tree, it will output to console all unresolved, circular dependencies and when module depends on itself situations.
###Broom.debug
if set to true
, Broom will look for functions that executes more than Broom.timeLimit mseconds and output into console it's name and arguments.
####Broom.beforeEach
Array with functions that called before every module, all that function returns will passed to Broom.afterEach
functions
####Broom.afterEach
Array with functions that called after every module with results that returned from beforeEach functions.
####beforeEach and afterEach example
var flow = new Broom();
flow.debug = true;
flow.beforeEach.push(function (pathToModule, moduleArguments) {
return Date.now();
});
flow.afterEach.push(function (pathToModule, beforeEachArgs) {
console.log(pathToModule + ' executes ' + (Date.now() - beforeEachArgs[0]) + ' ms');
});
####Broom.timeLimit
max execution time in milliseconds for single function.
#Licence
MIT