Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Reactive Patterns with RxJS for Angular
  • Toc
  • feedback
Reactive Patterns with RxJS for Angular

Reactive Patterns with RxJS for Angular

By : Lamis Chebbi
4.1 (16)
close
Reactive Patterns with RxJS for Angular

Reactive Patterns with RxJS for Angular

4.1 (16)
By: Lamis Chebbi

Overview of this book

RxJS is a fast, reliable, and compact library for handling asynchronous and event-based programs. It is a first-class citizen in Angular and enables web developers to enhance application performance, code quality, and user experience, so using reactive patterns in your Angular web development projects can improve user interaction on your apps, which will significantly improve the ROI of your applications. This book is a step-by-step guide to learning everything about RxJS and reactivity. You'll begin by understanding the importance of the reactive paradigm and the new features of RxJS 7. Next, you'll discover various reactive patterns, based on real-world use cases, for managing your application’s data efficiently and implementing common features using the fewest lines of code. As you build a complete application progressively throughout the book, you'll learn how to handle your app data reactively and explore different patterns that enhance the user experience and code quality, while also improving the maintainability of Angular apps and the developer's productivity. Finally, you'll test your asynchronous streams and enhance the performance and quality of your applications by following best practices. By the end of this RxJS Angular book, you'll be able to develop Angular applications by implementing reactive patterns.
Table of Contents (19 chapters)
close
1
Part 1 – Introduction
5
Part 2 – A Trip into Reactive Patterns
10
Part 3 – Multicasting Takes You to New Places
16
Part 4 – Final Touch

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.

bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete