Overview
DefaultEventBus is the synchronous implementation of the EventBus trait. Events are queued when published and processed in batch when drain() is called.
Location: crates/oneclaw-core/src/event_bus/bus.rs:19
Design Pattern
The drain pattern separates event publishing from event processing:- Publish: Events are added to a queue
- Drain: All pending events are dispatched to matching handlers
- History: Processed events are stored in a ring buffer
Usage
Basic Example
Configuration
Location:crates/oneclaw-core/src/event_bus/bus.rs:32
The Drain Pattern
Location:crates/oneclaw-core/src/event_bus/bus.rs:106
How Drain Works
- Take events: Atomically drain the queue
- Match handlers: Find subscriptions matching each event’s topic
- Execute handlers: Run handler callbacks, collecting response events
- Store history: Add processed events to ring buffer
- Publish responses: Queue any events returned by handlers
Response Events
Handlers can generate new events by returningSome(Event):
drain() call.
Topic Matching
Location:crates/oneclaw-core/src/event_bus/bus.rs:49
Event History
Location:crates/oneclaw-core/src/event_bus/bus.rs:160
Processed events are stored in a ring buffer for debugging:
max_history (default 100). Oldest events are removed when capacity is reached.
Synchronous Event Processing
When to Use DefaultEventBus
- Control flow: Need explicit control over when events are processed
- Testing: Predictable event processing in tests
- Batch operations: Process events in batches for efficiency
- Event loop integration: Fits well with tick-based runtime loops
Runtime Integration
Typical integration with OneClaw Runtime:Thread Safety
Location:crates/oneclaw-core/src/event_bus/bus.rs:19
All internal state uses Mutex for thread-safe access:
Use Cases
Device Event Processing
Alert Generation
Performance Considerations
- Queue overhead: Events are stored in a
Vec, cheap to append - Drain cost: O(events × subscriptions × pattern_match)
- History limit: Keep
max_historyreasonable (default 100) - Handler efficiency: Avoid blocking operations in handlers
Related
- EventBus Trait - Core interface
- AsyncEventBus - Async alternative
- Event Pipelines - Declarative event processing