#pragma once #include "ConfigTypes.hpp" #include "PluginTypes.hpp" #include "TrackTypes.hpp" #include "Types.hpp" #include #include #include #include namespace Echoion::Api { // Event handler type template using EventHandler = std::function; // Subscription handle. Wraps the underlying Void::EventBus handler id so // existing call sites that hold a token keep compiling; pass it back to // EventBus::unsubscribe() to detach. class SubscriptionToken { public: using Id = Void::EventBus::EventBus::HandlerId; SubscriptionToken() = default; explicit SubscriptionToken(Id id) : id_(id), valid_(true) {} [[nodiscard]] bool isValid() const { return valid_; } [[nodiscard]] Id getId() const { return id_; } private: Id id_ = 0; bool valid_ = false; }; // Type-keyed publish/subscribe bus. Delegates to Void::EventBus in // asynchronous mode: publish() snapshots handlers and dispatches them on a // background worker, so handlers that do I/O (RPC broadcast, DB writes) // never block the publishing thread. class EventBus { public: EventBus() = default; EventBus(const EventBus&) = delete; EventBus& operator=(const EventBus&) = delete; EventBus(EventBus&&) = delete; EventBus& operator=(EventBus&&) = delete; // The Void async worker runs from construction; kept for API // compatibility. stop() drains any in-flight dispatch. void start() {} void stop() { bus_.flush(); } template void publish(T event) { bus_.publish(event); } template SubscriptionToken subscribe(EventHandler handler) { return SubscriptionToken{bus_.subscribe(std::move(handler))}; } void unsubscribe(const SubscriptionToken& token) { if (token.isValid()) { bus_.unsubscribe(token.getId()); } } // Block until every event published before this call is delivered. void flush() { bus_.flush(); } private: Void::EventBus::EventBus bus_{Void::EventBus::Mode::Async}; }; // Global event bus singleton class GlobalEventBus { public: [[nodiscard]] static EventBus& instance(); private: GlobalEventBus() = default; }; // Common event types for Echoion namespace Events { // Playback events struct PlaybackStarted { TrackInfo track; uint64_t clientId; std::chrono::milliseconds position; }; struct PlaybackPaused { TrackInfo track; std::chrono::milliseconds position; }; struct PlaybackResumed { TrackInfo track; std::chrono::milliseconds position; }; struct PlaybackStopped { std::optional lastTrack; std::chrono::milliseconds finalPosition; }; struct PlaybackPositionChanged { TrackInfo track; std::chrono::milliseconds position; std::chrono::milliseconds duration; float progressPercent; // 0.0 - 100.0 }; struct TrackFinished { TrackInfo track; bool autoSkipped; std::chrono::milliseconds finalPosition; }; struct VolumeChanged { float oldVolume; float newVolume; uint64_t changedByClientId; }; struct ShuffleChanged { bool enabled; uint64_t changedByClientId; }; struct RepeatChanged { bool enabled; uint64_t changedByClientId; }; // Queue events struct TrackQueued { TrackInfo track; uint64_t queueId; uint64_t addedByClientId; uint32_t position; uint32_t queueLength; }; struct TrackRemoved { TrackInfo track; uint64_t queueId; uint64_t removedByClientId; uint32_t queueLength; }; struct QueueCleared { uint64_t clearedByClientId; uint32_t previousLength; }; struct QueueShuffled { uint64_t triggeredByClientId; }; struct QueueReordered { uint64_t queueId; uint32_t oldPosition; uint32_t newPosition; uint64_t changedByClientId; }; // Playlist events struct PlaylistLoadStarted { std::string playlistId; uint64_t requestedByClientId; uint64_t databasePlaylistId; }; struct PlaylistTrackLoaded { std::string playlistId; TrackInfo track; uint32_t loadedCount; uint32_t totalCount; uint64_t databasePlaylistId; }; struct PlaylistLoadCompleted { std::string playlistId; uint32_t totalTracksLoaded; bool success; std::optional error; }; // Subscription events struct UserSubscribed { std::string uid; std::string nickname; uint64_t clientId; }; struct UserUnsubscribed { std::string uid; std::string nickname; uint64_t clientId; }; // Connection events struct ClientConnected { ClientInfo client; }; struct ClientDisconnected { uint64_t clientId; std::string reason; }; struct ChannelChanged { uint64_t clientId; uint64_t oldChannelId; uint64_t newChannelId; }; // Error events struct PlaybackError { ErrorCode code; std::string message; std::optional track; }; struct NetworkError { std::string url; ErrorCode code; std::string message; }; // Configuration events struct ConfigReloaded { BotConfig newConfig; }; struct ConfigChanged { std::string key; std::string oldValue; std::string newValue; }; // Cache events struct CacheHit { std::string key; std::string type; }; struct CacheMiss { std::string key; std::string type; }; struct CacheEvicted { std::string key; std::string reason; }; // Database persistence events (for audit/logging) struct DatabasePersisted { std::string entityType; // "queue", "playback", "subscription", etc. std::string operation; // "insert", "update", "delete" bool success; std::optional error; }; } // namespace Events // Helper macros for event publishing/subscribing #define ECHOION_PUBLISH(event) \ Echoion::Api::GlobalEventBus::instance().publish(event) #define ECHOION_SUBSCRIBE(type, handler) \ Echoion::Api::GlobalEventBus::instance().subscribe(handler) } // namespace Echoion::Api