A lightweight library with an implementation of Pub-Sub.
Project description
# Domain Events
A lightweight library with an implementation of Pub-Sub.
## Install
```
$ pipenv install domain-events
```
or
```
$ pip install domain-events
```
## Usage
In order to have the pub-sub working in your application, you'll need to write your own subscribers and to subscribe them to the publisher.
`domain-events` provides an interface for subscribers that needs to be implemented by your own subscribers in the first place. Two methods are mandatory: `handle(self, event)` and `_events_subscribed_to(self)`. Following is an example of how to do it.
```python
from domain_events import Subscriber
class ExampleSubscriber(Subscriber):
def handle(self, event):
# Logic to handle the occurring event
def _events_subscribed_to(self):
""" Return the tuple of events it is subscribed to. """
return (AnEvent, AnotherEvent)
```
Once the subscriber is implemented, you'll need to subscribe it to the event publisher.
```python
from domain_events import Publisher
Publisher().subscribe(ExampleSubscriber())
```
Now, in order to publish an event, just call the `publish` method on the `Publisher` with one of your domain events.
```python
from domain_events import Event, Publisher
class AnEvent(Event):
pass
Publisher().publish(AnEvent())
```
The event will be passed on to the proper subscribers.
A lightweight library with an implementation of Pub-Sub.
## Install
```
$ pipenv install domain-events
```
or
```
$ pip install domain-events
```
## Usage
In order to have the pub-sub working in your application, you'll need to write your own subscribers and to subscribe them to the publisher.
`domain-events` provides an interface for subscribers that needs to be implemented by your own subscribers in the first place. Two methods are mandatory: `handle(self, event)` and `_events_subscribed_to(self)`. Following is an example of how to do it.
```python
from domain_events import Subscriber
class ExampleSubscriber(Subscriber):
def handle(self, event):
# Logic to handle the occurring event
def _events_subscribed_to(self):
""" Return the tuple of events it is subscribed to. """
return (AnEvent, AnotherEvent)
```
Once the subscriber is implemented, you'll need to subscribe it to the event publisher.
```python
from domain_events import Publisher
Publisher().subscribe(ExampleSubscriber())
```
Now, in order to publish an event, just call the `publish` method on the `Publisher` with one of your domain events.
```python
from domain_events import Event, Publisher
class AnEvent(Event):
pass
Publisher().publish(AnEvent())
```
The event will be passed on to the proper subscribers.
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 Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for domain_events-1.0-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50c4a15ee0465ccb5af9ac13a96141db399e8b8cf35763e170315f0e17ed0fd8 |
|
MD5 | b8d9b5e897930d46f5a83dda36a8d68b |
|
BLAKE2b-256 | e83451186434de1ecbcbb949d8845b0bcb922bd73964ba5d288e67f70b411304 |