When working with a Thorin component or accessing another part of the application from within a component, it is important that you now how the core module will load them up.
The Thorin core module acts as a global singleton (placed under
global.thorin for convenience). Therefore,
whenever you launch your thorin app using node app.js --env=development
,
and require('thorin')
, it will perform the following steps
require('thorin')
, until the moment you call thorin.run()
, it
will register all the components that you add via addTransport, addStore, addPlugin,
addLibrary, addSanitizer or addErrorParser
thorin.run()
, it will start running all registered components and start the
application (see below).
Once you've called thorin.run()
, the initialization process begins and the following steps will be taken
thorin.loadPath()
callback(error, null)
. Note that this is a asynchronous
call.
callback(error, null)
.
This is the time where components should initialize any kind of external connection, perform binding or do any
kind of file altering, since the call is done asynchronously.
During the startup period of a Thorin app, on every step of the way and for each component, thorin will emit a specific event.
You can tap into these events by calling thorin.on({eventName}, {componentName} callback)
. The following events
are triggered:
init()
function of a given component.
setup()
call.
run()
call.
The above core events are available only in the boot period of a thorin app, and are triggered exactly once for each component. A short example can be seen below.
'use strict'
const thorin = require('thorin');
// Add the SQL store and the HTTP transport
thorin
.addStore(require('thorin-store-sql'))
.addTransport(require('thorin-transport-http'));
// Capture the init event for our SQL store.
thorin.on(thorin.EVENT.INIT, 'store.sql', (storeObj) => {
log.info(`SQL store initialized but not yet connected to store`);
});
// Capture the run event for our HTTP transport
thorin.on(thorin.EVENT.RUN, 'transport.http', (transportObj) => {
log.info(`HTTP web server is binded and accepting connections`);
});
// Capture the moment when thorin finished calling the init() function of ALL our registered components.
thorin.on(thorin.EVENT.INIT, () => {
log.info(`All components successfully initialized`);
});
thorin.run();
You can always create a new issue on GitHub or contact one of the core founders by chat.