Subjects
A subject is an entity that is simultaneously an Observer and an Observable. It helps to relay notifications from one Observable to a set of Observers. We can implement sophisticated techniques such as the caching and buffering of data. We can also use a subject to convert a hot Observable into a cold Observable. There are four variants of subjects implemented in RxCpp. They are as follows:
SimpleSubjectBehaviorSubjectReplaySubjectSynchronizeSubject
Let's write a simple program that will subscribe data as an Observer and act as an Observable for a pair of subscribers:
//------- SimpleSubject.cpp
#include <rxcpp/rx.hpp>
#include <memory>
int main(int argc, char *argv[]) {
//----- Create an instance of Subject
rxcpp::subjects::subject<int> subject;
//----- Retreive the Observable
//----- attached to the Subject
auto observable = subject.get_observable();
//------ Subscribe Twice
observable.subscribe( [] ( int v ) { printf("1--...