Events
plateforme.core.events
This module provides a foundational pattern for event-driven programming within the Plateforme framework, enabling objects to emit and respond to events.
The EventEmitter
class, serving as a mixin, endows any object with the
ability to handle events. It allows for registering event listeners, emitting
events, and invoking these listeners upon event occurrences. This class is
designed to be integrated with other objects to facilitate event-driven
architecture.
The module also includes a decorator, emit
, which enhances methods to emit
events at specified points in their execution, such as before, after, or upon
encountering an error. This decorator allows for a clean and declarative way to
attach event emission to method invocations, further promoting a modular and
flexible event-driven design.
Examples:
>>> class MyComponent(EventEmitter):
... @emit(events=['before', 'after'])
... def perform_action(self):
... print("Action performed")
>>> component = MyComponent()
>>> component.on('perform_action_before', lambda: print("Before action"))
>>> component.on('perform_action_after', lambda: print("After action"))
>>> component.perform_action()
'Before action'
'Action performed'
'After action'
EventSource
module-attribute
EventSource = Callable[Concatenate[_T, _P], _R]
A type alias for a source of events.
EventType
module-attribute
EventType = Literal['before', 'after', 'error']
A type alias for event types.
EventEmitter
A mixin class to add event handling capabilities to any object.
on
Registers a listener for a given event.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
str
|
The name of the event. |
required |
listener
|
Callable[..., Any]
|
The callback function to invoke when the event is emitted. |
required |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/events.py
emit
Emits an event, calling all registered listeners for this event.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
str
|
The name of the event. |
required |
args
|
Any
|
Positional arguments to pass to the listener. |
()
|
kwargs
|
Any
|
Keyword arguments to pass to the listener. |
{}
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/events.py
emit
emit(
events: list[EventType] | None = None,
) -> Callable[
[EventSource[_R" optional hover>_R, _P" optional hover>_P, _R]], EventSource[_T, _P, _R]
]
A decorator to add event emission.
It adds event emission before, after or on error of a function execution.
This decorator is designed to be used with methods of classes that inherit
from EventEmitter
class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
events
|
list[EventType] | None
|
List of events to emit. The possible values are:
- |
None
|