Operator Rundown
Get an overview of all the operators you have studied in this chapter.
We'll cover the following
This chapter presented us with a few new operators, so here’s a recap of them, along with some scenarios with ways in which we can use them in our applications. Remember, we can always find the complete API documentation for operators on the RxJS GitHub site.
Rx.Observable.from
Default behavior: Synchronous
Since many of the data sources we use in our applications will come from arrays or iterables, it makes sense to have an operator that creates Observables out of them. from
is one of the operators we’ll use the most.
With the from
operator, we can create Observables from arrays, array-like objects (for instance, the arguments object or DOM NodeLists), and even types that implement the iterable protocol, such as string, map, and set.
Rx.Observable.range
Default behavior: Synchronous
The range
operator generates finite Observables that emit integers in a particular range. It is extremely versatile and can be used in many scenarios. For example, we could use the range
operator to generate the initial squares on the board of a game like Minesweeper.
Rx.Observable.interval
Default behavior: Asynchronous
Each time we need to generate values spaced in time, we’ll probably start with an interval
operator as the generator. Since the interval
operator emits sequential integers every x milliseconds (where x is a parameter we pass), we just need to transform the values to whatever we want. Our game in the upcoming chapters is heavily based on that technique.
Rx.Observable.distinct
Default behavior: Same as the Observable it filters
The distinct
operator is one of those extremely simple operators that saves a ton of development work. It filters out of the sequence any value that has already been emitted. That keeps us from writing time and again that error-prone boilerplate code that uses a dictionary somewhere with the emitted results, against which we compare incoming results. You know what kind of code I’m talking about. That’s gone with distinct
.
Get hands-on with 1200+ tech skills courses.