当前位置:网站首页>Development skills of rxjs observable custom operator
Development skills of rxjs observable custom operator
2022-07-02 19:42:00 【Wang Zixi】
Operators are RxJS Kuhe Observables The foundation block . It enables us to use some keywords ( function ) To perform complex operations . Operator is just getting the source Observable、 Take action on it and return to the new Observable Function of .
rxJS Of Pipeline operators pipe Allow us to link operators . Each operator accepts a Observable And return a Observable, This consistent behavior makes linking possible . Operator Back to Observable As input to the next operator .
Here is the simplest customization Operator Example .
import {
interval, of } from 'rxjs';
function fancyOperator(source) {
return source;
}
interval(1000)
.pipe(fancyOperator)
.subscribe((value) => console.log(value));
pipe The call does not actually execute Operator The logic of the body of a function , until Observable By subscribe until ?? Is that right ?
pipeFromArray Just take out Operation The first function stored in the array .
And then call it , Input parameters this Is the source Observable.
Now we're right fancyOperator Add a few features .
import {
interval, Observable } from 'rxjs';
function fancyOperator(source) {
return Observable.create((observer) => {
observer.next('Jerry');
observer.complete();
});
}
interval(1000)
.pipe(fancyOperator)
.subscribe((value) => console.log(value));
Here we have adopted the method of transplanting flowers and trees , In the custom Operator A new Observable, This Observable Data is only transmitted once , It enters the termination state .
Look at a custom in a real project Observable Example :
import {
fromEvent, interval, Observable } from 'rxjs';
import {
filter } from 'rxjs/operators';
function filterKey(key) {
return filter((event: KeyboardEvent) => event.key === key);
}
fromEvent(document, 'keyup')
.pipe(filterKey('Enter'))
.subscribe(
(data) => console.log(data) // KeyboardEvent
);
This customization Operator The input is Enter, The output is filter Return value of the call , A new function .filter Call to receive a predicate, Returns a new function .
to glance at filter Returns the value printed when the function runs :
Put the original Observable, Transmit to filter Return the function and execute :
filter The returned function pairs the original Observable Carry on the processing , Back to a new Observable:
Trigger filterOperator Constructor for :
Put the new filterOperator The instance passes in the original Observable Of lift Method . This lift The operation is very simple , Create a new one Observable, Put the original Observable Hook up the new Observable Of source Field , At the same time Operator Assign instance to new Observable Example of operator Method .
So when you finally subscribe , The subscription is lift New after operation Observable example .
subscribe Will trigger filterOperator Of call Method :
边栏推荐
猜你喜欢
What is the Bluetooth chip ble, how to select it, and what is the path of subsequent technology development
《架构整洁之道》读书笔记(下)
Notes on hardware design of kt148a voice chip IC
定了,就是它!
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
编写完10万行代码,我发了篇长文吐槽Rust
After writing 100000 lines of code, I sent a long article roast rust
程序猿入门攻略(十二)——数据的存储
Refactoring: improving the design of existing code (Part 1)
450-深信服面经1
随机推荐
AcWing 1125. 牛的旅行 题解(最短路、直径)
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
AcWing 1127. 香甜的黄油 题解(最短路—spfa)
Refactoring: improving the design of existing code (Part 2)
LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
RPD product: super power squad nanny strategy
Getting started with typescript
How to set priorities in C language? Elaborate on C language priorities
Istio1.12:安装和快速入门
Pytorch版本、CUDA版本与显卡驱动版本的对应关系
思考变量引起的巨大变化
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
Notes de lecture sur le code propre
开始练习书法
Function high order curry realization
程序猿入门攻略(十二)——数据的存储
KT148A语音芯片ic的硬件设计注意事项
自動生成VGG圖像注釋文件
Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1
定了,就是它!