Operator

Our last concept to cover is the operator. An operator is simply a method that acts on an Observable and changes the stream in some way. Operators are by nature immutable. This immutability makes the code easier to test and reason about. RxJS comes with over 60 operators to help in most situations where you define your streams and their behavior.

There might be a case where you need to create your own operator, but most likely there is an operator out there that already does what you want.

When you define your stream and its behavior, you will use one or more operators. It might look like the following:

let stream$ = Rx.Observable.of(1,2)
.map( x => x +1 )
.filter( x > 2 );

stream$.subscribe( data => console.log('data', data))
// data 3

Here, we can see that we are using the .map() operator and .filter() to change our stream's data. .map() operates on each value in the stream by incrementing each value by one. .filter() operates on the changed stream; a change brought about by calling .map(). It also operates on each value in the stream but conditionally decides what should be emitted. The end result is only one value being emitted, 3. There are a ton more operators, but this should give you an idea of what operators are and how they can be used.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.236.70