From 317cef378228734f0709c0050f13ee0447fa0341 Mon Sep 17 00:00:00 2001 From: faumaray Date: Tue, 23 Jun 2026 13:56:32 +0300 Subject: [PATCH] Drop the EventBus wrapper; keep only the domain event structs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace eventbus/EventBus.hpp (the Api::EventBus wrapper, GlobalEventBus singleton, SubscriptionToken, and the unused ECHOION_PUBLISH/SUBSCRIBE macros) with eventbus/Events.hpp — just the Events:: structs. The bus is now Void::EventBus::EventBus, owned by Core. Co-Authored-By: Claude Opus 4.8 --- include/echoion/api.hpp | 2 +- include/echoion/api/eventbus/EventBus.hpp | 292 ---------------------- include/echoion/api/eventbus/Events.hpp | 200 +++++++++++++++ 3 files changed, 201 insertions(+), 293 deletions(-) delete mode 100644 include/echoion/api/eventbus/EventBus.hpp create mode 100644 include/echoion/api/eventbus/Events.hpp diff --git a/include/echoion/api.hpp b/include/echoion/api.hpp index cb24d44..11bfd64 100644 --- a/include/echoion/api.hpp +++ b/include/echoion/api.hpp @@ -27,7 +27,7 @@ // Core systems (for Core implementation only) #include "api/database/Database.hpp" -#include "api/eventbus/EventBus.hpp" +#include "api/eventbus/Events.hpp" // Export macros #include "api/interfaces/Export.hpp" diff --git a/include/echoion/api/eventbus/EventBus.hpp b/include/echoion/api/eventbus/EventBus.hpp deleted file mode 100644 index ea04ddc..0000000 --- a/include/echoion/api/eventbus/EventBus.hpp +++ /dev/null @@ -1,292 +0,0 @@ -#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 diff --git a/include/echoion/api/eventbus/Events.hpp b/include/echoion/api/eventbus/Events.hpp new file mode 100644 index 0000000..032f1f4 --- /dev/null +++ b/include/echoion/api/eventbus/Events.hpp @@ -0,0 +1,200 @@ +#pragma once + +// Domain event types published on the application event bus. The bus itself +// is Void::EventBus::EventBus (owned by Core); there is no echoion wrapper. + +#include "ConfigTypes.hpp" +#include "PluginTypes.hpp" +#include "TrackTypes.hpp" +#include "Types.hpp" +#include +#include +#include +#include + +namespace Echoion::Api::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", ... + std::string operation; // "insert", "update", "delete" + bool success; + std::optional error; + }; + +} // namespace Echoion::Api::Events