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.NoOperator & Description
1ajaxThis operator will make an ajax request for the given URL.
2fromThis operator will create an observable from an array, an array-like object, a promise, an iterable object, or an observable-like object.
3fromEventThis operator will give output as an observable that is to be used on elements that emit an event for example buttons, clicks, etc.
4fromEventPatternThis operator will create an observable from the input function that is used to register event handlers.
5intervalThis operator will create an Observable for every time for the time given..
6ofThis operator will take in the arguments passed and convert them to observable.
7rangeThis operator will create an Observable that will give you a sequence of numbers based on the range provided.
8throwErrorThis operator will create an observable that will notify an error.
9timerThis operator will create an observable that will emit the value after the timeout and the value will keep increasing after each call.
10iifThis 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.NoOperator & Description
1CountThe count() operator takes in an Observable with values and converts it into an Observable that will give a single value
2MaxMax method will take in an observable with all values and return an observable with the max value
3MinMin method will take in an observable with all values and return an observable with the min value.
4ReduceIn 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.NoOperator & Description
1concatThis operator will sequentially emit the Observable given as input and proceed to the next one.
2forkJoinThis 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.
3mergeThis operator will take in the input observable and will emit all the values from the observable and emit one single output observable.
4raceIt 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.NoOperator & Description
1bufferThe 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.
2bufferCountIn 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.
3bufferTimeThis 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.
4bufferToggleIn 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.
5bufferWhenThis 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.
6expandThe 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.
7groupByIn groupBy operator, the output is grouped based on a specific condition and these group items are emitted as GroupedObservable.
8mapIn 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.
9mapToA constant value is given as output along with the Observable every time the source Observable emits a value.
10mergeMapIn 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.
11switchMapIn 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.
12windowIt 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.NoOperator & Description
1debounceA value emitted from the source Observable after a while and the emission is determined by another input given as Observable or promise.
2debounceTimeIt will emit value from the source observable only after the time is complete.
3distinctThis operator will give all the values from the source observable that are distinct when compared with the previous value.
4elementAtThis operator will give a single value from the source observable based upon the index given.
5filterThis operator will filter the values from source Observable based on the predicate function given.
6firstThis operator will give the first value emitted by the source Observable.
7lastThis operator will give the last value emitted by the source Observable.
8ignoreElementsThis operator will ignore all the values from the source Observable and only execute calls to complete or error callback functions.
9sampleThis operator will give the most recent value from the source Observable , and the output will depend upon the argument passed to it emits.
10skipThis operator will give back an observable that will skip the first occurrence of count items taken as input.
11throttleThis 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.NoOperator & Description
1tapThis 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.
2delayThis operator delays the values emitted from the source Observable based on the timeout given.
3delayWhenThis operator delays the values emitted from the source Observable based on the timeout from another observable taken as input.
4observeOnThis operator based on the input scheduler will reemit the notifications from the source Observable.
5subscribeOnThis operator helps to asynchronous subscribes to the source Observable based on the scheduler taken as input.
6timeIntervalThis 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.
7timestampReturns the timestamp along with the value emitted from source Observable which tells about the time when the value was emitted.
8timeoutThis operator will throw an error if the source Observable does not emit a value after the given timeout.
9toArrayAccumulates 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.NoOperator & Description
1defaultIfEmptyThis operator will return a default value if the source observable is empty.
2everyIt will return an Observable based on the input function satisfies the condition on each of the value on source Observable.
3findThis will return the observable when the first value of the source Observable satisfies the condition for the predicate function taken as input.
4findIndexThis operator based on the input scheduler will reemit the notifications from the source Observable.
5isEmptyThis 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.NoOperator & Description
1multicastA 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.
2publishThis operator gives back ConnectableObservable and needs to use connect() method to subscribe to the observables.
3publishBehaviorpublishBehaviour make use of BehaviourSubject, and returns ConnectableObservable. The connect() method has to be used to subscribe to the observable created.
4publishLastpublishBehaviour make use of AsyncSubject, and returns back ConnectableObservable. The connect() method has to be used to subscribe to the observable created.
5publishReplaypublishReplay 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.
6shareIt 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.NoOperator & Description
1catchErrorThis operator takes care of catching errors on the source Observable by returning a new Observable or an error.
2retryThis 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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *