stimbus
Version:
An event bus for stimulus
30 lines (29 loc) • 806 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class EventBus {
#bus;
#events;
constructor() {
this.#events = {};
this.#bus = {};
}
addEvent(callerId, type, listener) {
if (!this.#events[callerId]) {
this.#events[callerId] = {};
}
this.#events[callerId][type] = listener;
this.#bus[type]?.push(listener);
}
removeEvent(callerId, type) {
const listener = this.#events[callerId][type];
if (!listener) {
return;
}
this.#bus[type] = this.#bus[type]?.filter((x) => x !== listener);
delete this.#events[callerId][type];
}
trigger(type, detail) {
this.#bus[type]?.forEach((x) => x(detail));
}
}
exports.default = EventBus;