RxJS - Operators



Operators are an important part of RxJS. An operator is a pure function that takes in observable as input and the output is also an observable.

Working with Operators

An operator is a pure function which takes in observable as input and the output is also an observable.

To work with operators we need a pipe() method.

Example of using pipe()

let obs = of(1,2,3); // an observable
obs.pipe(
   operator1(),
   operator2(),
   operator3(),
   operator3(),
)

In above example we have created a observable using of() method that takes in values 1, 2 and 3. Now on this observable you can perform different operation using any numbers of operators using pipe() method as shown above. The execution of operators will go on sequentially on the observable given.

Below is a working example −

import { of } from 'rxjs';
import { map, reduce, filter } from 'rxjs/operators';

let test1 = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
let case1 = test1.pipe(
   filter(x => x % 2 === 0),
   reduce((acc, one) => acc + one, 0)
)
case1.subscribe(x => console.log(x));

Output

30

In above example, we have used filter operator that, filters the even numbers and, next we have used reduce() operator that will add the even values and give the result when subscribed.

Here is a list of Observables that we are going to discuss.

  • Creation
  • Mathematical
  • Join
  • Transformation
  • Filtering
  • Utility
  • Conditional
  • Multicasting
  • Error handling

Creation Operators

Following are the operators we are going to discuss in Creation operator category −

Sr.No Operator & Description
1 ajax

This operator will make an ajax request for the given URL.

2 from

This operator will create an observable from an array, an array-like object, a promise, an iterable object, or an observable-like object.

3 fromEvent

This operator will give output as an observable that is to be used on elements that emit an event for example buttons, clicks, etc.

4 fromEventPattern

This operator will create an observable from the input function that is used to register event handlers.

5 interval

This operator will create an Observable for every time for the time given..

6 of

This operator will take in the arguments passed and convert them to observable.

7 range

This operator will create an Observable that will give you a sequence of numbers based on the range provided.

8 throwError

This operator will create an observable that will notify an error.

9 timer

This operator will create an observable that will emit the value after the timeout and the value will keep increasing after each call.

10 iif

This operator will decide which Observable will be subscribed.

Mathematical Operators

The following are the operators we are going to discuss in the Mathematical operator category −

Sr.No Operator & Description
1 Count

The count() operator takes in an Observable with values and converts it into an Observable that will give a single value

2 Max

Max method will take in an observable with all values and return an observable with the max value

3 Min

Min method will take in an observable with all values and return an observable with the min value.

4 Reduce

In reduce operator, accumulator function is used on the input observable, and the accumulator function will return the accumulated value in the form of an observable, with an optional seed value passed to the accumulator function.

The reduce() function will take in 2 arguments, one accumulator function, and second the seed value.

Join Operators

The following are the operators we are going to discuss in the Join operator category.

Sr.No Operator & Description
1 concat

This operator will sequentially emit the Observable given as input and proceed to the next one.

2 forkJoin

This operator will be taken in an array or dict object as an input and will wait for the observable to complete and return the last values emitted from the given observable.

3 merge

This operator will take in the input observable and will emit all the values from the observable and emit one single output observable.

4 race

It will give back an observable that will be a mirror copy of the first source observable.

Transformation Operators

The following are the operators we are going to discuss in the Transformation operator category.

Sr.No Operator & Description
1 buffer

The buffer operates on an observable and takes in argument as an observable. It will start buffering the values emitted on its original observable in an array and will emit the same when the observable taken as argument, emits. Once the observable taken as arguments emits, the buffer is reset and starts buffering again on original till the input observable emits and the same scenario repeats.

2 bufferCount

In the case of buffercount() operator, it will collect the values from the observable on which it is called and emit the same when the buffer size given to buffercount matches.

3 bufferTime

This is similar to bufferCount, so here, it will collect the values from the observable on which it is called and emit the bufferTimeSpan is done. It takes in 1 argument i.e. bufferTimeSpan.

4 bufferToggle

In the case of bufferToggle() it takes 2 arguments, openings and closingSelector. The opening arguments are subscribable or a promise to start the buffer and the second argument closingSelector is again subscribable or promise an indicator to close the buffer and emit the values collected.

5 bufferWhen

This operator will give the values in the array form, it takes in one argument as a function that will decide when to close, emit and reset the buffer.

6 expand

The expand operator takes in a function as an argument which is applied on the source observable recursively and also on the output observable. The final value is an observable.

7 groupBy

In groupBy operator, the output is grouped based on a specific condition and these group items are emitted as GroupedObservable.

8 map

In the case of map operator, a project function is applied on each value on the source Observable and the same output is emitted as an Observable.

9 mapTo

A constant value is given as output along with the Observable every time the source Observable emits a value.

10 mergeMap

In the case of mergeMap operator, a project function is applied on each source value and the output of it is merged with the output Observable.

11 switchMap

In the case of switchMap operator, a project function is applied on each source value and the output of it is merged with the output Observable, and the value given is the most recent projected Observable.

12 window

It takes an argument windowboundaries which is an observable and gives back a nested observable whenever the given windowboundaries emits

Filtering Operators

The following are the operators we are going to discuss in the filtering operator category.

Sr.No Operator & Description
1 debounce

A value emitted from the source Observable after a while and the emission is determined by another input given as Observable or promise.

2 debounceTime

It will emit value from the source observable only after the time is complete.

3 distinct

This operator will give all the values from the source observable that are distinct when compared with the previous value.

4 elementAt

This operator will give a single value from the source observable based upon the index given.

5 filter

This operator will filter the values from source Observable based on the predicate function given.

6 first

This operator will give the first value emitted by the source Observable.

7 last

This operator will give the last value emitted by the source Observable.

8 ignoreElements

This operator will ignore all the values from the source Observable and only execute calls to complete or error callback functions.

9 sample

This operator will give the most recent value from the source Observable , and the output will depend upon the argument passed to it emits.

10 skip

This operator will give back an observable that will skip the first occurrence of count items taken as input.

11 throttle

This operator will output as well as ignore values from the source observable for the time determined by the input function taken as an argument and the same process will be repeated.

Utility Operators

The following are the operators we are going to discuss in the utility operator category.

Sr.No Operator & Description
1 tap

This operator will have the output, the same as the source observable, and can be used to log the values to the user from the observable. The main value, error if any or if the task is complete.

2 delay

This operator delays the values emitted from the source Observable based on the timeout given.

3 delayWhen

This operator delays the values emitted from the source Observable based on the timeout from another observable taken as input.

4 observeOn

This operator based on the input scheduler will reemit the notifications from the source Observable.

5 subscribeOn

This operator helps to asynchronous subscribes to the source Observable based on the scheduler taken as input.

6 timeInterval

This operator will return an object which contains current value and the time elapsed between the current and previous value that is calculated using scheduler input taken.

7 timestamp

Returns the timestamp along with the value emitted from source Observable which tells about the time when the value was emitted.

8 timeout

This operator will throw an error if the source Observable does not emit a value after the given timeout.

9 toArray

Accumulates all the source value from the Observable and outputs them as an array when the source completes.

Conditional Operators

The following are the operators we are going to discuss in the conditional operator category.

Sr.No Operator & Description
1 defaultIfEmpty

This operator will return a default value if the source observable is empty.

2 every

It will return an Observable based on the input function satisfies the condition on each of the value on source Observable.

3 find

This will return the observable when the first value of the source Observable satisfies the condition for the predicate function taken as input.

4 findIndex

This operator based on the input scheduler will reemit the notifications from the source Observable.

5 isEmpty

This operator will give the output as true if the input observable goes for complete callback without emitting any values and false if the input observable emits any values.

Multicasting Operators

The following are the operators we are going to discuss in the multicasting operator category..

Sr.No Operator & Description
1 multicast

A multicast operator shares the single subscription created with other subscribers. The params that multicast takes in, is a subject or a factory method that returns a ConnectableObservable that has connect() method. To subscribe, connect() method has to be called.

2 publish

This operator gives back ConnectableObservable and needs to use connect() method to subscribe to the observables.

3 publishBehavior

publishBehaviour make use of BehaviourSubject, and returns ConnectableObservable. The connect() method has to be used to subscribe to the observable created.

4 publishLast

publishBehaviour make use of AsyncSubject, and returns back ConnectableObservable. The connect() method has to be used to subscribe to the observable created.

5 publishReplay

publishReplay make use of behaviour subject wherein it can buffer the values and replay the same to the new subscribers and returns ConnectableObservable. The connect() method has to be used to subscribe to the observable created.

6 share

It is an alias for mutlicast() operator with the only difference is that you don't have to called connect () method manually to start the subscription.

Error Handling Operators

The following are the operators we are going to discuss in error handling operator category.

Sr.No Operator & Description
1 catchError

This operator takes care of catching errors on the source Observable by returning a new Observable or an error.

2 retry

This operator will take care of retrying back on the source Observable if there is error and the retry will be done based on the input count given.

Advertisements