SillyTavern/public/lib/eventemitter.js

155 lines
3.8 KiB
JavaScript
Raw Normal View History

2023-07-20 19:32:15 +02:00
/* Polyfill indexOf. */
var indexOf;
if (typeof Array.prototype.indexOf === 'function') {
indexOf = function (haystack, needle) {
return haystack.indexOf(needle);
};
} else {
indexOf = function (haystack, needle) {
var i = 0, length = haystack.length, idx = -1, found = false;
while (i < length && !found) {
if (haystack[i] === needle) {
idx = i;
found = true;
}
i++;
}
return idx;
};
};
/* Polyfill EventEmitter. */
var EventEmitter = function () {
this.events = {};
};
EventEmitter.prototype.on = function (event, listener) {
2024-04-02 19:25:37 +02:00
// Unknown event used by external libraries?
if (event === undefined) {
console.trace('EventEmitter: Cannot listen to undefined event');
return;
}
2023-07-20 19:32:15 +02:00
if (typeof this.events[event] !== 'object') {
this.events[event] = [];
}
this.events[event].push(listener);
};
/**
* Makes the listener the last to be called when the event is emitted
* @param {string} event Event name
* @param {function} listener Event listener
*/
EventEmitter.prototype.makeLast = function (event, listener) {
if (typeof this.events[event] !== 'object') {
this.events[event] = [];
}
const events = this.events[event];
const idx = events.indexOf(listener);
if (idx > -1) {
events.splice(idx, 1);
}
events.push(listener);
}
/**
* Makes the listener the first to be called when the event is emitted
* @param {string} event Event name
* @param {function} listener Event listener
*/
EventEmitter.prototype.makeFirst = function (event, listener) {
if (typeof this.events[event] !== 'object') {
this.events[event] = [];
}
const events = this.events[event];
const idx = events.indexOf(listener);
if (idx > -1) {
events.splice(idx, 1);
}
events.unshift(listener);
}
2023-07-20 19:32:15 +02:00
EventEmitter.prototype.removeListener = function (event, listener) {
var idx;
if (typeof this.events[event] === 'object') {
idx = indexOf(this.events[event], listener);
if (idx > -1) {
this.events[event].splice(idx, 1);
}
}
};
EventEmitter.prototype.emit = async function (event) {
2024-03-16 17:17:04 +01:00
if (localStorage.getItem('eventTracing') === 'true') {
2024-05-28 23:57:32 +02:00
console.trace('Event emitted: ' + event, args);
2024-03-16 17:17:04 +01:00
} else {
console.debug('Event emitted: ' + event);
}
2023-07-20 19:32:15 +02:00
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 {
await listeners[i].apply(this, args);
}
catch (err) {
console.error(err);
console.trace('Error in event listener');
}
}
}
};
2023-12-18 21:54:28 +01:00
EventEmitter.prototype.emitAndWait = function (event) {
2024-03-16 17:17:04 +01:00
if (localStorage.getItem('eventTracing') === 'true') {
2024-05-28 23:57:32 +02:00
console.trace('Event emitted: ' + event, args);
2024-03-16 17:17:04 +01:00
} else {
console.debug('Event emitted: ' + event);
}
2023-12-18 21:54:28 +01:00
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');
}
}
}
};
2023-07-20 19:32:15 +02:00
EventEmitter.prototype.once = function (event, listener) {
this.on(event, function g () {
this.removeListener(event, g);
listener.apply(this, arguments);
});
};
export { EventEmitter }