Add 'synchronous' event emitter

This commit is contained in:
maver 2023-12-18 21:54:28 +01:00
parent be5d428706
commit 0a649a395d
1 changed files with 21 additions and 0 deletions

View File

@ -69,6 +69,27 @@ EventEmitter.prototype.emit = async function (event) {
} }
}; };
EventEmitter.prototype.emitAndWait = function (event) {
console.debug('Event emitted: ' + event);
var i, listeners, length, args = [].slice.call(arguments, 1);
if (typeof this.events[event] === 'object') {
listeners = this.events[event].slice();
length = listeners.length;
for (i = 0; i < length; i++) {
try {
listeners[i].apply(this, args);
}
catch (err) {
console.error(err);
console.trace('Error in event listener');
}
}
}
};
EventEmitter.prototype.once = function (event, listener) { EventEmitter.prototype.once = function (event, listener) {
this.on(event, function g () { this.on(event, function g () {
this.removeListener(event, g); this.removeListener(event, g);