EventEmitter
class¶
Bases: ABC
An abstract base class for event emitters.
Notes:
-
This class defines a common interface for emitting events. It serves as a foundation for implementing custom event emitters with specific dispatch strategies. It is designed to handle
string-named
events with positional and keyword arguments, as well as instances ofdataclass
objects andExceptions
objects. -
The main goal of this class is to decouple the event emission process from the underlying implementation. This loose coupling promotes flexibility, adaptability, and adheres to the Open-Closed principle, allowing custom event emitters to be implemented without affecting existing consumers.
Read more in the Pyventus docs for Event Emitter.
Source code in pyventus/emitters/event_emitter.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
Classes¶
EventEmission
¶
Represents an event emission that has been triggered but whose propagation is not yet complete. It provides a self-contained context for executing the event emission, encapsulating both the event data and the associated event handlers.
This class acts as an isolated unit of work to asynchronously propagate the emission
of an event. When an event occurs, the EventEmitter
class creates an EventEmission
instance, which is then processed by the _process()
method to handle the event
propagation.
Source code in pyventus/emitters/event_emitter.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
Attributes¶
id
property
¶
Gets the unique identifier of the event emission.
RETURNS | DESCRIPTION |
---|---|
str
|
The unique identifier of the event emission. |
timestamp
property
¶
Gets the timestamp when the event emission was created.
RETURNS | DESCRIPTION |
---|---|
datetime
|
The timestamp when the event emission was created. |
event
property
¶
Gets the name of the event being emitted.
RETURNS | DESCRIPTION |
---|---|
str
|
The name of the event. |
Functions¶
__init__
¶
__init__(event: str, event_handlers: List[EventHandler], event_args: Tuple[Any, ...], event_kwargs: Dict[str, Any], debug: bool) -> None
Initialize an instance of EventEmission
.
PARAMETER | DESCRIPTION |
---|---|
event
|
The name of the event being emitted.
TYPE:
|
event_handlers
|
List of event handlers associated with the event.
TYPE:
|
event_args
|
Positional arguments to be passed to the event handlers.
TYPE:
|
event_kwargs
|
Keyword arguments to be passed to the event handlers.
TYPE:
|
debug
|
Indicates if debug mode is enabled.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
PyventusException
|
If |
Source code in pyventus/emitters/event_emitter.py
__call__
async
¶
Execute the event handlers concurrently.
RETURNS | DESCRIPTION |
---|---|
None
|
None |
Source code in pyventus/emitters/event_emitter.py
Functions¶
__init__
¶
__init__(event_linker: Type[EventLinker] = EventLinker, debug: bool | None = None) -> None
Initialize an instance of EventEmitter
.
PARAMETER | DESCRIPTION |
---|---|
event_linker
|
Specifies the type of event linker used to manage and access events along with their corresponding event handlers. Defaults to
TYPE:
|
debug
|
Specifies the debug mode for the subclass logger. If
TYPE:
|
RAISES | DESCRIPTION |
---|---|
PyventusException
|
If the |
Source code in pyventus/emitters/event_emitter.py
emit
¶
Emits an event and triggers its associated event handlers.
Notes:
- When emitting
dataclass
objects orException
objects, they are automatically passed to the event handler as the first positional argument, even if you pass additional*args
or**kwargs
. - If there are event handlers subscribed to the global event
...
, also known asEllipsis
, they will also be triggered each time an event or exception is emitted.
PARAMETER | DESCRIPTION |
---|---|
event
|
The event to be emitted. It can be
TYPE:
|
args
|
Positional arguments to be passed to the event handlers.
TYPE:
|
kwargs
|
Keyword arguments to be passed to the event handlers.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None |
Source code in pyventus/emitters/event_emitter.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
|
_process
abstractmethod
¶
_process(event_emission: EventEmission) -> None
Process the event emission execution. Subclasses must implement this method to define the specific processing logic.
PARAMETER | DESCRIPTION |
---|---|
event_emission
|
The event emission to be processed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None |