Files
Echoion/EchoionStandalone/src/EventCallbackGate.cpp
T
faumaray ef3139ca64 Update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-12 02:57:35 +03:00

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);
}
}
}