A dependency-based code runner for Application Composition Layers.
npm install app-compositorThe library is very minimal, with its only real dependency being when. For application modules which require asynchronous initialization, Promises/A+ are used.
app-compositor provides a dependency-based execution sequencing system. It will run the provided initialization functions in an order that is sufficient for all dependencies to be satisfied, or refuse to run the application whatsoever.
This is not an autoloader. It will not require your modules for you (as of now) nor pass them as arguments. It is the programmer's (or rather, the composition layer implementor's) duty to pass all required resources for an application to launch fully.
``js`
var mongodb = require('mongodb');
var http = require('http');
mongodb.MongoClient.connect('mongodb://127.0.0.1:49153/test', function initializeServer(error, database){
if(error){
console.error(error);
return;
}
var server = http.createServer(function serveCollectionView(request, response){
database.collection('movies').find().toArray(function printMovies(error, entries){
if(error){
response.writeHead(500, 'DB error');
response.end('Database error: ' + error.toString());
return;
}
response.writeHead(200, {
'Content-Type': 'application/json; encoding=utf-8'
});
response.end(JSON.stringify(entries));
});
});
server.listen('8888');
});
In case you'd like to run the example and need some data for the collection:
``
db.movies.insert({title: 'Pulp Function', slogan: 'You won\'t know the FUN til you\'ve seen the FUNCTION'});
1. Split the DB server connection and the HTTP server socket creation into separate functions
2. Make it so that their respective functions return the constructed resources via promises instead of callbacks where necessary
3. Define a dependency of the HTTP server upon a functioning MongoDB connection (in a real application, the HTTP server would probably be a framework such as Express and various middlewares/routes would be attached separately).
This is the same file, refactored into separated modules:
`js
var compositor = require('app-compositor');
var manager = new compositor.CompositionManager();
function database(){
var when = require('when');
this.is('database');
this.provides('database', function(dependency){
// The "dependency" getter function does not allow access to any resources in this module, because we have declared no dependencies using this.requires().
var mongodb = require('mongodb');
// The "provider" function should return a promise if it operates asynchronously. This promise will later be resolved with a working DB connection.
return when.promise(function connectToDB(resolve, reject){
mongodb.MongoClient.connect('mongodb://127.0.0.1:49153/test', function connected(error, database){
if(error){
reject(error);
return;
}
resolve(database);
});
});
});
}
function web(){
var when = require('when');
this.is('web');
this.requires('database');
this.provides('web', function(dependency){
var http = require('http');
var movieCollection = dependency('database').collection('movies');
var server = http.createServer(function serveCollectionView(request, response){
movieCollection.find().toArray(function printMovies(error, entries){
if(error){
response.writeHead(500, 'DB error');
response.end('Database error: ' + error.toString());
return;
}
response.writeHead(200, {
'Content-Type': 'application/json; encoding=utf-8'
});
response.end(JSON.stringify(entries));
});
});
// Since we don't initialize the server asynchronously, we can simply return it without using Promises.
server.listen(8888);
return server;
});
}
manager.runModules([database, web]).done(function applicationCompositionFinished(results){
console.log('Composition finished! The application is now running.');
}, function handleCompositionError(error){
console.error('Composition error:', error);
});
`
Notice how we could just cut database() and web() from this file and move them to their own CommonJS modules. Then, it's just a matter of calling require() and passing them into the composition layer together.
Note that, if a module defines multiple resource-providing functions (via multiple calls to this.provides()), then those functions will be called completely independently, perhaps at various times and in random order (so long as the order of dependencies is preserved).
it), running is aborted and a rejection is generated instead of fulfillment.
this.requires needs to be called for every resource that the programmer intends to use in resource creation functions via the dependency getter.dependency getter ##
The dependency getter is a function that is passed into the resource creation function as the sole argument. It has the following signature:
getDependency(dependencyName:string) : anythingThe resource creation function can obtain access to other resources (that the module has previously declared as needed via
this.requires`) by calling the dependency getter.Runs the specified set of modules in an order that ensures all dependencies are fulfilled for every module.
Returns a promise that:
* Fulfills with a key-value map of the generated resources (declared provided name => generated value) if composition succeeds and every module has been run
* Rejects with an app-compositor-specific error if some dependencies are determined to be insatisfiable when gathering dependency data (because of missing resources or a dependency cycle)
* Rejects with an app-compositor-specific error if a module tries to obtain an illegal (undeclared) dependency at resource generation time
* Rejects with the original error if a resource generation function throws an error or a promise returned by the resource generation function rejects