From bf75790921cc003852b984b56f1f5c38c2fd680b Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Fri, 24 Apr 2026 16:06:58 +0800 Subject: [PATCH] feat: Refactor EventBus to use priority-based listener management and improve unsubscribe functionality --- src/libagent/includes/event/eventbus.hpp | 131 ++++++++++++++++------- 1 file changed, 95 insertions(+), 36 deletions(-) diff --git a/src/libagent/includes/event/eventbus.hpp b/src/libagent/includes/event/eventbus.hpp index 3e93c37..2af39c6 100644 --- a/src/libagent/includes/event/eventbus.hpp +++ b/src/libagent/includes/event/eventbus.hpp @@ -1,69 +1,128 @@ #pragma once -#include #include #include #include #include #include +#include +#include +#include +#include class EventBus { public: + using Token = std::uint64_t; + template using Listener = std::function; template - void subscribe(Listener listener, int priority = 0) { + Token subscribe(Listener listener, int priority = 0) { std::lock_guard lock(mutex_); - auto& dispatcher = getDispatcher(); - dispatcher.appendListener(priority, std::move(listener)); + + auto& vec = getListeners(); + + Token token = nextToken_++; + + vec.push_back({ + token, + priority, + std::move(listener) + }); + + std::sort(vec.begin(), vec.end(), + [](const auto& a, const auto& b) { + return a.priority > b.priority; + }); + + return token; + } + + template + bool unsubscribe(Token token) { + std::lock_guard lock(mutex_); + + auto type = std::type_index(typeid(Event)); + auto it = listeners_.find(type); + if (it == listeners_.end()) { + return false; + } + + auto& vec = + *std::static_pointer_cast>>(it->second); + + auto oldSize = vec.size(); + + vec.erase( + std::remove_if(vec.begin(), vec.end(), + [token](const Node& node) { + return node.token == token; + }), + vec.end() + ); + + return vec.size() != oldSize; + } + + template + void post(const Event& event) { + std::vector> copy; + + { + std::lock_guard lock(mutex_); + + auto type = std::type_index(typeid(Event)); + auto it = listeners_.find(type); + + if (it == listeners_.end()) { + return; + } + + copy = + *std::static_pointer_cast>>(it->second); + } + + for (auto& node : copy) { + node.callback(event); + } } template void clear() { std::lock_guard lock(mutex_); - dispatchers_.erase(std::type_index(typeid(Event))); + listeners_.erase(std::type_index(typeid(Event))); } - template - void post(const Event& event) { - std::shared_ptr dispatcherPtr; - - { - std::lock_guard lock(mutex_); - auto it = dispatchers_.find(std::type_index(typeid(Event))); - if(it == dispatchers_.end()) return; - dispatcherPtr = it->second; - } - - auto dispatcher = std::static_pointer_cast< - eventpp::EventDispatcher - >(dispatcherPtr); - - dispatcher->dispatch(0, event); + void clearAll() { + std::lock_guard lock(mutex_); + listeners_.clear(); } private: - std::mutex mutex_; + template + struct Node { + Token token; + int priority; + Listener callback; + }; - std::unordered_map> dispatchers_; + std::mutex mutex_; + std::atomic nextToken_ {1}; + + std::unordered_map> listeners_; template - eventpp::EventDispatcher& getDispatcher() { - auto typeIdx = std::type_index(typeid(Event)); - auto it = dispatchers_.find(typeIdx); + std::vector>& getListeners() { + auto type = std::type_index(typeid(Event)); + auto it = listeners_.find(type); - if(it == dispatchers_.end()) { - auto dispatcher = std::make_shared< - eventpp::EventDispatcher - >(); - - dispatchers_[typeIdx] = dispatcher; - return *dispatcher; + if (it == listeners_.end()) { + auto ptr = std::make_shared>>(); + listeners_[type] = ptr; + return *ptr; } - return *std::static_pointer_cast< - eventpp::EventDispatcher - >(it->second); + return *std::static_pointer_cast>>(it->second); } }; \ No newline at end of file