当前位置:网站首页>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 :


边栏推荐
- Gamefi chain game system development (NFT chain game development function) NFT chain game system development (gamefi chain game development source code)
- rxjs Observable 自定义 Operator 的开发技巧
- Correspondence between pytoch version, CUDA version and graphics card driver version
- JASMINER X4 1U深度拆解,揭开高效省电背后的秘密
- MySQL function
- 《重构:改善既有代码的设计》读书笔记(下)
- AcWing 1126. Minimum cost solution (shortest path Dijkstra)
- 程序猿入门攻略(十二)——数据的存储
- Codeworks round 802 (Div. 2) pure supplementary questions
- Think about the huge changes caused by variables
猜你喜欢

Py's interpret: a detailed introduction to interpret, installation, and case application

SQLite 3.39.0 发布,支持右外连接和全外连接

mysql函数

Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1

注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机

《架构整洁之道》读书笔记(下)

KT148A语音芯片ic的用户端自己更换语音的方法,上位机

Py之interpret:interpret的简介、安装、案例应用之详细攻略

嵌入式(PLD) 系列,EPF10K50RC240-3N 可编程逻辑器件

Kt148a voice chip IC software reference code c language, first-line serial port
随机推荐
数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
ShardingSphere-JDBC5.1.2版本关于SELECT LAST_INSERT_ID()本人发现还是存在路由问题
Golang并发编程——goroutine、channel、sync
Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1
After writing 100000 lines of code, I sent a long article roast rust
Solution: vs2017 cannot open the source file stdio h main. H header document [easy to understand]
《重构:改善既有代码的设计》读书笔记(下)
Quanzhi A33 uses mainline u-boot
字典
AcWing 1134. 最短路计数 题解(最短路)
Golang concurrent programming goroutine, channel, sync
AcWing 1135. 新年好 题解(最短路+搜索)
Bubble sort array
Codeforces Round #802 (Div. 2) 纯补题
《重构:改善既有代码的设计》读书笔记(上)
高并发下如何避免产生重复数据?
自动生成VGG图像注释文件
MySQL function
Postman下载安装
Understanding and function of polymorphism