patient-mini/utils/eventBus.js
码农搬砖工 9018a4f868 xxx
2025-04-23 18:23:41 +08:00

21 lines
590 B
JavaScript

export default function createBus() {
return {
events: {},
on(event, callback) {
if (!this.events[event]) this.events[event] = [];
this.events[event].push(callback);
},
off(event, callback) {
if (!this.events[event]) return;
if (!callback) this.events[event] = [];
else {
const index = this.events[event].indexOf(callback);
if (index !== -1) this.events[event].splice(index, 1);
}
},
emit(event, ...args) {
if (this.events[event]) this.events[event].forEach((callback) => callback(...args));
},
};
}