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.
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.
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));
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
Following are the operators we are going to discuss in Creation operator category −
This operator will make an ajax request for the given URL.
This operator will create an observable from an array, an array-like object, a promise, an iterable object, or an observable-like object.
This operator will give output as an observable that is to be used on elements that emit an event for example buttons, clicks, etc.
This operator will create an observable from the input function that is used to register event handlers.
This operator will create an Observable for every time for the time given..
This operator will take in the arguments passed and convert them to observable.
This operator will create an Observable that will give you a sequence of numbers based on the range provided.
This operator will create an observable that will notify an error.
This operator will create an observable that will emit the value after the timeout and the value will keep increasing after each call.
This operator will decide which Observable will be subscribed.
The following are the operators we are going to discuss in the Mathematical operator category −
The count() operator takes in an Observable with values and converts it into an Observable that will give a single value
Max method will take in an observable with all values and return an observable with the max value
Min method will take in an observable with all values and return an observable with the min value.
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.
The following are the operators we are going to discuss in the Join operator category.
This operator will sequentially emit the Observable given as input and proceed to the next one.
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.
This operator will take in the input observable and will emit all the values from the observable and emit one single output observable.
It will give back an observable that will be a mirror copy of the first source observable.
The following are the operators we are going to discuss in the Transformation operator category.
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.
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.
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.
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.
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.
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.
In groupBy operator, the output is grouped based on a specific condition and these group items are emitted as GroupedObservable.
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.
A constant value is given as output along with the Observable every time the source Observable emits a value.
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.
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.
It takes an argument windowboundaries which is an observable and gives back a nested observable whenever the given windowboundaries emits
The following are the operators we are going to discuss in the filtering operator category.
A value emitted from the source Observable after a while and the emission is determined by another input given as Observable or promise.
It will emit value from the source observable only after the time is complete.
This operator will give all the values from the source observable that are distinct when compared with the previous value.
This operator will give a single value from the source observable based upon the index given.
This operator will filter the values from source Observable based on the predicate function given.
This operator will give the first value emitted by the source Observable.
This operator will give the last value emitted by the source Observable.
This operator will ignore all the values from the source Observable and only execute calls to complete or error callback functions.
This operator will give the most recent value from the source Observable , and the output will depend upon the argument passed to it emits.
This operator will give back an observable that will skip the first occurrence of count items taken as input.
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.
The following are the operators we are going to discuss in the utility operator category.
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.
This operator delays the values emitted from the source Observable based on the timeout given.
This operator delays the values emitted from the source Observable based on the timeout from another observable taken as input.
This operator based on the input scheduler will reemit the notifications from the source Observable.
This operator helps to asynchronous subscribes to the source Observable based on the scheduler taken as input.
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.
Returns the timestamp along with the value emitted from source Observable which tells about the time when the value was emitted.
This operator will throw an error if the source Observable does not emit a value after the given timeout.
Accumulates all the source value from the Observable and outputs them as an array when the source completes.
The following are the operators we are going to discuss in the conditional operator category.
This operator will return a default value if the source observable is empty.
It will return an Observable based on the input function satisfies the condition on each of the value on source Observable.
This will return the observable when the first value of the source Observable satisfies the condition for the predicate function taken as input.
This operator based on the input scheduler will reemit the notifications from the source Observable.
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.
The following are the operators we are going to discuss in the multicasting operator category..
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.
This operator gives back ConnectableObservable and needs to use connect() method to subscribe to the observables.
publishBehaviour make use of BehaviourSubject, and returns ConnectableObservable. The connect() method has to be used to subscribe to the observable created.
publishBehaviour make use of AsyncSubject, and returns back ConnectableObservable. The connect() method has to be used to subscribe to the observable created.
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.
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.
The following are the operators we are going to discuss in error handling operator category.
This operator takes care of catching errors on the source Observable by returning a new Observable or an error.
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.

    51 Lectures 
     4 hours 

 Daniel Stern
 Print
 Add Notes
 Bookmark this page