Overview
Framefox provides a powerful event system that allows you to build reactive, decoupled applications. The event dispatcher enables components to communicate without tight coupling.Event System
The event system consists of:- EventDispatcher: Central hub for managing events and listeners
- Event Listeners: Functions or classes that respond to events
- Decorators: Convenient event dispatching in middleware
- Auto-loading: Automatic listener registration
Basic Usage
Dispatching Events
Adding Listeners
Event Listeners
Class-Based Listeners
Create organized listener classes:Auto-Loading Listeners
Framefox can automatically discover and register listeners:DispatchEvent Decorator
Use the@DispatchEvent decorator to automatically dispatch events in middleware:
- Before execution:
kernel.request_receivedwith{"request": request} - After execution:
kernel.request_completedwith{"response": response}
Built-in Events
Framefox dispatches several built-in events:Kernel Events
Worker Events
Listening to Built-in Events
Custom Events
Creating Custom Events
Define your own events for application logic:Multi-Listener Events
Multiple listeners can respond to the same event:Event Naming Conventions
Follow these conventions for event names:Advanced Patterns
Event Priority
Execute listeners in specific order by registering them sequentially:Asynchronous Event Handling
For long-running operations, dispatch to background tasks:Event Aggregation
Collect multiple events for batch processing:Best Practices
- Event Names: Use clear, descriptive names with entity.action format
- Payload Structure: Keep payloads simple and serializable
- Error Handling: Wrap listener code in try-except blocks
- Performance: Keep listeners fast; use background tasks for heavy work
- Testing: Test listeners independently from event dispatching
- Documentation: Document available events and their payload structure