0.1.10 • Published 8 years ago

phase-control v0.1.10

Weekly downloads
29
License
-
Repository
-
Last release
8 years ago

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 = require('express')
  , phaseControl = require('phase-control');

var options = {
    waitingTime: 5000,
    logEnable: false
}
var app = phaseControl(express(), options); //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.phase('initialize','config/initializers'); //will work like phase with path to file for all files
app.phase('init routers','routes.js'); //will require routes.js

routes.js

module.exports = function() {
    //app used like this
    this.use('/admin', admin);
}

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.exports = function(done) { //callback name 'done' is mandatory
    //app used like this
    var self = this;

    setTimeout(function() {
        self.use('/admin', admin);
    }, 300);
}

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

License

The MIT License

0.1.10

8 years ago

0.1.9

10 years ago

0.1.8

10 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago