mirror of
https://github.com/HugoAura/Seewo-HugoAura.git
synced 2026-06-21 23:54:26 +08:00
[Feat] New settings passwd UX & Config enc support
This commit is contained in:
47
src/aura/utils/eventBus.js
Executable file
47
src/aura/utils/eventBus.js
Executable file
@@ -0,0 +1,47 @@
|
||||
class EventBus {
|
||||
constructor() {
|
||||
this.listeners = new Map();
|
||||
}
|
||||
|
||||
on(eventName, callback) {
|
||||
if (!this.listeners.has(eventName)) {
|
||||
this.listeners.set(eventName, new Set());
|
||||
}
|
||||
this.listeners.get(eventName).add(callback);
|
||||
return () => this.off(eventName, callback);
|
||||
}
|
||||
|
||||
off(eventName, callback) {
|
||||
if (this.listeners.has(eventName)) {
|
||||
this.listeners.get(eventName).delete(callback);
|
||||
if (this.listeners.get(eventName).size === 0) {
|
||||
this.listeners.delete(eventName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
emit(eventName, ...args) {
|
||||
if (this.listeners.has(eventName)) {
|
||||
this.listeners.get(eventName).forEach((callback) => {
|
||||
try {
|
||||
callback(...args);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`[HugoAura / EventBus] Error in ${eventName} callback:`,
|
||||
error
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
once(eventName, callback) {
|
||||
const onceCallback = (...args) => {
|
||||
callback(...args);
|
||||
this.off(eventName, onceCallback);
|
||||
};
|
||||
return this.on(eventName, onceCallback);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = EventBus;
|
||||
Reference in New Issue
Block a user