Skip to main content

How to use RxJS operators to consume Observables in your workflow


RxJS

RxJS is a framework for reactive programming that makes use of Observables, making it really easy to write asynchronous code. According to the official documentation, this project is a kind of reactive extension to JavaScript with better performance, better modularity, better debuggable call stacks, while staying mostly backwards compatible, with some breaking changes that reduce the API surface. It is the official library used by Angular to handle reactivity, converting pull operations for call-backs into Observables.

Prerequisites

To be able to follow through in this article’s demonstration you should have:
  • Node version 11.0 installed on your machine
  • Node Package Manager version 6.7 (usually ships with Node installation)
  • Angular CLI version 7.0
  • The latest version of Angular (version 7)
// run the command in a terminal
ng version
Confirm that you are using version 7, and update to 7 if you are not.
  • Download this tutorial’s starter project here to follow through the demonstrations
  • Unzip the project and initialize the node modules in your terminal with this command:
npm install

Understanding RxJS operators

Observables are the foundation of reactive programming in RxJS and operators are the best way to consume or utilize them. Operators are methods you can use on Observables and subjects to manipulate, filter or change the Observable in a specified manner into a new Observable. They provide a platform for complex logic to be run on Observables and gives the developer total control of the output of the Observables.
It is important to note that operators do not change the initial Observable, they just edit it and output an entirely new Observable.

Types of operators

According to the official RxJS documentation, there are two types of operators.
A. Pipeable operators: These are operators that can be piped to existing Observables using the pipe syntax.
observableInstance.pipe(operator())
They are called on existing Observables and they do not change the Observable instance, they return a new Observable with a subscribe method based on the initial Observable.
B. Creation operators: These operators, on the other hand, create an Observable with either a pre-defined behavior or by joining more than one Observable together. They can be referred to as standalone methods that create new Observables.

How operators work: Marble diagram

how operators work
https://rxjs-dev.firebaseapp.com/guide/operators
The image above shows a visual representation of how operators work. It is always a left to right and top-down progression. The Observable is first created and emits some values then at completion by the complete parameter, the defined operator takes the emitted values as input and then modifies them to give a brand new Observable.

Categories of operators

There are over 100 operators in RxJS and they can be classified into various categories, some of these are creation, transformation, filtering, joining, multicasting, error handling and utility.
CategoryOperators
Creation Operatorsajax, bindCallback, defer, empty, from, fromEvent, fromEventPattern, generate, interval, of, range, throwError, timer and iif. There are join creation operators too like combineLatest, concat, forkJoin, merge, race and zip.
Transformation Operatorsbuffer, bufferCount, bufferTime, bufferToggle, bufferWhen, concatMap, concatMapTo, exhaust, exhaustMap, expand, groupBy, map, mapTo, mergeMap, mergeMapTo, mergeScan, pairwise, partition, pluck, scan, switchMap, switchMapTo, window, windowCount, windowTime, windowToggle, windowWhen.
Filtering Operatorsaudit, auditTime, debounce, debounceTime, distinct, distinctKey, distinctUntilChange, distinctUntilKeyChanged, elementAt, filter, first, ignoreElements, last, sample, sampleTime, single, skip, skipLast, skipUntil, skipWhile, take, takeLast, takeUntil, takeWhile, throttle and throttleTime.
Joining OperatorscombineAll, concatAll, exhaust, mergeAll, startWith and withLatestFrom.
Multicasting Operators, Joining Operatorsmulticast, publish, publishBehaviour, publishLast, publishReplay and share.
Error Handling OperatorscatchError, retry and retryWhen.
Utility Operatorstap, delay, delayWhen, dematerialize, materialize, observeOn, subscribeOn, timeInterval, timestamp, timeout, timeoutWith and toArray.

Popularly used RxJS operators

If you followed this post from the start, you will have a starter project opened up in VS Code to follow up with these illustrations. In this section you will be shown how to use a few top RxJS operators in your Angular  workflow:

merge()

merge
https://rxjs-dev.firebaseapp.com/guide/operators
This operator is a join creation operator that simply merges one observable with another observable and returns the combination of them both as one Observable. Open your app.component.ts file and copy in the code block below:
import { Component, OnInit } from '@angular/core';
import { Observable, merge} from 'rxjs';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
  ngOnInit(){
   const observable1 = Observable.create((observer:any) => {
      observer.next('I am Observable 1')
  });
  
  const observable2 = Observable.create((observer:any) => {
      observer.next('I am Observable 2')
  });
  
  const observable3 = merge(observable1, observable2);
  
  observable3.subscribe((data) => console.log(data));
  }
}
Your browser console should look like this:
observable in console

of()

of ()
https://rxjs-dev.firebaseapp.com/guide/operators
This is creation operator used to create Observables from any kind of data, be it a string or an array, an object or even a promise. Test it out with this code block below:
import { Component, OnInit } from '@angular/core';
import { Observable, of} from 'rxjs';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
  ngOnInit(){
     const observable1 = of(1,2,3)
      .subscribe((data) => console.log(data));
 }
}

map()

map operator
https://rxjs-dev.firebaseapp.com/guide/operators
This is an operator defined in a pipe inside which you can modify the content of emitted values from one observable to form another new observable. In your app.component.ts file copy in the code block below:
import { Component, OnInit } from '@angular/core';
import { Observable, of} from 'rxjs';
import { map } from 'rxjs/operators';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
  ngOnInit(){
const observable1 = of('my name is lotanna');
  observable1.pipe(
    map(data => data.toUpperCase())
  ).subscribe((data) => console.log(data));
}}
Inside the pipe, you can then add your modification logic, in our case it is to convert the emitted values to upper case.

fromEvent()

fromEvent
https://rxjs-dev.firebaseapp.com/guide/operators
This operator takes any DOM element and an event name as props and then creates a new observable with it. A simple document click operator will look like this:
import { Component, OnInit } from '@angular/core';
import { fromEvent} from 'rxjs';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
  ngOnInit(){
const observable1 = fromEvent(document, 'click')
  .subscribe(() => console.log('You clicked the page!'));
}}

pluck()

pluck operators
https://rxjs-dev.firebaseapp.com/guide/operators
Just as the name implies, the pluck operator plucks a single property from an array that has multiple properties. Here is a quick example:
import { Component, OnInit } from '@angular/core';
import { from } from 'rxjs';
import { pluck } from 'rxjs/operators';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
  ngOnInit(){
from([
    { brand: 'iPhone', model: 'Xmax', price: '$1000'},
    { brand: 'Samsung', model: 'S10', price: '$850'}
])
.pipe(
  pluck('price')
)
.subscribe((data) => console.log(data));
}}

take()

take operator
https://rxjs-dev.firebaseapp.com/guide/operators
This operator takes the very occurrence of emitted events in an observable. So for instance, we already worked on a fromEvent operator for a page click. With the take operator, the new observable can only record the very first click.
import { Component, OnInit } from '@angular/core';
import { fromEvent } from 'rxjs';
import { take } from 'rxjs/operators';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
  ngOnInit(){
const observable1 = fromEvent(document, 'click')
 .pipe(
   take(2)
 )
  .subscribe(() => console.log('You clicked the page!'));
}}
This records only the first two clicks on the page as expected.

Conclusion

This article introduces RxJS operators as the main character in reactive programming. Observables are the foundation and the operators are the methods that help us consume Observables properly. We also looked at categories of operators and how to use some of the very popular ones. Happy hacking!

Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...