|
| 1 | +#include "event_dispatcher.h" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <iostream> |
| 5 | + |
| 6 | +namespace nativeapi { |
| 7 | + |
| 8 | +EventDispatcher::EventDispatcher() |
| 9 | + : running_(false), stop_requested_(false), next_listener_id_(1) {} |
| 10 | + |
| 11 | +EventDispatcher::~EventDispatcher() { |
| 12 | + Stop(); |
| 13 | +} |
| 14 | + |
| 15 | +size_t EventDispatcher::AddListener(std::type_index event_type, |
| 16 | + EventListener* listener) { |
| 17 | + if (!listener) { |
| 18 | + return 0; // Invalid listener |
| 19 | + } |
| 20 | + |
| 21 | + std::lock_guard<std::mutex> lock(listeners_mutex_); |
| 22 | + size_t listener_id = next_listener_id_.fetch_add(1); |
| 23 | + |
| 24 | + listeners_[event_type].push_back({listener, listener_id}); |
| 25 | + |
| 26 | + return listener_id; |
| 27 | +} |
| 28 | + |
| 29 | +bool EventDispatcher::RemoveListener(size_t listener_id) { |
| 30 | + std::lock_guard<std::mutex> lock(listeners_mutex_); |
| 31 | + |
| 32 | + for (auto& [event_type, listener_list] : listeners_) { |
| 33 | + auto it = std::find_if(listener_list.begin(), listener_list.end(), |
| 34 | + [listener_id](const ListenerInfo& info) { |
| 35 | + return info.id == listener_id; |
| 36 | + }); |
| 37 | + |
| 38 | + if (it != listener_list.end()) { |
| 39 | + listener_list.erase(it); |
| 40 | + return true; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return false; |
| 45 | +} |
| 46 | + |
| 47 | +void EventDispatcher::RemoveAllListeners(std::type_index event_type) { |
| 48 | + std::lock_guard<std::mutex> lock(listeners_mutex_); |
| 49 | + listeners_[event_type].clear(); |
| 50 | +} |
| 51 | + |
| 52 | +void EventDispatcher::RemoveAllListeners() { |
| 53 | + std::lock_guard<std::mutex> lock(listeners_mutex_); |
| 54 | + listeners_.clear(); |
| 55 | + callback_listeners_.clear(); |
| 56 | +} |
| 57 | + |
| 58 | +void EventDispatcher::DispatchSync(const Event& event) { |
| 59 | + std::type_index event_type = typeid(event); |
| 60 | + std::vector<EventListener*> listeners_copy; |
| 61 | + |
| 62 | + // Copy listeners to avoid holding the lock during dispatch |
| 63 | + { |
| 64 | + std::lock_guard<std::mutex> lock(listeners_mutex_); |
| 65 | + auto it = listeners_.find(event_type); |
| 66 | + if (it != listeners_.end()) { |
| 67 | + listeners_copy.reserve(it->second.size()); |
| 68 | + for (const auto& info : it->second) { |
| 69 | + listeners_copy.push_back(info.listener); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Dispatch to all listeners |
| 75 | + for (auto* listener : listeners_copy) { |
| 76 | + try { |
| 77 | + listener->OnEvent(event); |
| 78 | + } catch (const std::exception& e) { |
| 79 | + std::cerr << "Exception in event listener: " << e.what() << std::endl; |
| 80 | + } catch (...) { |
| 81 | + std::cerr << "Unknown exception in event listener" << std::endl; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void EventDispatcher::DispatchAsync(std::unique_ptr<Event> event) { |
| 87 | + if (!event) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + // Start the worker thread if not already running |
| 92 | + if (!running_.load()) { |
| 93 | + Start(); |
| 94 | + } |
| 95 | + |
| 96 | + { |
| 97 | + std::lock_guard<std::mutex> lock(queue_mutex_); |
| 98 | + event_queue_.push(std::move(event)); |
| 99 | + } |
| 100 | + |
| 101 | + queue_condition_.notify_one(); |
| 102 | +} |
| 103 | + |
| 104 | +void EventDispatcher::Start() { |
| 105 | + if (running_.load()) { |
| 106 | + return; // Already running |
| 107 | + } |
| 108 | + |
| 109 | + stop_requested_.store(false); |
| 110 | + running_.store(true); |
| 111 | + |
| 112 | + worker_thread_ = std::thread(&EventDispatcher::ProcessAsyncEvents, this); |
| 113 | +} |
| 114 | + |
| 115 | +void EventDispatcher::Stop() { |
| 116 | + if (!running_.load()) { |
| 117 | + return; // Not running |
| 118 | + } |
| 119 | + |
| 120 | + stop_requested_.store(true); |
| 121 | + queue_condition_.notify_all(); |
| 122 | + |
| 123 | + if (worker_thread_.joinable()) { |
| 124 | + worker_thread_.join(); |
| 125 | + } |
| 126 | + |
| 127 | + running_.store(false); |
| 128 | + |
| 129 | + // Clear any remaining events in the queue |
| 130 | + std::lock_guard<std::mutex> lock(queue_mutex_); |
| 131 | + while (!event_queue_.empty()) { |
| 132 | + event_queue_.pop(); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +bool EventDispatcher::IsRunning() const { |
| 137 | + return running_.load(); |
| 138 | +} |
| 139 | + |
| 140 | +size_t EventDispatcher::GetListenerCount(std::type_index event_type) const { |
| 141 | + std::lock_guard<std::mutex> lock(listeners_mutex_); |
| 142 | + auto it = listeners_.find(event_type); |
| 143 | + return (it != listeners_.end()) ? it->second.size() : 0; |
| 144 | +} |
| 145 | + |
| 146 | +size_t EventDispatcher::GetTotalListenerCount() const { |
| 147 | + std::lock_guard<std::mutex> lock(listeners_mutex_); |
| 148 | + size_t total = 0; |
| 149 | + for (const auto& [event_type, listener_list] : listeners_) { |
| 150 | + total += listener_list.size(); |
| 151 | + } |
| 152 | + return total; |
| 153 | +} |
| 154 | + |
| 155 | +void EventDispatcher::ProcessAsyncEvents() { |
| 156 | + while (running_.load()) { |
| 157 | + std::unique_ptr<Event> event; |
| 158 | + |
| 159 | + // Wait for an event or stop signal |
| 160 | + { |
| 161 | + std::unique_lock<std::mutex> lock(queue_mutex_); |
| 162 | + queue_condition_.wait(lock, [this] { |
| 163 | + return !event_queue_.empty() || stop_requested_.load(); |
| 164 | + }); |
| 165 | + |
| 166 | + if (stop_requested_.load()) { |
| 167 | + break; |
| 168 | + } |
| 169 | + |
| 170 | + if (!event_queue_.empty()) { |
| 171 | + event = std::move(event_queue_.front()); |
| 172 | + event_queue_.pop(); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + // Dispatch the event if we have one |
| 177 | + if (event) { |
| 178 | + DispatchSync(*event); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +EventDispatcher& GetGlobalEventDispatcher() { |
| 184 | + static EventDispatcher global_dispatcher; |
| 185 | + return global_dispatcher; |
| 186 | +} |
| 187 | + |
| 188 | +} // namespace nativeapi |
0 commit comments