Skip to content

Commit 573acc5

Browse files
authored
Docs/event system (#2)
1 parent cf8bf12 commit 573acc5

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

docs/guide/event-system/index.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Event System
2+
3+
The Event System in PySpring provides a powerful way to implement event-driven architecture in your applications. It allows components to communicate in a decoupled manner through events, following the publish-subscribe pattern.
4+
5+
## Overview
6+
7+
The Event System consists of two main components:
8+
9+
1. `ApplicationEventPublisher`: The core component for publishing events
10+
2. `ApplicationEventHandlerRegistry`: Manages event handler registration and execution
11+
12+
## Key Features
13+
14+
- Thread-safe event publishing mechanism
15+
- Decorator-based event listener registration
16+
- Asynchronous event processing
17+
- Type-safe events using Pydantic models
18+
- Automatic event handler initialization
19+
- Integration with the component system
20+
21+
## Usage
22+
23+
### Creating Events
24+
25+
Events are defined as Pydantic models that inherit from `ApplicationEvent`:
26+
27+
```python
28+
from py_spring_core import ApplicationEvent
29+
30+
class MyEvent(ApplicationEvent):
31+
message: str
32+
```
33+
34+
### Publishing Events
35+
36+
To publish events, inject the `ApplicationEventPublisher` into your component:
37+
38+
```python
39+
from py_spring_core import ApplicationEventPublisher, Component
40+
41+
class MyComponent(Component):
42+
event_publisher: ApplicationEventPublisher
43+
44+
def do_something(self):
45+
self.event_publisher.publish(MyEvent(message="Hello World!"))
46+
```
47+
48+
### Subscribing to Events
49+
50+
Use the `@EventListener` decorator to subscribe to events. Event listeners must inherit from the `Component` class:
51+
52+
```python
53+
from py_spring_core import Component, EventListener
54+
55+
class MyListener(Component):
56+
@EventListener(MyEvent)
57+
def handle_event(self, event: MyEvent):
58+
print(f"Received event: {event.message}")
59+
```
60+
61+
The event listener will be automatically registered during application startup and will receive events of the specified type.
62+
63+
## Technical Details
64+
65+
### Event Processing
66+
67+
- Events are processed asynchronously in a dedicated thread
68+
- The system uses a thread-safe queue for event distribution
69+
- Event handlers are automatically registered during application startup
70+
- Events are validated using Pydantic models
71+
72+
73+
## Best Practices
74+
75+
1. **Event Design**
76+
- Keep events focused and specific
77+
- Use meaningful event names
78+
- Include only necessary data in events
79+
80+
2. **Event Handling**
81+
- Keep event handlers lightweight
82+
- Avoid long-running operations in event handlers
83+
- Use proper error handling in event handlers
84+
85+
## Example
86+
87+
Here's a complete example showing event publishing and handling:
88+
89+
```python
90+
from py_spring_core import (
91+
ApplicationEvent,
92+
ApplicationEventPublisher,
93+
EventListener,
94+
Component
95+
)
96+
97+
# Define an event
98+
class UserCreatedEvent(ApplicationEvent):
99+
user_id: str
100+
username: str
101+
102+
# Publisher component
103+
class UserService(Component):
104+
event_publisher: ApplicationEventPublisher
105+
106+
def create_user(self, username: str):
107+
user_id = "generated_id" # In real app, generate proper ID
108+
self.event_publisher.publish(UserCreatedEvent(
109+
user_id=user_id,
110+
username=username
111+
))
112+
return user_id
113+
114+
115+
class UserNotificationListener(Component):
116+
@EventListener(UserCreatedEvent)
117+
def send_welcome_email(self, event: UserCreatedEvent):
118+
print(f"Sending welcome email to: {event.username}")
119+
120+
class UserEventListener(Component):
121+
@EventListener(UserCreatedEvent)
122+
def handle_user_created(self, event: UserCreatedEvent):
123+
print(f"New user created: {event.username} (ID: {event.user_id})")
124+
125+
```
126+
127+
## Version Information
128+
129+
This feature is available in PySpring version 0.0.11 and later.
130+
131+
## Dependencies
132+
133+
The Event System is built into PySpring core and doesn't require additional dependencies.

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
- **Decorator-Based Route Mapping**: PySpring now supports a more declarative approach to route definition using decorators, similar to Spring's annotation-based routing. This provides a cleaner and more type-safe way to define API endpoints with better IDE support and code organization.
4343

44+
- **Event System**: PySpring provides a powerful event system that enables event-driven architecture in your applications. It features thread-safe event publishing, decorator-based event listeners, and asynchronous event processing. The system is fully integrated with the component system and provides type-safe events using Pydantic models.
45+
4446
### Getting Started
4547
To get started with **PySpring**, follow these steps:
4648

mkdocs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ nav:
3939
- Dependency Injection:
4040
- What is Dependency Injection: guide/dependency-injection/what-is-denpendency-injection.md
4141
- Auto Dependency Injection: guide/dependency-injection/auto-deppendency-injection.md
42+
- Event System:
43+
- Intro to Event System: guide/event-system/index.md
4244
- Scheduling:
4345
- Scheduler: guide/scheduling/scheduler.md
46+
4447

4548
markdown_extensions:
4649
- pymdownx.highlight:

0 commit comments

Comments
 (0)