Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Reactive Patterns with RxJS for Angular

You're reading from   Reactive Patterns with RxJS for Angular A practical guide to managing your Angular application's data reactively and efficiently using RxJS 7

Arrow left icon
Product type Paperback
Published in Apr 2022
Publisher Packt
ISBN-13 9781801811514
Length 224 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Lamis Chebbi Lamis Chebbi
Author Profile Icon Lamis Chebbi
Lamis Chebbi
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Preface 1. Part 1 – Introduction
2. Chapter 1: The Power of the Reactive Paradigm FREE CHAPTER 3. Chapter 2: RxJS 7 – The Major Features 4. Chapter 3: A Walkthrough of the Application 5. Part 2 – A Trip into Reactive Patterns
6. Chapter 4: Fetching Data as Streams 7. Chapter 5: Error Handling 8. Chapter 6: Combining Streams 9. Chapter 7: Transforming Streams 10. Part 3 – Multicasting Takes You to New Places
11. Chapter 8: Multicasting Essentials 12. Chapter 9: Caching Streams 13. Chapter 10: Sharing Data between Components 14. Chapter 11: Bulk Operations 15. Chapter 12: Processing Real-Time Updates 16. Part 4 – Final Touch
17. Chapter 13: Testing RxJS Observables 18. Other Books You May Enjoy

Learning about the marble diagram – our secret weapon

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:

  • Input Observable(s): This represents one or many observables given as input to the operator.
  • Operator: This represents the operator to be executed with its parameters.
  • Output Observable: This represents the observable produced after the operator's execution:
Figure 1.2 – The operator execution

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:

  • The timeline: Observables are asynchronous streams that produce data over time. Therefore, the representation of time is crucial in the marble diagram, and it is represented as an arrow flowing from left to right.
  • The marble values: These are the values emitted by the observables over time.
  • The completion status: The vertical line represents the successful completion of the observables.
  • The error status: The X represents an error emitted by the observable. Neither values nor the vertical line representing the completion will be emitted thereafter:
Figure 1.3 – Elements of the marble diagram

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

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.

You have been reading a chapter from
Reactive Patterns with RxJS for Angular
Published in: Apr 2022
Publisher: Packt
ISBN-13: 9781801811514
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime