Subjects
A Subject is an entity that is both an Observer and an Observable. It helps to relay notifications from one Observable (typically) to a set of Observers. We can implement sophisticated techniques such as the caching and buffering of data using a Subject. We can also use a Subject to transform a hot Observable into a cold Observable. There are four variants of subjects implemented in RxCpp library.
They are as follows:
SimpleSubject
BehaviorSubject
ReplaySubject
SynchronizeSubject
Let's write a simple program that will demonstrate the work of a Subject. The code listing will demonstrate how we can push data to a Subject and retrieve them using the Observer side of the Subject.
//------- 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 ...