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) => {
|
2018-01-15 16:41:18 +00:00
|
|
|
const fullModulePath = paths.join(modulePath, moduleName);
|
2017-06-23 04:19:34 +00:00
|
|
|
|
|
|
|
try {
|
2018-01-15 16:41:18 +00:00
|
|
|
const mod = require(fullModulePath);
|
|
|
|
|
2017-06-23 04:19:34 +00:00
|
|
|
if(_.isFunction(mod.registerEvents)) {
|
|
|
|
// :TODO: ... or just systemInit() / systemShutdown() & mods could call Events.on() / Events.removeListener() ?
|
|
|
|
mod.registerEvents(this);
|
|
|
|
}
|
|
|
|
} catch(e) {
|
2018-01-15 16:41:18 +00:00
|
|
|
Log.warn( { error : e }, 'Exception during module "registerEvents"');
|
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
|
|
|
};
|