2017-03-31 07:34:43 -04:00
|
|
|
var EventEmitter = require('events');
|
2017-02-24 06:18:57 -05:00
|
|
|
|
2017-08-03 20:03:17 -04:00
|
|
|
function warnIfLegacy(eventName) {
|
|
|
|
const legacyEvents = ['abi-vanila', 'abi', 'abi-contracts-vanila', 'abi-vanila-deployment'];
|
|
|
|
if (legacyEvents.indexOf(eventName) >= 0) {
|
2017-10-17 06:51:52 -04:00
|
|
|
console.info("this event is deprecated and will be removed in future versions: " + eventName);
|
2017-08-03 20:03:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 05:56:42 -04:00
|
|
|
function log(eventType, eventName) {
|
|
|
|
if (['end', 'prefinish', 'error', 'new', 'demo', 'block', 'version'].indexOf(eventName) >= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//console.log(eventType, eventName);
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:03:17 -04:00
|
|
|
const _on = EventEmitter.prototype.on;
|
|
|
|
const _setHandler = EventEmitter.prototype.setHandler;
|
|
|
|
|
|
|
|
EventEmitter.prototype.on = function(requestName, cb) {
|
2017-10-13 05:56:42 -04:00
|
|
|
log("listening to event: ", requestName);
|
2017-08-03 20:03:17 -04:00
|
|
|
warnIfLegacy(requestName);
|
|
|
|
return _on.call(this, requestName, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
EventEmitter.prototype.setHandler = function(requestName, cb) {
|
2017-10-13 05:56:42 -04:00
|
|
|
log("setting handler for: ", requestName);
|
2017-08-03 20:03:17 -04:00
|
|
|
warnIfLegacy(requestName);
|
|
|
|
return _setHandler.call(this, requestName, cb);
|
|
|
|
};
|
|
|
|
|
2017-12-30 17:51:52 -05:00
|
|
|
EventEmitter.prototype.request = function() {
|
|
|
|
let requestName = arguments[0];
|
|
|
|
let other_args = [].slice.call(arguments, 1);
|
|
|
|
|
2017-10-13 05:56:42 -04:00
|
|
|
log("requesting: ", requestName);
|
2017-08-03 20:03:17 -04:00
|
|
|
warnIfLegacy(requestName);
|
2017-12-30 17:51:52 -05:00
|
|
|
return this.emit('request:' + requestName, ...other_args);
|
2017-07-06 18:48:20 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
EventEmitter.prototype.setCommandHandler = function(requestName, cb) {
|
2017-10-13 05:56:42 -04:00
|
|
|
log("setting command handler for: ", requestName);
|
2017-08-03 20:03:17 -04:00
|
|
|
return this.on('request:' + requestName, function(_cb) {
|
2017-12-30 17:51:52 -05:00
|
|
|
cb.call(this, ...arguments);
|
2017-07-06 18:48:20 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-21 14:21:18 -04:00
|
|
|
EventEmitter.prototype.setCommandHandlerOnce = function(requestName, cb) {
|
|
|
|
log("setting command handler for: ", requestName);
|
|
|
|
return this.once('request:' + requestName, function(_cb) {
|
2017-12-30 17:51:52 -05:00
|
|
|
cb.call(this, ...arguments);
|
2017-10-21 14:21:18 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-02-24 06:18:57 -05:00
|
|
|
module.exports = EventEmitter;
|