Custom Operators
Goals
- Learn about higher-order Observables.
- Learn how to use the Pipe operator to create a custom operator.
- Learn how to create a custom operator without existing operators.
Custom Operators
Creating custom operators have several benefits:
- Encapsulation of logic into a single function.
- Reusable with any Observable.
- Distributable if necessary.
Creating a Custom Operator
There are two distinct approaching for creating a custom RxJS operator:
- Use the
pipe()
function to create a new operator using existing operators. - Create a higher-order function that returns a function that is invoked with the source Observable and returns a new Observable.
Each approach has advantages and disadvantages.
First, the pipe()
function is a simpler approach to creating a new custom operator.
By using existing operators we can encapsulate the necessary behavior and logic into a new operator.
RxJS provides many operators that are at our disposal, and it is probably likely that we can create our new custom operator by using one more of existing operators to achieve the desired result.