chore: complete update
This commit is contained in:
46
resources/js/core/events.js
Normal file
46
resources/js/core/events.js
Normal file
@@ -0,0 +1,46 @@
|
||||
// src/core/events.js
|
||||
|
||||
// --- 1. Globaler EventBus ---
|
||||
|
||||
const listeners = new Map();
|
||||
|
||||
/**
|
||||
* Abonniert ein benanntes Event
|
||||
*/
|
||||
export function on(eventName, callback) {
|
||||
if (!listeners.has(eventName)) listeners.set(eventName, []);
|
||||
listeners.get(eventName).push(callback);
|
||||
|
||||
// Unsubscribe
|
||||
return () => {
|
||||
const arr = listeners.get(eventName);
|
||||
if (arr) listeners.set(eventName, arr.filter(fn => fn !== callback));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Sendet ein benanntes Event mit Payload
|
||||
*/
|
||||
export function emit(eventName, payload) {
|
||||
const arr = listeners.get(eventName);
|
||||
if (arr) arr.forEach(fn => fn(payload));
|
||||
}
|
||||
|
||||
// --- 2. ActionDispatcher ---
|
||||
|
||||
const actionListeners = new Set();
|
||||
|
||||
/**
|
||||
* Action registrieren (globaler Listener für alle Aktionen)
|
||||
*/
|
||||
export function registerActionListener(callback) {
|
||||
actionListeners.add(callback);
|
||||
return () => actionListeners.delete(callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktion ausführen
|
||||
*/
|
||||
export function dispatchAction(type, payload = {}) {
|
||||
actionListeners.forEach(fn => fn({ type, payload }));
|
||||
}
|
||||
Reference in New Issue
Block a user