strongloop/loopback-boot — pull request #40 by shlomiassaf (2014-09-12 20:57:22+00:00)
Title: Add support for an afterBoot callback, when specified

Hi,

When executing the boot process, async boot modules can not notify when they are done.
Such notification is important in several situations.

For example, we want to register an API endpoint in a boot-module but the registration depends on an async operation (e.g: build a models from a database schema).
We must make sure we register the 'catch-all' route after our boot-module finish to register API endpoints.
Another example is to execute the loopback-explorer after at that point.

This pull implements a basic interface to handle such cases, it does that without changing the current interface and it is transparent to old configurations.

To include a final callback add a `afterBoot` property to config object that holds a ref to a function.
`boot(app, {appRootDir: __dirname, afterBoot: function() {console.log('done'); } } );`

By default, a boot-module does not suspend the queue.
For a boot-module to suspend the execution until a boot-module has finished, a boot-module must:

1) Hold a property called `waitForCallback` with the value true;
2) register a 2nd parameter in its signature, this is the callback parameter.
3) execute the callback when done, in all situations.

Example:
```
module.exports = waitableBootModule;

function waitableBootModule(server, doneCb) {
// wait 3 seconds, then continute.
setTimeout(function () {
if (doneCb && typeof doneCb == 'function'){
doneCb();
}
}, 3000);
}

waitableBootModule.waitForCallback = true;
```

It is important to note that `afterBoot` callback might execute before all boot-modules finish, this can happen when boot-modules without `waitForCallback` implements async logic.