blatann.event_type module

class blatann.event_type.Event(name)

Bases: Generic[TSender, TEvent]

Represents an event that can have handlers registered and deregistered. All handlers registered to an event should take in two parameters: the event sender and the event arguments.

Those familiar with the C#/.NET event architecture, this should look very similar, though registration is done using the register() method instead of += event_handler

register(handler)

Registers a handler to be called whenever the event is emitted. If the given handler is already registered, function does not register the handler a second time.

This function can be used in a with context block which will automatically deregister the handler when the context is exited.

Example

>>> with device.client.on_connected.register(my_connected_handler):
>>>    # Do something, my_connected_handler will be deregistered upon leaving this context
Parameters

handler (Callable[[TypeVar(TSender), TypeVar(TEvent)], None]) – The handler to register

Return type

EventSubscriptionContext[TypeVar(TSender), TypeVar(TEvent)]

Returns

a context block that can be used to automatically unsubscribe the handler

deregister(handler)

Deregisters a previously-registered handler so it no longer receives the event. If the given handler is not registered, function does nothing

Parameters

handler (Callable[[TypeVar(TSender), TypeVar(TEvent)], None]) – The handler to deregister

class blatann.event_type.EventSource(name, logger=None)

Bases: Event

Represents an Event object along with the controls to emit the events and notify handlers. This is done to “hide” the notify method from subscribers.

property has_handlers: bool

Gets if the event has any handlers subscribed to the event

clear_handlers()

Clears all handlers from the event

notify(sender, event_args=None)

Notifies all subscribers with the given sender and event arguments

class blatann.event_type.EventSubscriptionContext(event, subscriber)

Bases: Generic[TSender, TEvent]