enigma-bbs/core/events.js

74 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-06-11 00:29:00 +00:00
/* jslint node: true */
'use strict';
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);
}
async.each(files, (moduleName, nextModule) => {
2018-01-15 16:41:18 +00:00
const fullModulePath = paths.join(modulePath, moduleName);
try {
2018-01-15 16:41:18 +00:00
const mod = require(fullModulePath);
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"');
}
return nextModule(null);
}, err => {
return nextPath(err);
});
});
}, err => {
return cb(err);
2017-06-11 00:29:00 +00:00
});
}
};