- RxJS - Discussion
- RxJS - Useful Resources
- RxJS - Quick Guide
- RxJS - Working with RxJS & ReactJS
- RxJS - Working with RxJS & Angular
- RxJS - Working with Scheduler
- RxJS - Working with Subjects
- RxJS - Working with Subscription
- RxJS - Operators
- RxJS - Observables
- RxJS - Latest Updates
- RxJS - Environment Setup
- RxJS - Overview
- RxJS - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 pst of Observables that we are going to discuss.
Creation
Mathematical
Join
Transformation
Filtering
Utipty
Conditional
Multicasting
Error handpng
Creation Operators
Following are the operators we are going to discuss in Creation operator category −
Sr.No | Operator & Description |
---|---|
1 |
This operator will make an ajax request for the given URL. |
2 |
This operator will create an observable from an array, an array-pke object, a promise, an iterable object, or an observable-pke object. |
3 |
This operator will give output as an observable that is to be used on elements that emit an event for example buttons, cpcks, etc. |
4 |
This operator will create an observable from the input function that is used to register event handlers. |
5 |
This operator will create an Observable for every time for the time given.. |
6 |
This operator will take in the arguments passed and convert them to observable. |
7 |
This operator will create an Observable that will give you a sequence of numbers based on the range provided. |
8 |
This operator will create an observable that will notify an error. |
9 |
This operator will create an observable that will emit the value after the timeout and the value will keep increasing after each call. |
10 |
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 |
The count() operator takes in an Observable with values and converts it into an Observable that will give a single value |
2 |
Max method will take in an observable with all values and return an observable with the max value |
3 |
Min method will take in an observable with all values and return an observable with the min value. |
4 |
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 |
This operator will sequentially emit the Observable given as input and proceed to the next one. |
2 |
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 |
This operator will take in the input observable and will emit all the values from the observable and emit one single output observable. |
4 |
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 |
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 |
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 |
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 |
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 |
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 |
The expand operator takes in a function as an argument which is appped on the source observable recursively and also on the output observable. The final value is an observable. |
7 |
In groupBy operator, the output is grouped based on a specific condition and these group items are emitted as GroupedObservable. |
8 |
In the case of map operator, a project function is appped on each value on the source Observable and the same output is emitted as an Observable. |
9 |
A constant value is given as output along with the Observable every time the source Observable emits a value. |
10 |
In the case of mergeMap operator, a project function is appped on each source value and the output of it is merged with the output Observable. |
11 |
In the case of switchMap operator, a project function is appped 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 |
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 |
A value emitted from the source Observable after a while and the emission is determined by another input given as Observable or promise. |
2 |
It will emit value from the source observable only after the time is complete. |
3 |
This operator will give all the values from the source observable that are distinct when compared with the previous value. |
4 |
This operator will give a single value from the source observable based upon the index given. |
5 |
This operator will filter the values from source Observable based on the predicate function given. |
6 |
This operator will give the first value emitted by the source Observable. |
7 |
This operator will give the last value emitted by the source Observable. |
8 |
This operator will ignore all the values from the source Observable and only execute calls to complete or error callback functions. |
9 |
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 |
This operator will give back an observable that will skip the first occurrence of count items taken as input. |
11 |
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. |
Utipty Operators
The following are the operators we are going to discuss in the utipty operator category.
Sr.No | Operator & Description |
---|---|
1 |
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 |
This operator delays the values emitted from the source Observable based on the timeout given. |
3 |
This operator delays the values emitted from the source Observable based on the timeout from another observable taken as input. |
4 |
This operator based on the input scheduler will reemit the notifications from the source Observable. |
5 |
This operator helps to asynchronous subscribes to the source Observable based on the scheduler taken as input. |
6 |
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 |
Returns the timestamp along with the value emitted from source Observable which tells about the time when the value was emitted. |
8 |
This operator will throw an error if the source Observable does not emit a value after the given timeout. |
9 |
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 |
This operator will return a default value if the source observable is empty. |
2 |
It will return an Observable based on the input function satisfies the condition on each of the value on source Observable. |
3 |
This will return the observable when the first value of the source Observable satisfies the condition for the predicate function taken as input. |
4 |
This operator based on the input scheduler will reemit the notifications from the source Observable. |
5 |
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 |
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 |
This operator gives back ConnectableObservable and needs to use connect() method to subscribe to the observables. |
3 |
pubpshBehaviour make use of BehaviourSubject, and returns ConnectableObservable. The connect() method has to be used to subscribe to the observable created. |
4 |
pubpshBehaviour make use of AsyncSubject, and returns back ConnectableObservable. The connect() method has to be used to subscribe to the observable created. |
5 |
pubpshReplay 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 |
It is an apas for mutpcast() operator with the only difference is that you don t have to called connect () method manually to start the subscription. |
Error Handpng Operators
The following are the operators we are going to discuss in error handpng operator category.
Sr.No | Operator & Description |
---|---|
1 |
This operator takes care of catching errors on the source Observable by returning a new Observable or an error. |
2 |
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. |