Introduction About React Observables:
In ReactiveX, an observer first needs to subscribe to an observable. After that, the observer reacts to the item or sequence of items that the observable emits. This pattern helps to simplify concurrent operations. This is because it does not block while waiting for the observables to emit objects.
A marble diagram represents observables and their transformations in the following ways:
A few things to know about React Observables:
While conducting many software programming tasks, you might expect a few things. You may expect that the instructions you write will execute and complete their tasks one at a time. You may also think that they will follow the order in which you have written to them. This is not the case for ReactiveX. Many instructions may execute in parallel and their results later capture in random order by observers. Here, first, you’ll define a mechanism for the regain and transfer of data instead of calling a method. After that, subscribe to an observer to it. Then the previously defined mechanism fires into action with the react observable. It captures and responds to the emissions whenever they are ready.
There’s one benefit of this approach. You can start all the tasks at a time. You don’t need to depend on one another. Rather than waiting for every task to complete you can start it without waiting. This way, all of your tasks will complete sooner than before.
We use many terminologies to describe this model of asynchronous programming and design. A few of these terms are:
- An observer subscribes to an observable
- An observable emits items or sends notifications to its observers by calling the observers’ methods.
In different situations, sometimes the observer is also known as a subscriber, watcher or reactor.
Establishing React Observable:
In an ordinary method call, typical in ReactiveX, the flow is something like this:
1) Call a method
2) Store the return value from the method in a variable
3) Use that variable and its new value to do something useful.
Another way you can write this is in the form of code. For example, you can refer the code below:
returnVal = someMethod(itsParameters);
In the asynchronous model, the flow will be like this:
1) You first need to define a method that does something useful with the return value from the asynchronous call. This method is a part of the observer.
2) Sometimes it is important to call the asynchronous call as observable.
3) Attach the observer to the observable by subscribing it. This also initiates the actions of the observable.
4) You can go on with your business. Whenever the call returns, the observer’s method will begin to operate.
It looks something like this:
onNext, onCompleted and onError:
You can connect an observer to an observable by using the subscribe method. Your observer implements some subset of the following methods:
Observable calls this method whenever it emits an item. This method takes the item as a parameter.
Observable uses this call during the emission of the item. This method considers the item as a parameter that is emitted.
Once the onNext call is generated for the last time, this method will be active. This will only happen if there aren’t any errors till now.
There is a special observer interface in some ReactiveX that implements an unsubscribe method. We call this method to indicate that the Subscriber doesn’t have any interest further in the respective content. Those Observables can then choose to stop generating new items to emit if they no longer have other interested observers.
The results of the unsubscription will go back through the chain of operators. These unsubscriptions will apply to the Observable that the observer subscribed to. As a result, each link in the chain will stop emitting items. This does not guarantee to happen immediately.
Hot and Cold React Observable
A hot react observable may begin emitting items as soon as it’s creation completes. And because of that, any observer who subscribes later may observe the sequence somewhere in the middle.
A cold react observable on the other hand waits until an observer subscribes to it. After that, it emits the items. As a result, an observer is able to see the whole sequence from the beginning.
Composition via React Observable Operators-
The Rx operators allow you to compose asynchronous sequences together in a declarative manner. It also provides all the efficiency benefits of callbacks. But it doesn’t provide the drawbacks of nesting callback handlers. These are typically associated with asynchronous systems.
All the details about the various operators and their usage is given in below links:
These pages include information about some operators that are not part of the ReactiveX. But these are implemented in one or more of language-specific implementations and/or optional modules.
Most operators that operate on observable, return an observable. This allows you to apply these operators in a chain.
A chain of observable operators do not operate independently on the original observable that originates the chain. Operator generates each operation immediately.
In an asynchronous method, we will need to define a method that will use the return value from an asynchronous call. After that, an asynchronous call will have to define itself as observable. Later, attach the observer to the observable by subscribing it. This will also help to start the action of the observable. ReactiveX also has a special method as an unsubscribe. We call this method to show that the subscriber is no longer interested in that particular content. Hot observers will continue emitting items as soon as they are created. A cold observer on the other hand will wait for the item to subscribe.