2017-06-11 00:29:00 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2017-06-23 04:19:34 +00:00
|
|
|
const paths = require('path');
|
|
|
|
const events = require('events');
|
|
|
|
const Log = require('./logger.js').log;
|
|
|
|
|
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
|
|
|
const async = require('async');
|
|
|
|
const glob = require('glob');
|
|
|
|
|
|
|
|
module.exports = new class Events extends events.EventEmitter {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
addListener(event, listener) {
|
|
|
|
Log.trace( { event : event }, 'Registering event listener');
|
|
|
|
return super.addListener(event, listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit(event, ...args) {
|
|
|
|
Log.trace( { event : event }, 'Emitting event');
|
|
|
|
return super.emit(event, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
on(event, listener) {
|
|
|
|
Log.trace( { event : event }, 'Registering event listener');
|
|
|
|
return super.on(event, listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
once(event, listener) {
|
|
|
|
Log.trace( { event : event }, 'Registering single use event listener');
|
|
|
|
return super.once(event, listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeListener(event, listener) {
|
|
|
|
Log.trace( { event : event }, 'Removing listener');
|
|
|
|
return super.removeListener(event, listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
startup(cb) {
|
|
|
|
async.each(require('./module_util.js').getModulePaths(), (modulePath, nextPath) => {
|
|
|
|
glob('*{.js,/*.js}', { cwd : modulePath }, (err, files) => {
|
|
|
|
if(err) {
|
|
|
|
return nextPath(err);
|
2017-06-12 02:44:34 +00:00
|
|
|
}
|
2017-06-23 04:19:34 +00:00
|
|
|
|
|
|
|
async.each(files, (moduleName, nextModule) => {
|
|
|
|
modulePath = paths.join(modulePath, moduleName);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const mod = require(modulePath);
|
|
|
|
|
|
|
|
if(_.isFunction(mod.registerEvents)) {
|
|
|
|
// :TODO: ... or just systemInit() / systemShutdown() & mods could call Events.on() / Events.removeListener() ?
|
|
|
|
mod.registerEvents(this);
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
|
2017-06-12 02:44:34 +00:00
|
|
|
}
|
2017-06-23 04:19:34 +00:00
|
|
|
|
|
|
|
return nextModule(null);
|
|
|
|
}, err => {
|
|
|
|
return nextPath(err);
|
|
|
|
});
|
2017-06-12 02:44:34 +00:00
|
|
|
});
|
2017-06-23 04:19:34 +00:00
|
|
|
}, err => {
|
|
|
|
return cb(err);
|
2017-06-11 00:29:00 +00:00
|
|
|
});
|
|
|
|
}
|
2017-06-23 04:19:34 +00:00
|
|
|
};
|