Phase-control
Phase-control allows to split application initialization to phases. These phases will be executed sequentially (asynchronous phases will be run as synchronous).
Install
$ npm install phase-control
Usage
Phase-control is generally applicable to any Node.js application or application framework. Express will be used below.
Mixin Phase-control
Create a new application and mixin the phase-control module.
var express = phaseControl = ; var options = waitingTime: 5000 logEnable: falsevar app = ; //or var app = phaseControl(express()), in this way default options will be used.
Default options:
- waitingTime: 3000 - time in ms for asynchronous function execution.
- logEnable: true.
Once mixed-in, the application will have function app.phase(phaseName, path to file or path to directory or function)
.
Register Phases
An application proceeds through a sequence of phases, in order to prepare itself to handle requests. Modules need to be configured, databases need to be connected, and routes need to be drawn.
Phase-control packages phases for these common scenarios:
app; //will work like phase with path to file for all filesapp; //will require routes.js
routes.js
module { //app used like this this;}
or routes.js may be asynchronous. In this way it will be run by phase-control like synchronous and next phase will wait until this is over.
module { //callback name 'done' is mandatory //app used like this var self = this; ;}
Custom phases can be registered as well, and come in synchronous and asynchronous flavors:
app.phase('some phase', function() {
// synchronous phase
});
app.phase('some phase', function(done) { //callback name 'done' is mandatory
setTimeout(function() {
// asynchronous phase
done();
}, 1000);
});
This allows you to split off your initialization steps into separate, organized and reusable chunks of logic.
Tests
$ npm install
$ npm test