Own the event bus in Core; inject it into MusicManager

Core holds a Void::EventBus::EventBus (Async), provides it as a service, and
runs its 18 event subscriptions against it directly (setupEventBusSubscriptions
is no longer const since it now mutates the owned bus). MusicManager gets the
bus via setEventBus() and publishes through a small emit<T>() helper (14
former GlobalEventBus publish sites). Delete EventBus.cpp (the singleton
definition). Build green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 4.8 committed 2026-06-23 13:56:32 +03:00
1 parent 9d56a6ff01
commit f7553d10d3
5 files changed
+63 -57

No files matched your search

+7 -1
View File
@@ -7,6 +7,7 @@
#include "RpcServer.hpp"
#include <ConfigTypes.hpp>
#include <CoreInterface.hpp>
#include <Void/Async/EventBus.hpp>
#include <Database.hpp>
#include <RpcTypes.hpp>
#include <StateBlob.hpp>
@@ -150,7 +151,7 @@ namespace Echoion::Core
[[nodiscard]] bool isAdmin(uint64_t clientId) const;
void updateWhisperList();
void setupRpcHandlers();
void setupEventBusSubscriptions() const noexcept;
void setupEventBusSubscriptions() noexcept;
// Components
std::shared_ptr<NetworkClient> networkClient_;
@@ -186,6 +187,11 @@ namespace Echoion::Core
// shims over the same instances.
Void::Service::ServiceRegistry registry_;
// Application event bus (async): playback/queue/subscription events
// published by MusicManager and consumed here for RPC broadcast +
// DB persistence. Owned by Core, provided as a service.
Void::EventBus::EventBus eventBus_{Void::EventBus::Mode::Async};
public:
// Access the service registry (composition root).
+15
View File
@@ -9,6 +9,7 @@
#include <ConfigTypes.hpp>
#include <TrackTypes.hpp>
#include <Types.hpp>
#include <Void/Async/EventBus.hpp>
#include <atomic>
#include <condition_variable>
#include <filesystem>
@@ -36,6 +37,10 @@ namespace Echoion::Core
std::shared_ptr<Api::Database> database = nullptr);
void shutdown();
// Inject the application event bus (owned by Core). Must be set
// before playback so emitted events reach subscribers.
void setEventBus(Void::EventBus::EventBus* bus) { eventBus_ = bus; }
[[nodiscard]] Api::Result<std::string>
play(const std::string& query, uint64_t clientId);
[[nodiscard]] Api::Result<void> pause();
@@ -157,6 +162,14 @@ namespace Echoion::Core
private:
// Publish a domain event on the injected bus (no-op until set).
template <typename T> void emit(T event)
{
if (eventBus_) {
eventBus_->publish<T>(std::move(event));
}
}
void playbackThread();
[[nodiscard]] Api::Result<Api::TrackInfo>
resolveQuery(const std::string& query);
@@ -207,6 +220,8 @@ namespace Echoion::Core
std::thread playlistAutoStartThread_;
std::shared_ptr<NetworkClient> networkClient_;
// Non-owning application event bus (provided by Core).
Void::EventBus::EventBus* eventBus_ = nullptr;
std::unique_ptr<YouTubeClient> youTubeClient_;
std::unique_ptr<AudioDecoder> audioDecoder_;
std::unique_ptr<SubscriptionManager> subscriptionManager_;
+26 -27
View File
@@ -6,7 +6,7 @@
#include "echoion/core/state/StateVersions.hpp"
#include <ConfigManager.hpp>
#include <Database.hpp>
#include <EventBus.hpp>
#include <Events.hpp>
#include <Void/Data/Json.hpp>
#include <Void/Data/Reflect.hpp>
#include <algorithm>
@@ -80,9 +80,8 @@ namespace Echoion::Core
ECHOION_LOG_INFO("Initializing Echoion Core...");
// Start EventBus processing thread
Api::GlobalEventBus::instance().start();
ECHOION_LOG_INFO("EventBus started");
// The async event bus runs from construction; nothing to start.
ECHOION_LOG_INFO("EventBus ready");
// Initialize network client
networkClient_ = std::make_shared<NetworkClient>();
@@ -110,6 +109,7 @@ namespace Echoion::Core
// Initialize music manager
musicManager_ = std::make_shared<MusicManager>();
musicManager_->setEventBus(&eventBus_);
auto musicResult = musicManager_->initialize(networkClient_, database_);
if (!musicResult) {
return std::unexpected(musicResult.error());
@@ -307,8 +307,7 @@ namespace Echoion::Core
}
registry_.provide<Api::ConfigManager>(
alias(&Api::ConfigManager::instance()));
registry_.provide<Api::EventBus>(
alias(&Api::GlobalEventBus::instance()));
registry_.provide<Void::EventBus::EventBus>(alias(&eventBus_));
if (networkClient_) {
registry_.provide<Api::INetworkClient>(networkClient_);
}
@@ -395,8 +394,8 @@ namespace Echoion::Core
database_.reset();
}
// Stop EventBus
Api::GlobalEventBus::instance().stop();
// Drain any in-flight event dispatch.
eventBus_.flush();
ECHOION_LOG_INFO("EventBus stopped");
// Clear callbacks
@@ -1796,13 +1795,13 @@ namespace Echoion::Core
ECHOION_LOG_INFO("RPC handlers registered");
}
void Core::setupEventBusSubscriptions() const noexcept
void Core::setupEventBusSubscriptions() noexcept
{
using namespace Api::Events;
// Subscribe to playback events for RPC broadcasting and database
// persistence
Api::GlobalEventBus::instance().subscribe<PlaybackStarted>(
eventBus_.subscribe<PlaybackStarted>(
[this](const PlaybackStarted& evt) mutable {
// Broadcast to RPC/WebSocket clients
if (rpcServer_) {
@@ -1822,7 +1821,7 @@ namespace Echoion::Core
}
});
Api::GlobalEventBus::instance().subscribe<PlaybackPaused>(
eventBus_.subscribe<PlaybackPaused>(
[this](const PlaybackPaused& evt) mutable {
if (rpcServer_) {
std::string data = std::format("{{\"position\":{}}}",
@@ -1831,7 +1830,7 @@ namespace Echoion::Core
}
});
Api::GlobalEventBus::instance().subscribe<PlaybackResumed>(
eventBus_.subscribe<PlaybackResumed>(
[this](const PlaybackResumed& evt) mutable {
if (rpcServer_) {
std::string data = std::format("{{\"position\":{}}}",
@@ -1840,14 +1839,14 @@ namespace Echoion::Core
}
});
Api::GlobalEventBus::instance().subscribe<PlaybackStopped>(
eventBus_.subscribe<PlaybackStopped>(
[this](const PlaybackStopped& evt) mutable {
if (rpcServer_) {
rpcServer_->broadcastEvent("playback.stopped", "{}");
}
});
Api::GlobalEventBus::instance().subscribe<PlaybackPositionChanged>(
eventBus_.subscribe<PlaybackPositionChanged>(
[this](const PlaybackPositionChanged& evt) mutable {
if (rpcServer_) {
std::string data =
@@ -1859,7 +1858,7 @@ namespace Echoion::Core
}
});
Api::GlobalEventBus::instance().subscribe<TrackFinished>(
eventBus_.subscribe<TrackFinished>(
[this](const TrackFinished& evt) mutable {
if (rpcServer_) {
rpcServer_->broadcastEvent("playback.finished", "{}");
@@ -1873,7 +1872,7 @@ namespace Echoion::Core
});
// Volume and playback mode changes
Api::GlobalEventBus::instance().subscribe<VolumeChanged>(
eventBus_.subscribe<VolumeChanged>(
[this](const VolumeChanged& evt) mutable {
if (rpcServer_) {
std::string data =
@@ -1882,7 +1881,7 @@ namespace Echoion::Core
}
});
Api::GlobalEventBus::instance().subscribe<ShuffleChanged>(
eventBus_.subscribe<ShuffleChanged>(
[this](const ShuffleChanged& evt) mutable {
if (rpcServer_) {
std::string data = std::format(
@@ -1891,7 +1890,7 @@ namespace Echoion::Core
}
});
Api::GlobalEventBus::instance().subscribe<RepeatChanged>(
eventBus_.subscribe<RepeatChanged>(
[this](const RepeatChanged& evt) mutable {
if (rpcServer_) {
std::string data = std::format(
@@ -1901,7 +1900,7 @@ namespace Echoion::Core
});
// Queue events
Api::GlobalEventBus::instance().subscribe<TrackQueued>(
eventBus_.subscribe<TrackQueued>(
[this](const TrackQueued& evt) mutable {
if (rpcServer_) {
std::string data = std::format(
@@ -1913,7 +1912,7 @@ namespace Echoion::Core
}
});
Api::GlobalEventBus::instance().subscribe<TrackRemoved>(
eventBus_.subscribe<TrackRemoved>(
[this](const TrackRemoved& evt) mutable {
if (rpcServer_) {
std::string data =
@@ -1923,14 +1922,14 @@ namespace Echoion::Core
}
});
Api::GlobalEventBus::instance().subscribe<QueueCleared>(
eventBus_.subscribe<QueueCleared>(
[this](const QueueCleared& evt) mutable {
if (rpcServer_) {
rpcServer_->broadcastEvent("queue.cleared", "{}");
}
});
Api::GlobalEventBus::instance().subscribe<QueueReordered>(
eventBus_.subscribe<QueueReordered>(
[this](const QueueReordered& evt) mutable {
if (rpcServer_) {
std::string data = std::format(
@@ -1942,7 +1941,7 @@ namespace Echoion::Core
});
// Playlist events - save to database via event bus using external ID
Api::GlobalEventBus::instance().subscribe<PlaylistLoadStarted>(
eventBus_.subscribe<PlaylistLoadStarted>(
[this](const PlaylistLoadStarted& evt) mutable {
if (rpcServer_) {
std::string data = std::string("{\"playlistId\":\"") +
@@ -1957,7 +1956,7 @@ namespace Echoion::Core
}
});
Api::GlobalEventBus::instance().subscribe<PlaylistTrackLoaded>(
eventBus_.subscribe<PlaylistTrackLoaded>(
[this](const PlaylistTrackLoaded& evt) mutable {
if (rpcServer_) {
std::string data =
@@ -1979,7 +1978,7 @@ namespace Echoion::Core
}
});
Api::GlobalEventBus::instance().subscribe<PlaylistLoadCompleted>(
eventBus_.subscribe<PlaylistLoadCompleted>(
[this](const PlaylistLoadCompleted& evt) mutable {
if (rpcServer_) {
std::string data =
@@ -1993,7 +1992,7 @@ namespace Echoion::Core
});
// Subscription events
Api::GlobalEventBus::instance().subscribe<UserSubscribed>(
eventBus_.subscribe<UserSubscribed>(
[this](const UserSubscribed& evt) mutable {
if (rpcServer_) {
std::string data =
@@ -2003,7 +2002,7 @@ namespace Echoion::Core
}
});
Api::GlobalEventBus::instance().subscribe<UserUnsubscribed>(
eventBus_.subscribe<UserUnsubscribed>(
[this](const UserUnsubscribed& evt) mutable {
if (rpcServer_) {
std::string data =
-14
View File
@@ -1,14 +0,0 @@
#include <EventBus.hpp>
namespace Echoion::Api
{
// Global event bus instance. The EventBus type is otherwise header-only
// (it delegates to Void::EventBus); only the singleton lives here.
EventBus& GlobalEventBus::instance()
{
static EventBus instance;
return instance;
}
} // namespace Echoion::Api
+15 -15
View File
@@ -1,7 +1,7 @@
#include "ConfigManager.hpp"
#include "LoggerInterface.hpp"
#include "echoion/core/MusicManager.hpp"
#include <EventBus.hpp>
#include <Events.hpp>
#include <algorithm>
#include <execution>
#include <filesystem>
@@ -396,7 +396,7 @@ namespace Echoion::Core
ECHOION_LOG_INFO("Playback stopped");
// Publish playback stopped event
Api::GlobalEventBus::instance().publish(Api::Events::PlaybackStopped{
emit(Api::Events::PlaybackStopped{
.lastTrack = currentTrack_ ? std::optional(currentTrack_->track)
: std::nullopt,
.finalPosition = playbackPosition_});
@@ -493,7 +493,7 @@ namespace Echoion::Core
ECHOION_LOG_INFO(std::format("Volume set to {}", volume));
// Publish volume changed event
Api::GlobalEventBus::instance().publish(Api::Events::VolumeChanged{
emit(Api::Events::VolumeChanged{
.oldVolume = oldVolume,
.newVolume = volume,
.changedByClientId = 0 // Will be set by caller if available
@@ -663,7 +663,7 @@ namespace Echoion::Core
ECHOION_LOG_INFO(std::format("Removed track at position {}", position));
// Publish track removed event
Api::GlobalEventBus::instance().publish(Api::Events::TrackRemoved{
emit(Api::Events::TrackRemoved{
.track = removedTrack.track,
.queueId = removedTrack.queueId,
.removedByClientId = 0,
@@ -687,7 +687,7 @@ namespace Echoion::Core
ECHOION_LOG_INFO("Queue cleared");
// Publish queue cleared event
Api::GlobalEventBus::instance().publish(Api::Events::QueueCleared{
emit(Api::Events::QueueCleared{
.clearedByClientId = 0, .previousLength = previousLength});
return {};
@@ -738,7 +738,7 @@ namespace Echoion::Core
ECHOION_LOG_INFO(std::format("Moved track from {} to {}", from, to));
// Publish queue reordered event
Api::GlobalEventBus::instance().publish(
emit(
Api::Events::QueueReordered{.queueId = entry.queueId,
.oldPosition = from,
.newPosition = to,
@@ -768,7 +768,7 @@ namespace Echoion::Core
}
// Publish shuffle changed event
Api::GlobalEventBus::instance().publish(Api::Events::ShuffleChanged{
emit(Api::Events::ShuffleChanged{
.enabled = shuffle_, .changedByClientId = 0});
return shuffle_;
@@ -815,7 +815,7 @@ namespace Echoion::Core
ECHOION_LOG_INFO(repeat_ ? "Repeat enabled" : "Repeat disabled");
// Publish repeat changed event
Api::GlobalEventBus::instance().publish(Api::Events::RepeatChanged{
emit(Api::Events::RepeatChanged{
.enabled = repeat_, .changedByClientId = 0});
return repeat_;
@@ -865,7 +865,7 @@ namespace Echoion::Core
std::format("Starting async playlist load: {}", playlistId));
// Publish playlist load started event
Api::GlobalEventBus::instance().publish(
emit(
Api::Events::PlaylistLoadStarted{.playlistId = playlistId,
.requestedByClientId = clientId});
@@ -899,7 +899,7 @@ namespace Echoion::Core
added.fetch_add(1);
// Publish playlist track loaded event
Api::GlobalEventBus::instance().publish(
emit(
Api::Events::PlaylistTrackLoaded{
.playlistId = playlistId,
.track = track,
@@ -954,7 +954,7 @@ namespace Echoion::Core
playlistLoading_ = false;
// Publish playlist load completed event
Api::GlobalEventBus::instance().publish(
emit(
Api::Events::PlaylistLoadCompleted{
.playlistId = playlistId,
.totalTracksLoaded = added.load(),
@@ -1427,7 +1427,7 @@ namespace Echoion::Core
100.0f;
}
Api::GlobalEventBus::instance().publish(
emit(
Api::Events::PlaybackPositionChanged{
.track = currentTrack_->track,
.position = position,
@@ -1723,7 +1723,7 @@ namespace Echoion::Core
// Publish track finished event
if (currentTrack_.has_value()) {
Api::GlobalEventBus::instance().publish(Api::Events::TrackFinished{
emit(Api::Events::TrackFinished{
.track = currentTrack_->track,
.autoSkipped = false,
.finalPosition = getPlaybackPosition()});
@@ -1928,7 +1928,7 @@ Subscriptions are persistent across server reconnects.)";
}
// Publish subscription event
Api::GlobalEventBus::instance().publish(Api::Events::UserSubscribed{
emit(Api::Events::UserSubscribed{
.uid = uid, .nickname = nickname, .clientId = clientId});
if (added) {
@@ -1976,7 +1976,7 @@ Subscriptions are persistent across server reconnects.)";
// Publish unsubscription event
if (removed) {
Api::GlobalEventBus::instance().publish(
emit(
Api::Events::UserUnsubscribed{
.uid = uid, .nickname = nickname, .clientId = clientId});
return "You are now unsubscribed from the bot's audio output.";