Reactive Programming

First...Let's learn some things !

Reactive programming is the concept of developing code where different entities are watching through an information channel for data to pass, and when it does, each entity will "react" to that situation doing their business logic.

Imagine an empty tube with 3 holes in it, at each hole we have a person looking through and waiting for a little ball to pass. Because each person is different, they will see different aspects of the ball. For example, one can see that the ball is red, another that the ball is bouncing a little when it's passing through or even that maybe… it's not a ball at all !

That's exactly what reactive programming is, we have an information channel (observables) containing the data that is passing through it, and on the other hand we have entities (observers) that will be subscribed and waiting for that information to pass... and when it does, they will react to it doing whatever business logic we coded for them.

Redux, Ngrx/store, etc. are libraries that provide tools to manage the state of the application using the concept of "A Single Source Of Truth", these means that the whole information of the app will be contained inside a "store", a place where you can access useful and up to date information. Each time a change happens inside your application, information wise, the store will be updated with the latest changes. So you don't need to call the back end again for an update if not needed and also all entities that are watching the store's states will be automatically alerted of the change.

What is RxJS ?

RxJS it’s an amazing Javascript library that provides management of observables to do reactive programming in an efficient and structured way.

Operators

RxJS provides it’s magic with the usage of operators, they are functions to create complex asynchronous code but in a simple and declarative manner.

Two tipes:

All pictures are from these amazing website that i super recommend to play a little and learn interacting with the streams.

Subjects

An RxJS subject is a special kind of observable that supports multicasting to many observables.

Unicast is the ability to stream events from one source into another, in these case an observable has an observer waiting for changes.

Multicasting is the ability to stream events from one source into more than one, in these case an observable can have more than one observer listening for changes.

As they are observables, they can be subscribed and assigned an observer to receive events. In the background, they don’t create a new execution every time an observer subscribes to it, they just register the observer into a list containing all the listeners to that observable.

Subjects can also be thought as observers as they are objects that provide the methods: