Event reference#
EventListener#
EventController#
- class asyncio_event_hub.controller.EventController#
New in version 1.0.
Responsible for controlling the event loop, listening and emitting events.
- property running: bool#
New in version 1.0.
Returns bool indicating if the controller is running or not.
- property subcontrollers: List[EventController]#
New in version 1.0.
Returns a list of controller’s sub-controllers.
- critical()#
New in version 1.0.
Method that returns an async context manager, that prevents any events from being processed while in the critical section.
Example:
async with controller.critical(): # Can't process events here await something.initialize() # Can process events here
- start()#
New in version 1.0.
Starts the event loop of this master controller and it’s subcontrollers.
- add_subcontroller(controller: EventController)#
New in version 1.0.
Adds a sub-controller, that also receives events emitted to the current controller. If this master controller is running and the
controllerbeing added is not, it is automatically started. If this master controller is not running, thecontrollerbeing added is started, after this master controller is started.- Parameters:
controller (EventController) – The controller to add as a sub-controller.
- remove_subcontroller(controller: EventController)#
New in version 1.0.
Removes the sub-controller. The sub-controller IS NOT stopped.
- Parameters:
controller (EventController) – The controller to remove.
- Raises:
ValueError – The
controlleris not a subcontroller of the current controller
- stop()#
New in version 1.0.
Stops event loop asynchronously
- Returns:
A Future object that can be used to await for the controller’s task to stop and also the tasks of all the subcontrollers.
- Return type:
- add_listener(event: Enum | str | int, fnc: Callable[[T], Any], predicate: Callable[[T], bool] | None = None)#
New in version 1.0.
Registers the function
fncas an event listener forevent.- Parameters:
event (Union[Enum, str, int]) – The event of listener to add.
fnc (Callable) – The function listener to add.
predicate (Optional[Callable[[Any], bool]]) – Optional function parameter that accepts the same parameters as
fncand must return True, indicating iffncis to be called or not. If it returns False, thefncdoes not get called after event is emitted.
- remove_listener(event: Enum | str | int, fnc: Callable)#
New in version 1.0.
Remove the function
fncfrom the list of listeners forevent.- Parameters:
- Raises:
KeyError – The event doesn’t have any listeners.
ValueError – Provided function is not a listener.
- listen(event: Enum | str | int)#
New in version 1.0.
Decorator used to register the function as an event listener.
Example:
ctrl = EventController() @ctrl.listen("my_event") async def event_handler(): print("Received event")
- emit(event: Enum | str | int, *args, priority: int = 0, **kwargs) Future#
New in version 1.0.
Emits an
eventby calling all the registered listeners from the event loop.Example:
ctrl = EventController() ... # Add listeners (handlers) ctrl.emit("my_event")
- Parameters:
args – Variadic positional arguments passed to event listeners.
priority (int) –
New in version 1.1.
The priority of event. Events with higher priority are processed before the ones with lower priority. Defaults to 0. This is a keyword only priority.
kwargs – Variadic keyword arguments passed to event listeners.
- Returns:
A future object that can be awaited. You can use this to wait for the event to actually be processed. The result of the future will always be None.
- Return type:
- Raises:
TypeError – Arguments provided don’t match all the listener parameters.
- clear_queue()#
New in version 1.0.
Clears all emitted events from queue (recreates the queue).
- async event_loop()#
New in version 1.0.
Event loop task.