
Reactive Patterns with RxJS for Angular
By :

RxJS ships with more than 100 operators. Operators are one of the building blocks of RxJS. They are very useful for manipulating streams. All the reactive patterns that will be detailed later in this book are based on operators. And when it comes to explaining operators, it is better to refer to a visual representation. And that's why there are marble diagrams. Thankfully!
Marble diagrams are a visual representation of the operator's execution. We will be using the marble diagram in all chapters to understand the behavior of RxJS operators. At first, it might seem daunting, but it is delightfully simple. First, you only have to understand the anatomy of the diagram and then you're good to read and translate it. You will be able to understand any RxJS operator – those used in this book along with others, too.
So as we said, marble diagrams represent the execution of an operator. So, every diagram will include the following:
Figure 1.2 – The operator execution
Well, this is the big picture. Now, let's zoom in on the representation of the input/output observables. It includes the following:
Figure 1.3 – Elements of the marble diagram
That's all the elements you need to know. Now, let's put all of the pieces together into a real marble diagram:
Figure 1.4 – An example of a marble diagram for a custom operator
Let's break down what is happening in the preceding diagram. As you might have guessed, we have a custom operator called divideByTwo
that will emit half of every received number. When the input observable emits the values of 4
and 8
, the output observable produces 2
and 4
, respectively.
However, when the value of R
, which is non-numeric, is emitted, then an error is thrown, indicating abnormal termination. This case is not handled in the operator code. The input observable continues the emission and then completes successfully. However, the value will never be processed because, after the error, the stream is closed.
At this level, we went through all of the elements composing the marble diagram. And you will be able to understand the operators used in the chapters to come.