Skip to content

Event System

EventEmitter

emit(event_type, data) async

Emit an event and invoke its handler and callbacks.

Parameters:

Name Type Description Default
event_type str

The type of event to emit.

required
data any

The data to pass to the event handler and callbacks.

required

Returns:

Type Description
dict

A context dictionary containing the result of the event handler.

Raises:

Type Description
EventNotFoundException

If no event is registered for the given type.

EventHandlerException

If there is an error invoking the event handler.

emit_sync(event_type, data)

Synchronous version of emit that properly awaits all callbacks. This is necessary because callbacks are fire-and-forget in async contexts, but in sync contexts wrapped with async_to_sync, the event loop may close before the tasks complete.

Event

add_callback(callback)

Add a callback to be executed after the event handler.

remove_callback(callback)

Remove a callback from the event.

EventRegistry

Bases: RegistryBase

Registry for managing events and their handlers. Allows registering event handlers and callbacks.

register_callback(event_type, callback)

Register a callback for an existing event.

register_event(event_type, handler, callbacks=None)

Register an event with its handler and optional callbacks.

unregister_callback(event_type, callback)

Unregister a callback for an existing event.

unregister_event(event_type)

Unregister an event by its type.

EventHandler

Bases: ABC

Abstract class for handling events. Each event handler should implement this class.

get_default_callbacks()

Return the default callbacks for this event handler. This method can be overridden by subclasses to provide custom callbacks.

WebsocketEventHandler

Bases: EventHandler

Abstract class for handling WebSocket events. Each WebSocket event handler should implement this class.

return_message() async

Return a message or a list of messages to be sent over the WebSocket. This method should be implemented by subclasses.

noop_handler(data) async

A no-operation handler that does nothing and returns an empty dictionary. This can be used as a placeholder for events that do not require handling.

EventException

Bases: Exception

Base class for all exceptions raised by the WhiteboxEvent system.