当前位置:网站首页>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 方法进行调用:
最终,进入了应用程序打印代码执行,谜底就此解开:
边栏推荐
- 地铁路线图云开发小程序源码和配置教程
- Windows 安装MySQL5.7 以上的版本(压缩包形式安装)
- Development and source code construction of digital collection system
- Linker and linker options, runtime libraries and runtime library settings, configuration settings, build process and methods
- Ngui, floating blood
- Circuit board made of real gold -- golden finger
- 5.读取指定路径名-dirname
- 使用 Feign 实现声明式 REST 调用
- Pyspark case series 4-dataframe output to a single folder solution
- Tiktok encounters cultural conflict in the UK, and many employees leave in a short time
猜你喜欢

Metro roadmap cloud development applet source code and configuration tutorial

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

Leetcode 1995. 统计特殊四元组(暴力枚举)

使用 Ribbon 实现客户端负载均衡

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

International multilingual sea mall rebate product automatic matching order source code

Implementation of competition scoring system based on C language
Implementing declarative rest calls using feign

NFT products are alive

TikTok在英国遭遇文化冲突,短期内众多员工离职
随机推荐
Ngui, map zoom in and out
【K-Means】K-Means学习实例
Streaming computing knowledge
【MYSQL】存储过程的使用
Jedislock redis distributed lock implementation
FPGA infrastructure [reference ug998]
About CI framework batch export to compressed file
MYSQL(九)
链接器和链接器选项、运行时库和运行时库设置、配置设置、生成过程和方法
Implementation of competition scoring system based on C language
使用bat向文件的第一行中写入内容
Leetcode 1952. Triple divisor
Wsarecv: an existing connection was forcefully closed by the remote host
Ngui, backpack drag and drop, and random cloning of picture knowledge points
Kingbasees create database objects in batch
数字藏品系统开发源码搭建
5. read the specified pathname -dirname
Leetcode 1995. 统计特殊四元组(暴力枚举)
Golang compilation and linking parameters, runtime
Ngui, chat scroll box, UI textlist