Creating custom pipe operators by mixing existing operators
RxJS provides a powerful set of built-in operators that can be used to transform, filter, and combine streams of data.
Filter null
and undefined
values – filter in practice
In RxJS, filter
is a higher-order function that only emits items from the original sequence that satisfy a specified predicate function.
Thanks of T & {}
, null
and undefined
types will be removed from types.
P.S: T & {}
means exactly the same like Exclude<T, null | undefined>
.
Filter passed values as an argument – advanced usage of filter operator
Instead of hardcoded null
and undefined
values in the function you can move it to the function’s argument.
Thanks to Exclude<T, G[number]>
TypeScript can exclude defined types.
Operator for auto-complete – combine three operators into one
Auto-complete, or word completion, is a feature in which an application predicts the rest of a word a user is typing. It is very important to limit the number of requests to a service to prevent excessive usage and protects the service from being overloaded or overwhelmed.
See the example of how to create a custom auto-complete operator in RxJS to protect your service from redundant calls.
auditTime
ignores source values for a given time in milliseconds then emits the most recent value from the source. Thanks to that each change will emit value only once per 500 milliseconds.
distinctUntilChanged
emit value to the next operator only when a value has been changed.
filter
will emit value only if the input has more than 3 characters.
Logger operator – single operator with predefined values
The purpose of a logger is to help developers understand the behavior of a system, diagnose issues, and troubleshoot problems. See the example of how to create a custom logger operator in RxJS to log each event to the console.
Thanks to the defined colors in the log console, the output will look colorful and intuitive.
Creating a custom pipe operator based on a custom implementation
Creating a custom pipe operator should be the last step to resolving your problem. Try to resolve a problem using available operators, if it is not possible only then try to implement a custom operator.
To create a custom pipe operator, we first need to define a function that takes an observable as input and returns a new observable. This function should contain the logic for the transformation we want to perform.
You should always tear down created subscriptions inside a new Observable
.
To do it return created subscription or add a custom callback
which will be called when the source will be unsubscribed.
Thanks of T & {}
, null
and undefined
types will be removed from types.
P.S: T & {}
means exactly the same like Exclude<T, null | undefined>
.
Custom pipe operator with arguments
To create a custom pipe operator with custom arguments, we need to create a function generator.
Do not forget to tear down your subscription!
Thanks to Exclude<T, G[number]>
TypeScript can exclude defined types.
Conclusion
Creating a custom pipe operator in RxJS can help simplify complex data transformations and make your reactive programming code more readable and reusable, but remember, creating your own pipe operator from scratch should be your last choice.