diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-08-07 17:08:15 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-08-07 17:08:15 +0200 |
commit | 8b275c0b0a1064a813ef14a109b64e1fce461893 (patch) | |
tree | 7bb1cc13d2eb9be2a344b4cd35f5e581df970f0d /src/Event.cpp | |
parent | 2017-08-05 (diff) | |
download | AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar.gz AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar.bz2 AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar.lz AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar.xz AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar.zst AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.zip |
Diffstat (limited to 'src/Event.cpp')
-rw-r--r-- | src/Event.cpp | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/src/Event.cpp b/src/Event.cpp index e58a6f4..94eedba 100644 --- a/src/Event.cpp +++ b/src/Event.cpp @@ -19,13 +19,20 @@ void EventListener::PushEvent(Event event) { eventsMutex.lock(); handlersMutex.lock(); if (handlers[event.type]) { - //LOG(ERROR) << "Listener notified about event " << int(event.type); events.push(event); } handlersMutex.unlock(); eventsMutex.unlock(); } +void EventListener::DirectCall(Event event) +{ + handlersMutex.lock(); + if (handlers[event.type]) + handlers[event.type](event.data); + handlersMutex.unlock(); +} + bool EventListener::IsEventsQueueIsNotEmpty() { eventsMutex.lock(); bool value = !events.empty(); @@ -74,20 +81,33 @@ void EventAgregator::PushEvent(EventType type, EventData data) { Event event; event.type = type; event.data = data; + queueMutex.lock(); eventsToHandle.push(event); + queueMutex.unlock(); if (!isStarted) { isStarted = true; std::thread(&EventAgregator::EventHandlingLoop).detach(); } } +void EventAgregator::DirectEventCall(EventType type, EventData data) +{ + Event event {type, data}; + listenersMutex.lock(); + for (auto &listener : listeners) { + listenersMutex.unlock(); + listener->DirectCall(event); + listenersMutex.lock(); + } + listenersMutex.unlock(); +} + void EventAgregator::EventHandlingLoop() { + LoopExecutionTimeController timer(std::chrono::milliseconds(5)); while (true) { queueMutex.lock(); if (!eventsToHandle.empty()) { - auto queue = eventsToHandle; - while (!eventsToHandle.empty()) - eventsToHandle.pop(); + auto queue = std::move(eventsToHandle); queueMutex.unlock(); while (!queue.empty()) { @@ -103,9 +123,6 @@ void EventAgregator::EventHandlingLoop() { queueMutex.lock(); } queueMutex.unlock(); + timer.Update(); } -} - -void SetGlobalState(GlobalState state) { - EventAgregator::PushEvent(EventType::GlobalAppState, GlobalAppStateData{state}); }
\ No newline at end of file |