Initial commit
This commit is contained in:
commit
1e174a0789
24 files changed
+5499
No files matched your search
@@ -0,0 +1,413 @@
|
||||
# Event System
|
||||
|
||||
## Overview
|
||||
|
||||
Echoion uses a publish-subscribe event system for decoupled communication between components. The EventBus allows components to communicate without direct dependencies.
|
||||
|
||||
## Event System Architecture
|
||||
|
||||
```mermaid
|
||||
flowchart TB
|
||||
subgraph "Publishers"
|
||||
MM[MusicManager]
|
||||
AD[AudioDecoder]
|
||||
NC[NetworkClient]
|
||||
YC[YouTubeClient]
|
||||
end
|
||||
|
||||
subgraph "EventBus"
|
||||
Queue[Event Queue]
|
||||
Router[Event Router]
|
||||
end
|
||||
|
||||
subgraph "Subscribers"
|
||||
CM[CommandManager]
|
||||
PI[PluginInterface]
|
||||
RS[RpcServer]
|
||||
LG[Logger]
|
||||
DB[Database]
|
||||
end
|
||||
|
||||
MM -->|TrackQueued| Queue
|
||||
AD -->|PlaybackError| Queue
|
||||
NC -->|NetworkError| Queue
|
||||
YC -->|TrackResolved| Queue
|
||||
|
||||
Queue --> Router
|
||||
Router --> CM
|
||||
Router --> PI
|
||||
Router --> RS
|
||||
Router --> LG
|
||||
Router --> DB
|
||||
```
|
||||
|
||||
## Event Flow
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant Pub as Publisher
|
||||
participant Bus as EventBus
|
||||
participant Q as Queue
|
||||
participant D as Dispatcher
|
||||
participant Sub1 as Subscriber 1
|
||||
participant Sub2 as Subscriber 2
|
||||
|
||||
Pub->>Bus: publish(event)
|
||||
Bus->>Q: enqueue(event)
|
||||
|
||||
loop Event Processing Thread
|
||||
Q->>D: dequeue()
|
||||
D->>D: Lookup handlers
|
||||
par Parallel Dispatch
|
||||
D->>Sub1: handle(event)
|
||||
D->>Sub2: handle(event)
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
## Event Types
|
||||
|
||||
### Playback Events
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class PlaybackStarted {
|
||||
+TrackInfo track
|
||||
+uint64 clientId
|
||||
+timestamp
|
||||
}
|
||||
|
||||
class PlaybackPaused {
|
||||
+duration position
|
||||
+timestamp
|
||||
}
|
||||
|
||||
class PlaybackResumed {
|
||||
+duration position
|
||||
+timestamp
|
||||
}
|
||||
|
||||
class PlaybackStopped {
|
||||
+timestamp
|
||||
}
|
||||
|
||||
class TrackFinished {
|
||||
+TrackInfo track
|
||||
+bool autoSkipped
|
||||
+timestamp
|
||||
}
|
||||
|
||||
class VolumeChanged {
|
||||
+float oldVolume
|
||||
+float newVolume
|
||||
+uint64 changedBy
|
||||
}
|
||||
```
|
||||
|
||||
### Queue Events
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class TrackQueued {
|
||||
+TrackInfo track
|
||||
+uint64 queueId
|
||||
+uint64 addedBy
|
||||
+uint32 position
|
||||
}
|
||||
|
||||
class TrackRemoved {
|
||||
+uint64 queueId
|
||||
+uint64 removedBy
|
||||
}
|
||||
|
||||
class QueueCleared {
|
||||
+uint64 clearedBy
|
||||
}
|
||||
|
||||
class QueueShuffled {
|
||||
+uint64 triggeredBy
|
||||
}
|
||||
```
|
||||
|
||||
### Connection Events
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class ClientConnected {
|
||||
+ClientInfo client
|
||||
+timestamp
|
||||
}
|
||||
|
||||
class ClientDisconnected {
|
||||
+uint64 clientId
|
||||
+string reason
|
||||
+timestamp
|
||||
}
|
||||
|
||||
class ChannelChanged {
|
||||
+uint64 clientId
|
||||
+uint64 oldChannelId
|
||||
+uint64 newChannelId
|
||||
}
|
||||
```
|
||||
|
||||
### Error Events
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class PlaybackError {
|
||||
+ErrorCode code
|
||||
+string message
|
||||
+TrackInfo track
|
||||
}
|
||||
|
||||
class NetworkError {
|
||||
+string url
|
||||
+ErrorCode code
|
||||
+string message
|
||||
}
|
||||
|
||||
class DecodeError {
|
||||
+string source
|
||||
+string error
|
||||
}
|
||||
```
|
||||
|
||||
### Configuration Events
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class ConfigReloaded {
|
||||
+BotConfig newConfig
|
||||
}
|
||||
|
||||
class ConfigChanged {
|
||||
+string key
|
||||
+string oldValue
|
||||
+string newValue
|
||||
}
|
||||
|
||||
class HotReloadCompleted {
|
||||
+bool success
|
||||
+string version
|
||||
}
|
||||
```
|
||||
|
||||
## EventBus Structure
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class EventBus {
|
||||
+publish(event)
|
||||
+subscribe(handler) SubscriptionToken
|
||||
+unsubscribe(token)
|
||||
+processOne()
|
||||
+start()
|
||||
+stop()
|
||||
}
|
||||
|
||||
class SubscriptionToken {
|
||||
+Id id
|
||||
+type_index type
|
||||
+isValid()
|
||||
}
|
||||
|
||||
class IEvent {
|
||||
+type_index getType()
|
||||
+timestamp
|
||||
}
|
||||
|
||||
class HandlerRegistry {
|
||||
-Map~type_index, Handlers~ handlers
|
||||
+register(type, handler)
|
||||
+dispatch(event)
|
||||
}
|
||||
|
||||
EventBus --> HandlerRegistry
|
||||
EventBus --> SubscriptionToken
|
||||
SubscriptionToken --> IEvent
|
||||
```
|
||||
|
||||
## Usage Patterns
|
||||
|
||||
### Publishing Events
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
Component[Component] --> Create[Create Event]
|
||||
Create --> Publish["ECHOION_PUBLISH(event)"]
|
||||
Publish --> Bus[EventBus]
|
||||
Bus --> Queue[Queue Event]
|
||||
```
|
||||
|
||||
### Subscribing to Events
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
Component[Component] --> Subscribe["ECHOION_SUBscribe(EventType, handler)"]
|
||||
Subscribe --> Token[Get Token]
|
||||
Token --> Store[Store Token]
|
||||
Store --> Receive[Receive Events]
|
||||
```
|
||||
|
||||
### Unsubscription
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
Token[SubscriptionToken] -->|RAII| Auto[Auto-unsubscribe<br/>on destruction]
|
||||
Token -->|Explicit| Manual[call unsubscribe()]
|
||||
```
|
||||
|
||||
## Event Flow Examples
|
||||
|
||||
### Track Playback Flow
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant User
|
||||
participant CM as CommandManager
|
||||
participant MM as MusicManager
|
||||
participant Bus as EventBus
|
||||
participant PI as PluginInterface
|
||||
participant RS as RpcServer
|
||||
participant DB as Database
|
||||
|
||||
User->>CM: !play song
|
||||
CM->>MM: queueTrack()
|
||||
MM->>Bus: publish(TrackQueued)
|
||||
|
||||
Bus->>PI: TrackQueued
|
||||
PI->>User: Reply "Added to queue"
|
||||
|
||||
Bus->>RS: TrackQueued
|
||||
RS->>RS: Broadcast to Desktop
|
||||
|
||||
MM->>MM: Start playback
|
||||
MM->>Bus: publish(PlaybackStarted)
|
||||
|
||||
Bus->>PI: PlaybackStarted
|
||||
Bus->>RS: PlaybackStarted
|
||||
Bus->>DB: PlaybackStarted
|
||||
|
||||
DB->>DB: Record in history
|
||||
```
|
||||
|
||||
### Error Handling Flow
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant AD as AudioDecoder
|
||||
participant Bus as EventBus
|
||||
participant MM as MusicManager
|
||||
participant PI as PluginInterface
|
||||
participant LG as Logger
|
||||
|
||||
AD->>AD: decodeFrame() fails
|
||||
AD->>Bus: publish(PlaybackError)
|
||||
|
||||
Bus->>MM: PlaybackError
|
||||
MM->>MM: Skip to next track
|
||||
|
||||
Bus->>PI: PlaybackError
|
||||
PI->>PI: Send error to channel
|
||||
|
||||
Bus->>LG: PlaybackError
|
||||
LG->>LG: Log detailed error
|
||||
```
|
||||
|
||||
## Thread Safety
|
||||
|
||||
```mermaid
|
||||
flowchart TB
|
||||
subgraph "Thread-Safe Operations"
|
||||
Publish[Publish from any thread]
|
||||
Subscribe[Subscribe from any thread]
|
||||
Unsubscribe[Unsubscribe from any thread]
|
||||
end
|
||||
|
||||
subgraph "Internal Protection"
|
||||
QueueMutex[Queue Mutex]
|
||||
HandlerMutex[Handler Registry<br/>Shared Mutex]
|
||||
end
|
||||
|
||||
Publish --> QueueMutex
|
||||
Subscribe --> HandlerMutex
|
||||
Unsubscribe --> HandlerMutex
|
||||
```
|
||||
|
||||
## Event Processing Modes
|
||||
|
||||
```mermaid
|
||||
flowchart TB
|
||||
subgraph "Processing Options"
|
||||
Async[Asynchronous<br/>Queued processing]
|
||||
Direct[Direct<br/>Immediate call]
|
||||
Ordered[Ordered<br/>Sequential dispatch]
|
||||
end
|
||||
|
||||
Async -->|Default| LowLatency[Low Publisher Latency]
|
||||
Direct -->|Optional| Immediate[Immediate Response]
|
||||
Ordered -->|Guaranteed| Sequence[Event Order Preserved]
|
||||
```
|
||||
|
||||
## Comparison: EventBus vs Plugin Events
|
||||
|
||||
| Feature | EventBus | PluginEvent |
|
||||
|---------|----------|-------------|
|
||||
| Purpose | Internal communication | Plugin ↔ Core |
|
||||
| Type-safe | Yes (templates) | Yes (enum) |
|
||||
| Decoupled | Full | Partial |
|
||||
| Thread-safe | Yes | Yes |
|
||||
| Persistence | No | No |
|
||||
| Use case | Component events | User actions |
|
||||
|
||||
## Best Practices
|
||||
|
||||
```mermaid
|
||||
flowchart TB
|
||||
subgraph "Best Practices"
|
||||
Fast[Keep handlers fast]
|
||||
Unsub[Unsubscribe when done]
|
||||
RAII[Use RAII for tokens]
|
||||
Circular[Avoid circular deps]
|
||||
end
|
||||
|
||||
Fast -->|Offload| Async[Async operations]
|
||||
Unsub -->|Prevent| Leaks[Memory leaks]
|
||||
RAII -->|Auto| Cleanup[Auto-cleanup]
|
||||
Circular -->|Ensure| DAG[DAG structure]
|
||||
```
|
||||
|
||||
1. **Keep handlers fast** - Long operations should be offloaded
|
||||
2. **Unsubscribe when done** - Prevents memory leaks
|
||||
3. **Use RAII** - Store tokens as members for auto-cleanup
|
||||
4. **Avoid circular dependencies** - Events should flow in one direction
|
||||
|
||||
## Debugging Events
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
subgraph "Debugging Tools"
|
||||
Logger[Event Logger<br/>Log all events]
|
||||
Counter[Event Counter<br/>Track rate]
|
||||
Inspector[Event Inspector<br/>View details]
|
||||
end
|
||||
|
||||
Bus --> Logger
|
||||
Bus --> Counter
|
||||
Bus --> Inspector
|
||||
```
|
||||
|
||||
## Creating Custom Events
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
Define[Define Struct] --> Publish[Publish Event]
|
||||
Publish --> Subscribe[Subscribe Handler]
|
||||
Subscribe --> Handle[Handle Event]
|
||||
```
|
||||
|
||||
1. Define event struct with timestamp
|
||||
2. Publish using `ECHOION_PUBLISH(event)`
|
||||
3. Subscribe using `ECHOION_SUBSCRIBE(EventType, handler)`
|
||||
4. Handle in callback function
|
||||
Reference in new issue
Block a user