Feat: add front-insertion listener registration to the event bus

- libagent: add EventBus::subscribeFront to insert a listener ahead of the existing ones for the same event

- libagent: add IService::listenFront, tracked like listen() so unlistenAll() still cleans it up; for cases where listener ordering matters (e.g. a transformer pipeline)
This commit is contained in:
xiaozhou233 committed 2026-10-15 21:03:03 +08:00
1 parent 5b0a26d49e
commit 13145f77c9
2 files changed
+44 -6

No files matched your search

+17
View File
@@ -67,6 +67,23 @@ public:
return handle;
}
// Like subscribe(), but inserts the listener at the front so it runs
// before previously registered listeners for the same event.
template <typename EventT>
Handle subscribeFront(std::function<void(const EventT&)> callback) {
std::lock_guard<std::mutex> lock(mutex_);
const Handle handle = next_handle_++;
auto& list = listeners_[std::type_index(typeid(EventT))];
list.insert(list.begin(), Listener{
handle,
[callback](const void* data) {
callback(*static_cast<const EventT*>(data));
}
});
return handle;
}
template <typename EventT>
bool unsubscribe(Handle handle) {
return unsubscribe(std::type_index(typeid(EventT)), handle);
+27 -6
View File
@@ -1,5 +1,6 @@
#pragma once
#include <functional>
#include <type_traits>
#include <typeindex>
#include <utility>
@@ -22,17 +23,28 @@ public:
virtual void onShutdown() {}
protected:
// Register `handler` to run after previously registered listeners.
template <typename ServiceT, typename EventT>
void listen(void (ServiceT::*handler)(const EventT&)) {
static_assert(std::is_base_of<IService, ServiceT>::value,
"ServiceT must derive from IService");
auto* self = static_cast<ServiceT*>(this);
const EventBus::Handle handle =
EventBus::getInstance().subscribe<EventT>(
[self, handler](const EventT& event) {
(self->*handler)(event);
});
handles_.push_back({std::type_index(typeid(EventT)), handle});
trackListener<EventT>(
[self, handler](const EventT& event) { (self->*handler)(event); },
false);
}
// Register `handler` to run before previously registered listeners.
// Use it when the ordering between listeners matters (e.g. a transformer
// pipeline that must run before other consumers).
template <typename ServiceT, typename EventT>
void listenFront(void (ServiceT::*handler)(const EventT&)) {
static_assert(std::is_base_of<IService, ServiceT>::value,
"ServiceT must derive from IService");
auto* self = static_cast<ServiceT*>(this);
trackListener<EventT>(
[self, handler](const EventT& event) { (self->*handler)(event); },
true);
}
// Remove all listeners registered by this service. Call it in onShutdown()
@@ -46,6 +58,15 @@ protected:
}
private:
template <typename EventT>
void trackListener(std::function<void(const EventT&)> callback, bool front) {
auto& bus = EventBus::getInstance();
const EventBus::Handle handle = front
? bus.subscribeFront<EventT>(std::move(callback))
: bus.subscribe<EventT>(std::move(callback));
handles_.push_back({std::type_index(typeid(EventT)), handle});
}
std::vector<std::pair<std::type_index, EventBus::Handle>> handles_;
};