当前位置:网站首页>RxJs fromEvent 工作原理分析
RxJs fromEvent 工作原理分析
2022-06-11 10:35:00 【华为云】
fromEvent(this.test, 'click').subscribe((event) => console.log(event));this.test 的赋值逻辑:
this.test = this.document.getElementById('test');每当该 id 为 test 的按钮被点击一次,则 fromEvent issue 一个新的值,内容为 MouseClick 事件:

本文介绍这个神奇的 fromEvent 的工作原理。
在 rxjs/_esm2015/index.js 下能看到所有 rxjs 支持的 operators:
fromEvent 构造函数支持最多 4 个输入参数,但我的例子里,之传入了两个。因此直接进入 Observable 对象的构造逻辑:

Observable 的构造函数,只有一个输入参数,该输入参数为一个函数。这个函数是一个匿名函数,只有函数体而无函数名称。

把该匿名函数维护在 Observable 的私有属性 _subscribe 里。
fromEvent 返回一个可观察对象,调用该对象的 subscribe 方法,给其注册观察者。

上图 observerOrNext 就是我们应用程序里,传入给 subscribe 方法的匿名函数,即使用 console.log 打印 id 为 test 的 button 被点击之后抛出的 MouseEvent 事件。
因为我们暂时没有给 fromEvent 返回的 Observable 对象指定 operator,因此第 20 行 operator 为 undefined:

调用函数 toSubscriber 创建一个新的 subscriber:
上图 nextOrObserver 就是我应用程序里指定的 console.log(event) 匿名函数,只是用 Subscriber 包了一层。
Subscriber.js 内部:Subscriber 的 destination 属性,指向了 SafeSubscriber 实例:


再回到 Observable 的 subscribe 方法。现在我们知道了,在其 toSubscriber 方法里,创建了一个 Subscriber 实例,其重要属性如上图1,2,3所示。因为没有为该 Observable 指定 operator,所以第 22 行的 IF 分支进不去,而是执行第 26 行的 else 分支。

进入 trySubscribe 方法:

_trySubscribe 是 _subscribe 方法的包裹,再加上错误处理:
文章开头提到的 Observable 私有属性 _subscribe, 指向 fromEvent.js 第14行匿名函数:

现在执行该匿名函数,函数体内 setupSubscription.
如何判断传进来的变量是 EventTarget 呢?

检查其是否具有 addEventListner 和 removeEventListener 即可。

这里把 fromEvent.js 里某个 handler 函数,作为按钮点击后的事件处理函数进行注册。

注意,这里给 click 点击事件注册的,并不是我们应用程序指定的 console.log(event), 而是 fromEvent.js 里一个内部函数,如下图所示:

当我点击 UI 的 test 按钮后,触发 click 事件,fromEvent.js 里定义的事件处理函数被调用:
还记得我们之前提到的 toSubscriber 函数调用么?将应用程序定义的 console.log(event), 包装成 SafeSubscriber,存储到 _next 属性里。

这里,最终触发 subscriber 的私有属性 _next 指向的应用程序处理逻辑:

使用了 JavaScript function 原生的 call 方法进行调用:
最终,进入了应用程序打印代码执行,谜底就此解开:
边栏推荐
- Encrypt and decrypt strings using RSA and Base64
- Ngui, map zoom in and out
- Development and source code construction of digital collection system
- MOSFET的SOA或者ASO是什么?
- Leetcode 1961. Check whether the string is an array prefix
- NGUI,背包拖拽,以及随机克隆图片知识点
- Ngui, cooling effect
- 金仓数据库KingbaseES UDP监控工具的使用
- Where is it safer to open an account for soda ash futures? How much money can you do?
- 硬件描述语言HDL
猜你喜欢

VMware install win7 virtual machine

电子设备辐射EMC整改案例

Campus lost and found applet source code can be used for graduation design

Pyramidtnt: TNT with characteristic pyramid structure

基于位置服务(LBS)的SSM的框架实现的兴趣社交软件平台设计与实现

为什么说Web3会是「创作者经济」的游戏规则改变者

Dimension types for different CV tasks

Arbitrum 基础架构:快速入门

TikTok在英国遭遇文化冲突,短期内众多员工离职

校园失物招领小程序源码可作毕业设计
随机推荐
Jszip get the file of the specified file in the uploaded zip package
网上开户是安全的吗?普通人可以开吗?
Window管理深入了解WindowManagerService
Ngui, backpack drag and drop, and random cloning of picture knowledge points
白屏时间、首屏时间
硬件描述语言HDL
1. system in Library
Use bat to write to the first line of the file
NFT products are alive
Batch add noise to data and generate new named annotation files
远程监控项目离线日志说明书
用真金做的电路板——金手指
地铁路线图云开发小程序源码和配置教程
MN梦奈宝塔主机系统V1.5版本发布
5. read the specified pathname -dirname
【机器学习理论】True Positive, True Negative, False Positive, False Negative概念
About CI framework batch export to compressed file
Arbitrum infrastructure: a quick start
Wuenda machine learning course - week 7
Jedislock redis distributed lock implementation