EventDispatcher
Central event dispatcher for the application.
This is a singleton service that manages event listeners and dispatches events to them. Events are dispatched asynchronously using a single worker thread that processes events from a queue, avoiding the overhead of creating new threads for each event.
Usage: # Register a listener dispatcher = EventDispatcher.get_instance() dispatcher.register(MyListener())
# Dispatch an event
event = UserCreatedEvent(entity=user, triggered_by=current_user)
dispatcher.dispatch(event)
# Unregister a listener
dispatcher.unregister(listener)
Private constructor. Use get_instance() instead.
Remove all registered listeners.
This is useful for testing or resetting the dispatcher.
Dispatch an event to all registered listeners.
Synchronous listeners (is_synchronous() == True) are called immediately in the caller's thread. Exceptions propagate to the caller.
Asynchronous listeners (is_synchronous() == False) are queued for the background worker thread. Exceptions are caught and logged.
Sync listeners always run BEFORE async listeners are queued.
EventGet all registered listeners.
list[EventListener]Register an event listener.
EventListenerShutdown the worker thread gracefully.
This method should be called when the application is shutting down. It waits for all pending events in the queue to be processed before stopping.
Unregister an event listener.
EventListenerGet the singleton instance of the EventDispatcher.
EventDispatcher