@shadow-dev/core
Version:
A modular core framework for Discord bot development, providing commands, buttons, menus, middleware, and more.
46 lines (45 loc) • 1.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShadowEventBus = void 0;
const EventPriority_1 = require("./EventPriority");
class ShadowEventBus {
constructor() {
this.listeners = new Map();
}
subscribe(eventName, listener, priority = EventPriority_1.EventPriority.NORMAL) {
if (!this.listeners.has(eventName)) {
this.listeners.set(eventName, new Map());
}
const priorityMap = this.listeners.get(eventName);
if (!priorityMap.has(priority)) {
priorityMap.set(priority, new Set());
}
priorityMap.get(priority).add(listener);
}
async dispatch(eventName, event) {
const priorityMap = this.listeners.get(eventName);
if (!priorityMap)
return;
const order = [
EventPriority_1.EventPriority.HIGH,
EventPriority_1.EventPriority.NORMAL,
EventPriority_1.EventPriority.LOW
];
for (const priority of order) {
const handlers = priorityMap.get(priority);
if (!handlers)
continue;
for (const handler of handlers) {
try {
await handler(event);
}
catch (err) {
console.error(`[EventBus] Error handling ${eventName} @ ${EventPriority_1.EventPriority[priority]}`, err);
}
if (event.cancelled)
return;
}
}
}
}
exports.ShadowEventBus = ShadowEventBus;