EventLinker
class¶
A base class that acts as a global registry for events and callbacks linkage. It provides a centralized mechanism for managing event subscriptions, unsubscriptions, and retrieval of events and their associated event handlers.
Notes:
-
The
EventLinker
class can be subclassed to create specific namespaces or contexts for managing events and event handlers separately. By subclassing theEventLinker
, users can organize event subscriptions and handlers within different scopes, providing modularity and flexibility in event management. Subclassing also allows users to configure settings of theEventLinker
to suit their specific use cases. -
The
EventLinker
has been implemented with thread safety in mind. All of its methods synchronize access to prevent race conditions when managing events and event handlers across multiple threads. This ensures that concurrent operations on theEventLinker
are properly synchronized, avoiding data inconsistencies and race conditions.
Read more in the Pyventus docs for Event Linker.
Source code in pyventus/linkers/event_linker.py
16 17 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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 |
|
Classes¶
EventLinkageWrapper
¶
A class that serves as a wrapper for event linking operations, providing a simplified interface for subscribing events with their corresponding callbacks.
Notes:
-
This class can be used as either a decorator or a context manager. When used as a decorator, it automatically subscribes the decorated callback to the provided events. When used as a context manager with the
with
statement, it allows multiple callbacks to be associated with the provided events within the context block. -
This class is not intended to be subclassed or manually created. The
EventLinkageWrapper
is used internally as a wrapper for event linking operations.
Source code in pyventus/linkers/event_linker.py
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 |
|
Attributes¶
on_event
property
¶
Decorator that sets the main callback for the event. This callback will be invoked when the associated event occurs.
RETURNS | DESCRIPTION |
---|---|
Callable[[EventCallbackType], EventCallbackType]
|
The decorated callback. |
on_success
property
¶
Decorator that sets the success callback. This callback will be invoked when the event execution completes successfully.
RETURNS | DESCRIPTION |
---|---|
Callable[[SuccessCallbackType], SuccessCallbackType]
|
The decorated callback. |
on_failure
property
¶
Decorator that sets the failure callback. This callback will be invoked when the event execution fails.
RETURNS | DESCRIPTION |
---|---|
Callable[[FailureCallbackType], FailureCallbackType]
|
The decorated callback. |
Functions¶
__init__
¶
__init__(*events: SubscribableEventType, event_linker: Type[EventLinker], force_async: bool, once: bool) -> None
Initialize an instance of EventLinkageWrapper
.
PARAMETER | DESCRIPTION |
---|---|
events
|
The events to subscribe/link to.
TYPE:
|
event_linker
|
The event linker instance used for subscription.
TYPE:
|
force_async
|
Determines whether to force all callbacks to run asynchronously.
TYPE:
|
once
|
Specifies if the callback is a one-time subscription.
TYPE:
|
Source code in pyventus/linkers/event_linker.py
__call__
¶
Subscribes the provided events to the decorated callback.
PARAMETER | DESCRIPTION |
---|---|
callback
|
The callback to associate with the events.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
EventCallbackType
|
The decorated callback. |
Source code in pyventus/linkers/event_linker.py
__enter__
¶
__enter__() -> EventLinkageWrapper
Enters the linkage context block, allowing multiple callbacks to be associated with the events.
RETURNS | DESCRIPTION |
---|---|
EventLinkageWrapper
|
The context manager object |
__exit__
¶
__exit__(exc_type: Type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None) -> None
Exits the linkage context block, subscribing the provided callbacks within the context to the specified events. Performs any necessary cleanup.
PARAMETER | DESCRIPTION |
---|---|
exc_type
|
The type of the exception raised, if any.
TYPE:
|
exc_val
|
The exception object raised, if any.
TYPE:
|
exc_tb
|
The traceback information, if any.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None |
Source code in pyventus/linkers/event_linker.py
Functions¶
__init_subclass__
¶
__init_subclass__(max_event_handlers: int | None = None, default_success_callback: SuccessCallbackType | None = None, default_failure_callback: FailureCallbackType | None = None, debug: bool | None = None, **kwargs: Any) -> None
Initialize a subclass of EventLinker
.
By default, this method sets up the main registry and thread lock object, but
it can also be used to configure specific settings of the EventLinker
subclass.
PARAMETER | DESCRIPTION |
---|---|
max_event_handlers
|
The maximum number of event handlers allowed per event, or
TYPE:
|
default_success_callback
|
The default callback to assign as the success callback in the event handlers when no specific success callback is provided.
TYPE:
|
default_failure_callback
|
The default callback to assign as the failure callback in the event handlers when no specific failure callback is provided.
TYPE:
|
debug
|
Specifies the debug mode for the subclass logger. If
TYPE:
|
kwargs
|
The keyword arguments to pass to the superclass
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None |
RAISES | DESCRIPTION |
---|---|
PyventusException
|
If |
Source code in pyventus/linkers/event_linker.py
get_event_name
classmethod
¶
Determines the name of the event.
PARAMETER | DESCRIPTION |
---|---|
event
|
The event to obtain the name for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
A string that represents the event name. |
RAISES | DESCRIPTION |
---|---|
PyventusException
|
If the |
Source code in pyventus/linkers/event_linker.py
get_max_event_handlers
classmethod
¶
Retrieve the maximum number of event handlers allowed per event.
RETURNS | DESCRIPTION |
---|---|
int | None
|
The maximum number of event handlers or |
Source code in pyventus/linkers/event_linker.py
get_default_success_callback
classmethod
¶
Retrieve the default callback to be assigned as the success callback in the event handlers when no specific success callback is provided.
RETURNS | DESCRIPTION |
---|---|
SuccessCallbackType | None
|
The default success callback or |
Source code in pyventus/linkers/event_linker.py
get_default_failure_callback
classmethod
¶
Retrieve the default callback to be assigned as the failure callback in the event handlers when no specific failure callback is provided.
RETURNS | DESCRIPTION |
---|---|
FailureCallbackType | None
|
The default failure callback or |
Source code in pyventus/linkers/event_linker.py
get_registry
classmethod
¶
get_registry() -> Mapping[str, List[EventHandler]]
Retrieve the main registry mapping.
RETURNS | DESCRIPTION |
---|---|
Mapping[str, List[EventHandler]]
|
A mapping of event names to event handlers. |
Source code in pyventus/linkers/event_linker.py
get_events
classmethod
¶
Retrieve a list of all the registered events.
RETURNS | DESCRIPTION |
---|---|
List[str]
|
A list of event names. |
get_event_handlers
classmethod
¶
get_event_handlers() -> List[EventHandler]
Retrieve a list of non-duplicated event handlers that have been registered across all events.
RETURNS | DESCRIPTION |
---|---|
List[EventHandler]
|
A list of event handlers. |
Source code in pyventus/linkers/event_linker.py
get_events_by_event_handler
classmethod
¶
get_events_by_event_handler(event_handler: EventHandler) -> List[str]
Retrieve a list of event names associated with the provided event handler.
PARAMETER | DESCRIPTION |
---|---|
event_handler
|
The handler to retrieve the associated events for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[str]
|
A list of event names. |
RAISES | DESCRIPTION |
---|---|
PyventusException
|
If the |
Source code in pyventus/linkers/event_linker.py
get_event_handlers_by_events
classmethod
¶
get_event_handlers_by_events(*events: SubscribableEventType) -> List[EventHandler]
Retrieve a list of non-duplicated event handlers associated with the provided events.
PARAMETER | DESCRIPTION |
---|---|
events
|
Events to retrieve the event handlers for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[EventHandler]
|
A list of event handlers. |
RAISES | DESCRIPTION |
---|---|
PyventusException
|
If the |
Source code in pyventus/linkers/event_linker.py
once
classmethod
¶
once(*events: SubscribableEventType, force_async: bool = False) -> EventLinkageWrapper
Decorator that allows you to conveniently subscribe callbacks to the provided events for a single invocation.
This method can be used as either a decorator or a context manager. When used as a
decorator, it automatically subscribes the decorated callback to the provided events.
When used as a context manager with the with
statement, it allows multiple callbacks
to be associated with the provided events within the context block.
PARAMETER | DESCRIPTION |
---|---|
events
|
The events to subscribe to.
TYPE:
|
force_async
|
Determines whether to force all callbacks to run asynchronously. If
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
EventLinkageWrapper
|
The decorator that wraps the callback. |
Source code in pyventus/linkers/event_linker.py
on
classmethod
¶
on(*events: SubscribableEventType, force_async: bool = False) -> EventLinkageWrapper
Decorator that allows you to conveniently subscribe callbacks to the provided events.
This method can be used as either a decorator or a context manager. When used as a
decorator, it automatically subscribes the decorated callback to the provided events.
When used as a context manager with the with
statement, it allows multiple callbacks
to be associated with the provided events within the context block.
PARAMETER | DESCRIPTION |
---|---|
events
|
The events to subscribe to.
TYPE:
|
force_async
|
Determines whether to force all callbacks to run asynchronously. If
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
EventLinkageWrapper
|
The decorator that wraps the callback. |
Source code in pyventus/linkers/event_linker.py
subscribe
classmethod
¶
subscribe(*events: SubscribableEventType, event_callback: EventCallbackType, success_callback: SuccessCallbackType | None = None, failure_callback: FailureCallbackType | None = None, force_async: bool = False, once: bool = False) -> EventHandler
Subscribes callbacks to the provided events.
PARAMETER | DESCRIPTION |
---|---|
events
|
The events to subscribe to.
TYPE:
|
event_callback
|
The callback to be executed when the event occurs.
TYPE:
|
success_callback
|
The callback to be executed when the event execution completes successfully.
TYPE:
|
failure_callback
|
The callback to be executed when the event execution fails.
TYPE:
|
force_async
|
Determines whether to force all callbacks to run asynchronously. If
TYPE:
|
once
|
Specifies if the event handler is a one-time subscription.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
EventHandler
|
The event handler object associated with the given events. |
Source code in pyventus/linkers/event_linker.py
unsubscribe
classmethod
¶
unsubscribe(*events: SubscribableEventType, event_handler: EventHandler) -> bool
Unsubscribes an event handler from the provided events. If there are no more handlers for a particular event, that event is also removed from the registry.
PARAMETER | DESCRIPTION |
---|---|
events
|
The events to unsubscribe from.
TYPE:
|
event_handler
|
The event handler to unsubscribe.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
|
RAISES | DESCRIPTION |
---|---|
PyventusException
|
If the |
Source code in pyventus/linkers/event_linker.py
remove_event_handler
classmethod
¶
remove_event_handler(event_handler: EventHandler) -> bool
Removes an event handler from all subscribed events. If there are no more handlers for a particular event, that event is also removed from the registry.
PARAMETER | DESCRIPTION |
---|---|
event_handler
|
The event handler to remove.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
|
RAISES | DESCRIPTION |
---|---|
PyventusException
|
If the |
Source code in pyventus/linkers/event_linker.py
remove_event
classmethod
¶
Removes an event from the registry, including all its event handler subscriptions.
PARAMETER | DESCRIPTION |
---|---|
event
|
The event to remove.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
|
Source code in pyventus/linkers/event_linker.py
remove_all
classmethod
¶
Removes all events and their associated event handlers from the registry.
RETURNS | DESCRIPTION |
---|---|
bool
|
|