Skip to content

Commit

Permalink
Event dispatching performance fix
Browse files Browse the repository at this point in the history
  • Loading branch information
FreezeEngine committed Aug 23, 2024
1 parent e0a7e45 commit bd18c90
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/api/eventing/EventManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ class IEventManager {
public:
template <typename T>
bool dispatch(T& ev) requires std::derived_from<T, Event> {
std::sort(listeners.begin(), listeners.end(), [](std::pair<uint32_t, EventListener> const& left,
std::pair<uint32_t, EventListener> const& right) {
return left.second.priority > right.second.priority;
});

for (auto& pair : listeners) {
if (pair.first == T::hash) {
if (pair.second.listener->shouldListen() || pair.second.callWhileInactive) {
Expand All @@ -33,6 +28,10 @@ class IEventManager {
void listen(Listener* ptr, EventListenerFunc listener, int priority = 0, bool callWhileInactive = false) requires std::derived_from<T, Event> {
mutex.lock();
listeners.push_back({ T::hash, EventListener{ listener, ptr, callWhileInactive, priority } });
std::sort(listeners.begin(), listeners.end(), [](std::pair<uint32_t, EventListener> const& left,
std::pair<uint32_t, EventListener> const& right) {
return left.second.priority > right.second.priority;
});
mutex.unlock();
}

Expand Down

0 comments on commit bd18c90

Please sign in to comment.