2017-06-11 00:29:00 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const events = require('events');
|
|
|
|
const Log = require('./logger.js').log;
|
|
|
|
const SystemEvents = require('./system_events.js');
|
2017-06-23 04:19:34 +00:00
|
|
|
|
2019-01-07 04:56:12 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const _ = require('lodash');
|
2019-01-07 04:56:12 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
module.exports = new (class Events extends events.EventEmitter {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
2022-06-05 20:04:25 +00:00
|
|
|
this.setMaxListeners(64); // :TODO: play with this...
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2017-06-23 04:19:34 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
getSystemEvents() {
|
|
|
|
return SystemEvents;
|
|
|
|
}
|
2018-03-04 20:34:35 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
addListener(event, listener) {
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.trace({ event: event }, 'Registering event listener');
|
2018-06-22 05:15:04 +00:00
|
|
|
return super.addListener(event, listener);
|
|
|
|
}
|
2017-06-23 04:19:34 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
emit(event, ...args) {
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.trace({ event: event }, 'Emitting event');
|
2018-06-22 05:15:04 +00:00
|
|
|
return super.emit(event, ...args);
|
|
|
|
}
|
2017-06-23 04:19:34 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
on(event, listener) {
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.trace({ event: event }, 'Registering event listener');
|
2018-06-22 05:15:04 +00:00
|
|
|
return super.on(event, listener);
|
|
|
|
}
|
2017-06-23 04:19:34 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
once(event, listener) {
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.trace({ event: event }, 'Registering single use event listener');
|
2018-06-22 05:15:04 +00:00
|
|
|
return super.once(event, listener);
|
|
|
|
}
|
2017-06-23 04:19:34 +00:00
|
|
|
|
2019-01-07 04:56:12 +00:00
|
|
|
//
|
|
|
|
// Listen to multiple events for a single listener.
|
|
|
|
// Called with: listener(event, eventName)
|
|
|
|
//
|
|
|
|
// The returned object must be used with removeMultipleEventListener()
|
|
|
|
//
|
|
|
|
addMultipleEventListener(events, listener) {
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.trace({ events }, 'Registering event listeners');
|
2019-01-07 04:56:12 +00:00
|
|
|
|
|
|
|
const listeners = [];
|
|
|
|
|
2018-07-21 20:32:06 +00:00
|
|
|
events.forEach(eventName => {
|
2019-01-07 04:56:12 +00:00
|
|
|
const listenWrapper = _.partial(listener, _, eventName);
|
|
|
|
this.on(eventName, listenWrapper);
|
2022-06-05 20:04:25 +00:00
|
|
|
listeners.push({ eventName, listenWrapper });
|
2019-01-07 04:56:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return listeners;
|
|
|
|
}
|
|
|
|
|
|
|
|
removeMultipleEventListener(listeners) {
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.trace({ events }, 'Removing listeners');
|
2019-01-07 04:56:12 +00:00
|
|
|
listeners.forEach(listener => {
|
|
|
|
this.removeListener(listener.eventName, listener.listenWrapper);
|
2018-07-21 20:32:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
removeListener(event, listener) {
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.trace({ event: event }, 'Removing listener');
|
2018-06-22 05:15:04 +00:00
|
|
|
return super.removeListener(event, listener);
|
|
|
|
}
|
2017-06-23 04:19:34 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
startup(cb) {
|
2018-07-21 20:32:06 +00:00
|
|
|
return cb(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
})();
|