diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-20 20:24:17 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-20 20:24:17 +0100 |
commit | 397da65e03ec681922aeea3881918026d36068a7 (patch) | |
tree | dca704ae1ad3241e0c559e9037c300ccede8a437 /src/crepe/api/EventManager.cpp | |
parent | 9f882131f09410113d757d96e5aa0322aa5584bd (diff) |
result of loeks temper tantrum
Diffstat (limited to 'src/crepe/api/EventManager.cpp')
-rw-r--r-- | src/crepe/api/EventManager.cpp | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/src/crepe/api/EventManager.cpp b/src/crepe/api/EventManager.cpp index dbdb0c3..64d7c26 100644 --- a/src/crepe/api/EventManager.cpp +++ b/src/crepe/api/EventManager.cpp @@ -15,22 +15,18 @@ void EventManager::dispatch_events() { bool event_handled = false; auto handlers_it = this->subscribers.find(event_type); - if (handlers_it != this->subscribers.end()) { - std::vector<CallbackEntry> & handlers = handlers_it->second; - - std::sort(handlers.begin(), handlers.end(), - [](const CallbackEntry & a, const CallbackEntry & b) { - return a.priority > b.priority; - }); - - for (auto handler_it = handlers.begin(); handler_it != handlers.end(); - ++handler_it) { - // If callback is executed and returns true, remove the event from the queue - if ((*handler_it).callback->exec(*event)) { - event_it = this->events_queue.erase(event_it); - event_handled = true; - break; - } + if (handlers_it == this->subscribers.end()) { + continue; + } + std::vector<CallbackEntry> & handlers = handlers_it->second; + + for (auto handler_it = handlers.begin(); handler_it != handlers.end(); + ++handler_it) { + // If callback is executed and returns true, remove the event from the queue + if ((*handler_it).callback->exec(*event)) { + event_it = this->events_queue.erase(event_it); + event_handled = true; + break; } } @@ -44,3 +40,17 @@ void EventManager::clear() { this->subscribers.clear(); this->events_queue.clear(); } + +void EventManager::unsubscribe(subscription_t event_id) { + for (auto& [event_type, handlers] : this->subscribers) { + for (auto it = handlers.begin(); it != handlers.end();) { + if (it->id == event_id) { + it = handlers.erase(it); + return; + } else { + ++it; + } + } + } +} + |