Dispatcher#

class capymoa.ocl.events.Dispatcher[source]#

Bases: object

Publish events to subscribed callbacks.

Create a source, subscribe one or more handlers for an event type, and dispatch event instances with notify().

>>> source = Dispatcher()
>>> def handler(event: Event): print(f"Received event: {type(event).__name__}")
>>> source.subscribe(Event, handler)
>>> source.notify(Event()) 
Received event: Event

Subscriptions are keyed by exact event class.

__init__() → None[source]#

Initialize an empty mapping of event types to callbacks.

notify(event: Event) → None[source]#

Notify all subscribers of an event.

Only subscribers of the exact event type will be notified. Subscribers of parent event types will not be notified.

subscribe(
event_type: type[_E] | None,
callable: Callable[[_E], None],
) → None[source]#

Register a callback for a specific event class.

unsubscribe(
event_type: type[_E] | None,
callable: Callable[[_E], None],
) → None[source]#

Remove a previously registered callback for an event class.