Menu
Introduction
Getting Started
Developer guide
Release
Architecture
CLI
Technical documentations
Version
Publication date

Mar 9, 2022

Confidentiality
Public
Reactions
1
Share

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)
Functions
__init__

Private constructor. Use get_instance() instead.

clear_listeners

Remove all registered listeners.

This is useful for testing or resetting the dispatcher.

dispatch

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.

event : Event
The event to dispatch
get_registered_listeners

Get all registered listeners.

Return type : list[EventListener]
register

Register an event listener.

listener : EventListener
The listener to register
shutdown

Shutdown 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

Unregister an event listener.

listener : EventListener
The listener to unregister
get_instance @classmethod

Get the singleton instance of the EventDispatcher.

Return type : EventDispatcher
Shine Logo
Technical bricks to reuse or customize

Have you developed a brick?

Share it to accelerate projects for the entire community.