📜  RxJS-运算符

📅  最后修改于: 2020-10-20 05:35:49             🧑  作者: Mango


运算符是RxJS的重要组成部分。运算符是一个纯函数,它以可观察值作为输入,而输出也是可观察的。

与运营商合作

运算符是一个纯函数,它以可观察的输入作为输入,而输出也是可观察的。

要与运算符,我们需要一个pipe()方法。

使用pipe()的示例

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

在上面的示例中,我们创建了一个可观察的using ()方法,其值分别为1、2和3。现在,在该可观察对象上,您可以使用pipe()方法使用任意数量的运算符执行不同的操作,如上所示。运算符的执行将按可观察到的顺序继续进行。

以下是一个工作示例-

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

在上面的示例中,我们使用了filter运算符,该函数对偶数进行过滤,接下来,我们使用了reduce()运算符,该运算符将添加偶数值并在订阅时给出结果。

这是我们将要讨论的Observable的列表。

  • 创建
  • 数学的
  • 加入
  • 转型
  • 筛选
  • 效用
  • 有条件的
  • 多播
  • 错误处理

创建运算符

以下是我们将要在创建运算符类,讨论运算符-

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.

数学运算符

下面是我们要在数学运算符类,讨论运算符-

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.

加盟运营商

以下是我们将要讨论的加入运算符类的运算符。

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.

转型运营商

下面是我们要在转型运算符类别,讨论运算符。

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

筛选运算符

下面是我们要在过滤运算符类别,讨论运算符。

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.

公用事业运营商

以下是我们将要在公用事业运算符类别,讨论运算符。

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.

条件运算符

以下是我们将在条件运算符类,讨论运算符。

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.

组播运营商

下面是我们要在多播运算符类,讨论运算符..

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.

错误处理运算符

以下是我们将要在错误处理运算符类,讨论运算符。

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.