ReactivePlusPlus
ReactiveX implementation for C++20
Loading...
Searching...
No Matches
Observers

Concepts

concept  rpp::constraint::observer_strategy
 Concept defines requirements for an user-defined observer strategy.
 

Classes

class  rpp::dynamic_observer< Type >
 Type-erased version of the rpp::observer. Any observer can be converted to dynamic_observer via rpp::observer::as_dynamic member function. More...
 
class  rpp::observer< Type, Strategy >
 Base class for any observer used in RPP. It handles core callbacks of observers. Objects of this class would be passed to subscribe of observable. More...
 

Typedefs

template<constraint::decayed_type Type, std::invocable< Type > OnNext, std::invocable< const std::exception_ptr & > OnError, std::invocable<> OnCompleted>
using rpp::lambda_observer = observer<Type, details::observers::lambda_strategy<Type, OnNext, OnError, OnCompleted>>
 Observer specialized with passed callbacks. Most easiesest way to construct observer "on the fly" via lambdas and etc.
 

Functions

template<constraint::decayed_type Type, std::invocable< Type > OnNext, std::invocable< const std::exception_ptr & > OnError = rpp::utils::rethrow_error_t, std::invocable<> OnCompleted = rpp::utils::empty_function_t<>>
auto rpp::make_lambda_observer (OnNext &&on_next, OnError &&on_error={}, OnCompleted &&on_completed={}) -> lambda_observer< Type, std::decay_t< OnNext >, std::decay_t< OnError >, std::decay_t< OnCompleted > >
 Constructs observer specialized with passed callbacks. Most easiesest way to construct observer "on the fly" via lambdas and etc.
 
template<constraint::decayed_type Type, std::invocable< Type > OnNext, std::invocable< const std::exception_ptr & > OnError = rpp::utils::rethrow_error_t, std::invocable<> OnCompleted = rpp::utils::empty_function_t<>>
auto rpp::make_lambda_observer (const rpp::composite_disposable_wrapper &d, OnNext &&on_next, OnError &&on_error={}, OnCompleted &&on_completed={}) -> lambda_observer_with_external_disposable< Type, std::decay_t< OnNext >, std::decay_t< OnError >, std::decay_t< OnCompleted > >
 Constructs observer specialized with passed callbacks. Most easiesest way to construct observer "on the fly" via lambdas and etc.
 

Detailed Description

Observer subscribes on Observable and obtains values provided by Observable.

In fact observer is kind of wrapper over 3 core functions:

Additionally in RPP observer handles disposables related logic:

Observer creation:
  • Observer creation inside subscribe:
    RPP expects user to create observers only inside subscribe function of observables. Something like this:
    rpp::source::just(1).subscribe([](int){}, [](const std::exception_ptr&){}, [](){});
    rpp::source::just(1) | rpp::operators::subscribe([](int){}, [](const std::exception_ptr&){}, [](){});
    auto just(const TScheduler &scheduler, T &&item, Ts &&... items)
    Creates rpp::observable that emits a particular items and completes.
    Definition from.hpp:201
    auto subscribe(observer< Type, ObserverStrategy > &&observer)
    Subscribes passed observer to emissions from this observable.
    Definition subscribe.hpp:226
    Some of the callbacks (on_next/on_error/on_completed) can be omitted. Check rpp::operators::subscribe for more details.
  • Advanced observer creation:
    Technically it is possible to create custom observer via creating new class/struct which satisfies concept rpp::constraint::observer_strategy, but it is highly not-recommended for most cases
    Also technically you could create your observer via make_lambda_observer function, but it is not recommended too: it could disable some built-in optimizations and cause worse performance.
    Also it is most probably bad pattern and invalid usage of RX if you want to keep/store observers as member variables/fields. Most probably you are doing something wrong IF you are not implementing custom observable/operator.
See also
https://reactivex.io/documentation/observable.html

Typedef Documentation

◆ lambda_observer

template<constraint::decayed_type Type, std::invocable< Type > OnNext, std::invocable< const std::exception_ptr & > OnError, std::invocable<> OnCompleted>
using rpp::lambda_observer = observer<Type, details::observers::lambda_strategy<Type, OnNext, OnError, OnCompleted>>

Observer specialized with passed callbacks. Most easiesest way to construct observer "on the fly" via lambdas and etc.

Template Parameters
Typeof value this observer can handle
OnNextis type of callback to handle on_next(const Type&) and on_next(Type&&)
OnErroris type of callback to handle on_error(const std::exception_ptr&)
OnCompletedis type of callback to handle on_completed()

Function Documentation

◆ make_lambda_observer() [1/2]

template<constraint::decayed_type Type, std::invocable< Type > OnNext, std::invocable< const std::exception_ptr & > OnError = rpp::utils::rethrow_error_t, std::invocable<> OnCompleted = rpp::utils::empty_function_t<>>
auto rpp::make_lambda_observer ( const rpp::composite_disposable_wrapper & d,
OnNext && on_next,
OnError && on_error = {},
OnCompleted && on_completed = {} ) -> lambda_observer_with_external_disposable<Type, std::decay_t<OnNext>, std::decay_t<OnError>, std::decay_t<OnCompleted>>

Constructs observer specialized with passed callbacks. Most easiesest way to construct observer "on the fly" via lambdas and etc.

Template Parameters
Typeof value this observer can handle
Parameters
dis disposable to attach to resulting observer
on_nextis callback to handle on_next(const Type&) and on_next(Type&&)
on_erroris callback to handle on_error(const std::exception_ptr&)
on_completedis callback to handle on_completed()

◆ make_lambda_observer() [2/2]

template<constraint::decayed_type Type, std::invocable< Type > OnNext, std::invocable< const std::exception_ptr & > OnError = rpp::utils::rethrow_error_t, std::invocable<> OnCompleted = rpp::utils::empty_function_t<>>
auto rpp::make_lambda_observer ( OnNext && on_next,
OnError && on_error = {},
OnCompleted && on_completed = {} ) -> lambda_observer<Type, std::decay_t<OnNext>, std::decay_t<OnError>, std::decay_t<OnCompleted>>

Constructs observer specialized with passed callbacks. Most easiesest way to construct observer "on the fly" via lambdas and etc.

Template Parameters
Typeof value this observer can handle
Parameters
on_nextis callback to handle on_next(const Type&) and on_next(Type&&)
on_erroris callback to handle on_error(const std::exception_ptr&)
on_completedis callback to handle on_completed()
Examples
readme.cpp.