43 lines
937 B
C++
43 lines
937 B
C++
#include "echoion/standalone/EventCallbackGate.hpp"
|
|
|
|
#include <Void/Core/Scope.hpp>
|
|
|
|
#include <utility>
|
|
|
|
namespace Echoion::Standalone
|
|
{
|
|
void EventCallbackGate::add(Api::EventCallback callback)
|
|
{
|
|
std::lock_guard lock(mutex_);
|
|
accepting_ = true;
|
|
callbacks_.push_back(std::move(callback));
|
|
}
|
|
|
|
void EventCallbackGate::quiesce()
|
|
{
|
|
std::unique_lock lock(mutex_);
|
|
accepting_ = false;
|
|
callbacks_.clear();
|
|
cv_.wait(lock, [this] { return activeDispatches_ == 0; });
|
|
}
|
|
|
|
void EventCallbackGate::dispatch(const Api::PluginEvent& event)
|
|
{
|
|
std::vector<Api::EventCallback> callbacks;
|
|
{
|
|
std::lock_guard lock(mutex_);
|
|
if (!accepting_) return;
|
|
++activeDispatches_;
|
|
callbacks = callbacks_;
|
|
}
|
|
const auto complete = Void::Scope::on_exit([this] {
|
|
std::lock_guard lock(mutex_);
|
|
--activeDispatches_;
|
|
cv_.notify_all();
|
|
});
|
|
for (const auto& callback : callbacks) {
|
|
if (callback) callback(event);
|
|
}
|
|
}
|
|
}
|