#include <userver/concurrent/async_event_channel.hpp>
AsyncEventChannel is an in-process pub-sub with strict FIFO serialization, i.e. only after the event was processed a new event may appear for processing, same listener is never called concurrently.
Example usage:
Definition at line 77 of file async_event_channel.hpp.
Public Types | |
using | Function = typename AsyncEventSource<Args...>::Function |
using | OnRemoveCallback = std::function<void(Function&)> |
Public Member Functions | |
AsyncEventChannel (std::string_view name) | |
The primary constructor. | |
AsyncEventChannel (std::string_view name, OnRemoveCallback on_listener_removal) | |
The constructor with AsyncEventSubscriberScope usage checking. | |
template<typename UpdaterFunc > | |
AsyncEventSubscriberScope | DoUpdateAndListen (FunctionId id, std::string_view name, Function &&func, UpdaterFunc &&updater) |
For use in UpdateAndListen of specific event channels. | |
template<typename Class , typename UpdaterFunc > | |
AsyncEventSubscriberScope | DoUpdateAndListen (Class *obj, std::string_view name, void(Class::*func)(Args...), UpdaterFunc &&updater) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
void | SendEvent (Args... args) const |
const std::string & | Name () const noexcept |
AsyncEventSubscriberScope | AddListener (Class *obj, std::string_view name, void(Class::*func)(Args...)) |
Subscribes to updates from this event source. | |
AsyncEventSubscriberScope | AddListener (FunctionId id, std::string_view name, Function &&func) |
A type-erased version of AddListener. | |
Protected Member Functions | |
virtual AsyncEventSubscriberScope | DoAddListener (FunctionId id, std::string_view name, Function &&func)=0 |
using concurrent::AsyncEventChannel< Args >::Function = typename AsyncEventSource<Args...>::Function |
Definition at line 79 of file async_event_channel.hpp.
using concurrent::AsyncEventChannel< Args >::OnRemoveCallback = std::function<void(Function&)> |
Definition at line 80 of file async_event_channel.hpp.
|
inlineexplicit |
The primary constructor.
name | used for diagnostic purposes and is also accessible with Name |
Definition at line 84 of file async_event_channel.hpp.
|
inline |
The constructor with AsyncEventSubscriberScope
usage checking.
The constructor with a callback that is called on listener removal. The callback takes a reference to ‘Function’ as input. This is useful for checking the lifetime of data captured by the listener update function.
on_listener_removal
function must be valid until the AsyncEventChannel
object is completely destroyed.Example usage:
name | used for diagnostic purposes and is also accessible with Name |
on_listener_removal | the callback used for check |
Definition at line 104 of file async_event_channel.hpp.
|
inlineinherited |
Subscribes to updates from this event source.
The listener won't be called immediately. To process the current value and then listen to updates, use UpdateAndListen
of specific event channels.
Example usage:
obj | the subscriber, which is the owner of the listener method, and is also used as the unique identifier of the subscription for this AsyncEventSource |
name | the name of the subscriber, for diagnostic purposes |
func | the listener method, usually called On<DataName>Update , e.g. OnConfigUpdate or OnCacheUpdate |
Unsubscribe
should be called explicitly Definition at line 127 of file async_event_source.hpp.
|
inlineinherited |
A type-erased version of AddListener.
id | the unique identifier of the subscription |
name | the name of the subscriber, for diagnostic purposes |
func | the callback used for event notifications |
Definition at line 136 of file async_event_source.hpp.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 124 of file async_event_channel.hpp.
|
inline |
For use in UpdateAndListen
of specific event channels.
Atomically calls updater
, which should invoke func
with the previously sent event, and subscribes to new events as if using AddListener.
Definition at line 115 of file async_event_channel.hpp.
|
inlinenoexcept |
Definition at line 158 of file async_event_channel.hpp.
|
inline |
Send the next event and wait until all the listeners process it.
Strict FIFO serialization is guaranteed, i.e. only after this event is processed a new event may be delivered for the subscribers, same listener/subscriber is never called concurrently.
Definition at line 138 of file async_event_channel.hpp.