A simple OOP event bus
Project description
Intro
This package implements an event bus based on the Mediator pattern. It is a powerful tool to avoid coupling and make components work together without any knowledge about each other (loose coupling). It helps:
- to avoid unneeded dependencies;
- to make testing easier;
- to extend the functionality by adding (not changing) the units;
- to reduce the tech debt.
Installation
pip install oop-event-bus
Usage
Create an event class with all the details you need
class UserSignedIn(Event):
def __init__(self, email: Email):
self.email = email
Create and register event listeners
class LogSignedInUsersListener(TypedEventListener[UserSignedIn]):
async def __call__(self, event: UserSignedIn):
...
event_bus = EventBus()
event_bus.listen(LogSignedInUsersListener())
Dispatch an event
await event_bus.dispatch(UserSignedIn(user.email))
Multi-listener
It is possible to register the listener to be called for more than one event:
class MyMultiEventListener(MultiEventListener):
def get_event_names(self) -> List[str]:
return [TestEvent.__name__, TestEvent2.__name__]
async def on_test_event(self, event: TestEvent):
...
async def on_test_event2(self, event: TestEvent2):
...
...
event_bus.listen(MyMultiEventListener())
event_bus.dispatch(TestEvent2(...))
Example
Given the following code:
class SomethingService:
def __init__(self, logger: Logger, mailer: Mailer, marketing: MarketingService):
self.logger = logger
self.mailer = mailer
self.marketing = marketing
async def do_something(self):
self._real()
self._business()
self._logic()
#side effects
self.logger.info("We did something")
await self.mailer.send_email("Welcome email")
#unrelated code that should be executed when we do something
await self.marketing.increase_marketing_counter()
There are SRP breaks, and a number of side effects unrelated to the business problem. This code is hard to test, it
couples the SomethingService with the concrete implementations of logging, mailing and marketing services.
With oop-event-bus you can move all side effects and unrelated code out.
Also, you will make it possible to extend functionality later without changing the code of this method.
from oop_bus import Event, EventBus
class WeDidSomething(Event):
...
class SomethingService:
def __init__(self, event_bus: EventBus):
self.event_bus = event_bus
async def do_something(self):
real()
business()
logic()
await self.event_bus.dispatch(WeDidSomething())
You can see that every piece of non-related code was replaced with the single event_bus call here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file oop_event_bus-0.1.0.tar.gz.
File metadata
- Download URL: oop_event_bus-0.1.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85c4afb39cafe4b20b39e8e34906c10c15c95a8325d80431e278dd47c4b29322
|
|
| MD5 |
23ca7e2d5099597798fb0c1f056b68e2
|
|
| BLAKE2b-256 |
edbc17bb9cd8da7b9e08d4adff5abd791f553dfd8d1eddb6ea226e95665d666b
|
File details
Details for the file oop_event_bus-0.1.0-py3-none-any.whl.
File metadata
- Download URL: oop_event_bus-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d6fddcbb9b58cd0347ccc608e20af55144a445e6bfab6d9b2ace610e685e9a2
|
|
| MD5 |
b3572765270af9488c685e6ebae1fa50
|
|
| BLAKE2b-256 |
b0cf1f4e471befb4c7b9839579b606525cfcaa3defa28c9acc81b988900f6f8d
|