userver: concurrent::AsyncEventSource< Args > Class Template Reference
Loading...
Searching...
No Matches
concurrent::AsyncEventSource< Args > Class Template Referenceabstract

#include <userver/concurrent/async_event_source.hpp>

Detailed Description

template<typename... Args>
class concurrent::AsyncEventSource< Args >

The read-only side of an event channel. Events are delivered to listeners in a strict FIFO order, i.e. only after the event was processed a new event may appear for processing, same listener is never called concurrently.

Definition at line 102 of file async_event_source.hpp.

+ Inheritance diagram for concurrent::AsyncEventSource< Args >:
+ Collaboration diagram for concurrent::AsyncEventSource< Args >:

Public Types

using Function = std::function<void(Args... args)>
 

Public Member Functions

template<class Class >
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.
 
void RemoveListener (FunctionId, UnsubscribingKind) noexcept override=0
 Used by AsyncEventSubscriberScope to cancel an event subscription.
 

Protected Member Functions

virtual AsyncEventSubscriberScope DoAddListener (FunctionId id, std::string_view name, Function &&func)=0
 

Member Typedef Documentation

◆ Function

template<typename... Args>
using concurrent::AsyncEventSource< Args >::Function = std::function<void(Args... args)>

Definition at line 104 of file async_event_source.hpp.

Member Function Documentation

◆ AddListener() [1/2]

template<typename... Args>
template<class Class >
AsyncEventSubscriberScope concurrent::AsyncEventSource< Args >::AddListener ( Class * obj,
std::string_view name,
void(Class::*)(Args...) func )
inline

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.

Warning
Listeners should not be added or removed while processing the event inside another listener.

Example usage:

UTEST(AsyncEventChannel, AddListenerSample) {
WeatherStorage weather_storage(WeatherKind::kSunny);
std::vector<WeatherKind> recorded_weather;
weather_storage.GetSource().AddListener(
concurrent::FunctionId(&recorder), "recorder",
[&](WeatherKind weather) { recorded_weather.push_back(weather); });
weather_storage.Set(WeatherKind::kRainy);
weather_storage.Set(WeatherKind::kSunny);
weather_storage.Set(WeatherKind::kSunny);
EXPECT_EQ(recorded_weather,
(std::vector{WeatherKind::kRainy, WeatherKind::kSunny,
WeatherKind::kSunny}));
recorder.Unsubscribe();
}
Parameters
objthe subscriber, which is the owner of the listener method, and is also used as the unique identifier of the subscription for this AsyncEventSource
namethe name of the subscriber, for diagnostic purposes
functhe listener method, usually called On<DataName>Update, e.g. OnConfigUpdate or OnCacheUpdate
Returns
a AsyncEventSubscriberScope controlling the subscription, which should be stored as a member in the subscriber; Unsubscribe should be called explicitly

Definition at line 129 of file async_event_source.hpp.

◆ AddListener() [2/2]

template<typename... Args>
AsyncEventSubscriberScope concurrent::AsyncEventSource< Args >::AddListener ( FunctionId id,
std::string_view name,
Function && func )
inline

A type-erased version of AddListener.

Parameters
idthe unique identifier of the subscription
namethe name of the subscriber, for diagnostic purposes
functhe callback used for event notifications

Definition at line 140 of file async_event_source.hpp.


The documentation for this class was generated from the following file: