当前位置:网站首页>Working principle analysis of rxjs fromEvent
Working principle analysis of rxjs fromEvent
2022-06-11 10:50:00 【Hua Weiyun】
fromEvent(this.test, 'click').subscribe((event) => console.log(event));this.test The assignment logic of :
this.test = this.document.getElementById('test');Whenever it's time to id by test The button is clicked once , be fromEvent issue A new value , The content is MouseClick event :

This article introduces this magical fromEvent How it works .
stay rxjs/_esm2015/index.js You can see all rxjs Supported by operators:
fromEvent Constructors support up to 4 Three input parameters , But in my case , It introduces two . So go straight to Observable Object construction logic :

Observable Constructor for , There is only one input parameter , The input parameter is a function . This function is an anonymous function , Only function body without function name .

Maintain the anonymous function in Observable Private property of _subscribe in .
fromEvent Returns an observable object , Calling the subscribe Method , Register an observer .

Upper figure observerOrNext In our application , Pass it on to subscribe Method , That is to use console.log Print id by test Of button Thrown after being clicked MouseEvent event .
Because we haven't given fromEvent Back to Observable Object designation operator, So the first 20 That's ok operator by undefined:

Call function toSubscriber Create a new subscriber:
Upper figure nextOrObserver It is specified in my application console.log(event) Anonymous functions , Just use Subscriber It's wrapped up .
Subscriber.js Inside :Subscriber Of destination attribute , Yes SafeSubscriber example :


Back to Observable Of subscribe Method . Now we know , In its toSubscriber In the method , Created a Subscriber example , Its important properties are shown in the figure above 1,2,3 Shown . Because there is no such thing as Observable Appoint operator, So the first 22 Yes IF Branch in , It's about carrying out the first 26 Yes else Branch .

Get into trySubscribe Method :

_trySubscribe yes _subscribe Method package , Plus error handling :
As mentioned at the beginning of the article Observable Private property _subscribe, Point to fromEvent.js The first 14 Self-executing anonymous function :

Now execute the anonymous function , Function body setupSubscription.
How to judge whether the variable passed in is EventTarget Well ?

Check if it has addEventListner and removeEventListener that will do .

Here is the fromEvent.js In a certain handler function , Register as the event handler after the button is clicked .

Be careful , Here to click Click on the event register , Not specified by our application console.log(event), It is fromEvent.js An internal function in , As shown in the figure below :

When I click UI Of test After button , Trigger click event ,fromEvent.js The event handler defined in is called :
Remember what we mentioned earlier toSubscriber Function call ? Application defined console.log(event), Package as SafeSubscriber, Store in _next In the attribute .

here , Final trigger subscriber Private property of _next Pointing to the application processing logic :

Used JavaScript function Native call Method call :
Final , Enter the application print code execution , The answer is solved :
边栏推荐
- PHP仿网易云原创音乐分享平台网站源码
- Install MySQL version 5.7 or above on windows (install in compressed package)
- 云开发mbti人格类型测试助手微信小程序源码
- 链接器和链接器选项、运行时库和运行时库设置、配置设置、生成过程和方法
- Mn Monet pagoda host system v1.5 release
- 数字藏品系统开发源码搭建
- MySQL foundation part common constraints summary part 2
- 微信云开发Al短视频一键换脸小程序源码
- Leetcode 1952. Triple divisor
- 5.读取指定路径名-dirname
猜你喜欢

校园失物招领小程序源码可作毕业设计

MySQL foundation part common constraints summary part 2

Sys in kingbasees_ Checksums bad block detection function
使用 Feign 实现声明式 REST 调用

PHP仿网易云原创音乐分享平台网站源码

Implementation of competition scoring system based on C language
![[machine learning theory] true positive, true negative, false positive, false negative concept](/img/59/8264d6cbd96480b59e5b8ff96320be.png)
[machine learning theory] true positive, true negative, false positive, false negative concept

MYSQL(九)

金仓数据KingbaseES 批量创建数据库对象

Practical process of selecting DC-DC switching power supply controller
随机推荐
杰理之获取 BLE 查看代码异常复位等异常情况原因【篇】
微信云开发Al短视频一键换脸小程序源码
Leetcode 1952. 三除数
MYSQL(九)
NFT 2.0: the next generation of NFT will be lean and trustworthy
SWUST oj668: the thief ran away
Campus lost and found applet source code can be used for graduation design
Wechat cloud development al short video one click face changing applet source code
VMware install win7 virtual machine
Using hystrix to implement fault-tolerant processing of microservices
Report on various activity plans of safety month 2022 (28 pages)
数字藏品系统开发源码搭建
Jedislock redis distributed lock implementation
Global pooling – pytoch
NFT 2.0: 下一代的NFT将是精简且值得信赖的NFT
MXNet对AlexNet模型的构建与实现(与LeNet的对比)
SAP Spartacus Reference App Structure
Xilinx pin constraint file xdc
Kingbasees create database objects in batch
新西兰是道路安全做的最好的国家之一