Event Manager Implementation

This module defines an event registry, notification and filtering class.

The main class of the module is the EventManager.

class encore.events.event_manager.EventManager

A single registry point for all application events.

connect(cls, func, filter=None, priority=0)

Add a listener for the event.

Parameters:
  • cls (class) – The class of events for which the listener is registered.
  • func (callable) – A callable to be called when the event is emitted. The function should expect one argument which is the event instance which was emitted.
  • filter (dict) –

    Filters to match for before calling the listener. The listener is called only when the event matches all of the filter .

    Filter specification:
    • key: string which is name of an attribute of the event instance.
    • value: the value of the specified attribute.
  • priority (int) – The priority of the listener. Higher priority listeners are called before lower priority listeners.

Note

Reconnecting an already connected listener will disconnect the old listener. This may have rammifications in changing the filters and the priority.

The filtering is added so that future optimizations can be done on specific events with large number of handlers. For example there should be a fast way to filter key events to specific listeners rather than iterating through all listeners.

disable(cls)

Disable the event from generating notifications.

Parameters:cls (class) – The class of events which we want to disable.
disconnect(cls, func)

Disconnects a listener from being notified about the event’

Parameters:
  • cls (class) – The class of events for which the listener is registered.
  • func (callable) – The callable which was registered for that class.
Raises:

KeyError - if func is not already connected.

emit(event, block=True)

Notifies all listeners about the event with the specified arguments.

Parameters:
  • event (instance of BaseEvent) – The BaseEvent instance to emit.
  • block (bool) – Whether to block the call until the event handling is finished. If block is False, the event will be emitted in a separate thread and the thread will be returned, so you can later query its status or do wait() on the thread.

Note

Listeners of superclasses of the event are also called. Eg. a BaseEvent listener will also be notified about any derived class events.

enable(cls)

Enable the event again to generate notifications.

Parameters:cls (class) – The class of events which we want to enable.
is_enabled(cls)

Check if the event is enabled.

Parameters:cls (class) – The class of events which we want check the status of.