#include "echoion/standalone/EventCallbackGate.hpp" #include #include 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 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); } } }