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:
- app
- modules
- first
- index.js
- second
- index.js
- third
- index.js
- first
- modules
yourapp.js
var Broom = broom; var flow = 'fileName': 'index.js' //Broom will hunt for only for files with this name, so you can put any other files in brooms module directory. 'dependenciesName': 'deps' //Broom will check this property for module dependencies 'entryName': 'onStart' //What function Broom should run ; flow; //pointing to modules root directory flow; //then set root arguments, you can reach them with {'main': '/'} in deps property of your modules flow; //assume that we handling http request //run it flow;
module example
first/index.js
var { thisdeps = 'main': '/' 'sec': '/second' 'third': '/third' ; thisonStart = thisentryPoint;};MyFirstModuleprototype { //here you can access results of second/index.js with data.sec ;};moduleexports = MyFirstModule;
second/index.js
var { thisdeps = 'main': '/' ; thisonStart = thisentryPoint;};MySecondModuleprototype { //here you can access root args with data.main ; };moduleexports = MySecondModule;
third/index.js
var { thisdeps = 'main': '/' ; thisonStart = thisentryPoint;};MyThirdModuleprototype { //here you can access root args with data.main ;};moduleexports = 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 = ; flowdebug = true; flowbeforeEach; flowafterEach;
Broom.timeLimit
max execution time in milliseconds for single function.
Licence
MIT