25 lines
632 B
JavaScript
25 lines
632 B
JavaScript
|
"use strict";
|
||
|
require("../../common/vendor.js");
|
||
|
const eventBus = {
|
||
|
listeners: {},
|
||
|
on(eventName, callback) {
|
||
|
if (!this.listeners[eventName]) {
|
||
|
this.listeners[eventName] = [];
|
||
|
}
|
||
|
this.listeners[eventName].push(callback);
|
||
|
},
|
||
|
emit(eventName, ...args) {
|
||
|
const callbacks = this.listeners[eventName];
|
||
|
if (callbacks) {
|
||
|
callbacks.forEach((callback) => callback(...args));
|
||
|
}
|
||
|
},
|
||
|
off(eventName, callback) {
|
||
|
const callbacks = this.listeners[eventName];
|
||
|
if (callbacks) {
|
||
|
this.listeners[eventName] = callbacks.filter((cb) => cb !== callback);
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
exports.eventBus = eventBus;
|