whistle.listeners

class ListenersCollection[source]

Bases: object

__init__()[source]
Return type:

None

add(event_id, listener, /, *, priority=0)[source]

Add a listener for the given event id, with the given priority.

Priority must be a number between -127 and 128, inclusive. As a convention coming from unix process priorities, we use priorities ranging from -20 (highest priority) to 20 (lowest priority), with 0 being the default priority.

The actual permited range is so because python dicts are WAY faster with string keys than integers. This fact still puzzles me but the difference is huge. The same rationale applies to the event_id parameter: although we could support arbitrary hashable values as event ids (and we encouraged so in whistle 1.x), we now restrict it to strings for performance reasons.

Parameters:
  • event_id (str) – string identifier for the event

  • listener (Callable[[IEvent], Any | Coroutine]) – callback to be called when the event is dispatched

  • priority (int) – integer priority for the listener (-20 (high) to 19 (low))

Return type:

None

get(event_id, /)[source]

Gets the listeners for the given event id, in order of priority.

Parameters:

event_id (str) – string identifier for the event

Return type:

tuple[Callable[[IEvent], Any | Coroutine], …]

has(event_id, /)[source]
Parameters:

event_id (str)

Return type:

bool

items()[source]
keys()[source]
remove(event_id, listener, /)[source]
Parameters:
Return type:

None