embark-area-51/lib/core/events.js

46 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-03-31 11:34:43 +00:00
var EventEmitter = require('events');
function warnIfLegacy(eventName) {
const legacyEvents = ['abi-vanila', 'abi', 'abi-contracts-vanila', 'abi-vanila-deployment'];
if (legacyEvents.indexOf(eventName) >= 0) {
console.warn("this event is deprecated and will be removed in future versions: " + eventName);
}
}
2017-10-13 09:56:42 +00:00
function log(eventType, eventName) {
if (['end', 'prefinish', 'error', 'new', 'demo', 'block', 'version'].indexOf(eventName) >= 0) {
return;
}
//console.log(eventType, eventName);
}
const _on = EventEmitter.prototype.on;
const _setHandler = EventEmitter.prototype.setHandler;
EventEmitter.prototype.on = function(requestName, cb) {
2017-10-13 09:56:42 +00:00
log("listening to event: ", requestName);
warnIfLegacy(requestName);
return _on.call(this, requestName, cb);
};
EventEmitter.prototype.setHandler = function(requestName, cb) {
2017-10-13 09:56:42 +00:00
log("setting handler for: ", requestName);
warnIfLegacy(requestName);
return _setHandler.call(this, requestName, cb);
};
EventEmitter.prototype.request = function(requestName, cb) {
2017-10-13 09:56:42 +00:00
log("requesting: ", requestName);
warnIfLegacy(requestName);
return this.emit('request:' + requestName, cb);
};
EventEmitter.prototype.setCommandHandler = function(requestName, cb) {
2017-10-13 09:56:42 +00:00
log("setting command handler for: ", requestName);
return this.on('request:' + requestName, function(_cb) {
cb.call(this, _cb);
});
};
module.exports = EventEmitter;