当前位置:网站首页>rxjs Observable of 操作符的单步调试分析
rxjs Observable of 操作符的单步调试分析
2022-07-01 21:48:00 【汪子熙】
看这段最简单的代码:
import {
Observable, of } from 'rxjs';
const observable = of(1, 2, 3);
observable.subscribe((message) => {
console.log(message);
});
输出:
输入的 1,2,3 被当成数组处理,触发 fromArray 函数调用:
因为不存在 scheduler 调用,所以进入 subscribeToArray 分支:
subscribeToArray 的实现在一个 subscribeToArray.ts
文件里,这个文件的命名空间如下:internal/util
注意,上图第 8 行的 for 循环,显然在 of 函数调用里不会执行,而是 of 返回的 Observable 被 subscribe 时才执行。subscribeToArray 的逻辑就是,一个简单的 for 循环,循环体内依次调用 subscriber 的 next 方法,最后调用 complete 方法。
subscribeToArray 返回的函数,存储在 Observable 构造函数的 _subscribe 属性内。
然后针对这个 of 返回的 Observable 实例,调用 subscribe 方法。
of Observable 实例的 _subscribe 方法,指向的就是刚刚 subscribeToArray 返回的函数:
直到 Observable 被 subscribe,这个函数体才得以执行。在函数体的 for 循环内,逐一调用 subscriber 的 next 方法:
subscriber 并不是应用开发人员创建的,而是 rxjs 框架内部维护和使用的。subscriber 有一个属性 destination,指向 Safesubscriber,这个 safesubscriber 的 _next 属性,指向的就是应用开发人员维护的回调函数。
这段函数先后执行顺序如下图图例所示:
subscribe 除了传入一个单一的回调函数之后,还支持 error 和 complete 处理。
看下面的例子:
import {
Observable, of } from 'rxjs';
const observable = of(1, 2, 3);
observable.subscribe(
(message) => {
console.log(message);
},
() => {
},
() => {
console.log('complete');
}
);
complete 方法和 next 方法的执行逻辑类似,唯一区别是在 for 循环体执行完毕之后触发:
由此可见,of 创建的 Observable 是 cold Observable.
如果是一个周期性发射数据的 Observable,我们还可以使用 unsubscribe 对其取消订阅。看下面的代码:
import {
interval } from 'rxjs';
const observable = interval(1000);
const subscription = observable.subscribe((x) => console.log(x));
setTimeout(() => {
subscription.unsubscribe();
}, 4500);
输出:
该 Observable 在输出 4 个整数后停止发射值。
边栏推荐
- PyTorch磨刀篇|argmax和argmin函数
- 多图预警~ 华为 ECS 与 阿里云 ECS 对比实战
- 旅游管理系统
- In the past 100 years, only 6 products have been approved, which is the "adjuvant" behind the vaccine competition
- C#/VB. Net to add text / image watermarks to PDF documents
- LC501. Mode in binary search tree
- 对象内存布局
- 搜狗微信APP逆向(二)so层
- The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
- 【MySQL】explain的基本使用以及各列的作用
猜你喜欢
互联网的智算架构设计
Ida dynamic debugging apk
PyTorch磨刀篇|argmax和argmin函数
In the past 100 years, only 6 products have been approved, which is the "adjuvant" behind the vaccine competition
配置筛选机
MySQL中对于索引的理解
Explain kubernetes network model in detail
对象内存布局
Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment
三翼鸟两周年:羽翼渐丰,腾飞指日可待
随机推荐
微信开放平台扫码登录[通俗易懂]
QT 使用FFmpeg4将argb的Qimage转换成YUV422P
JVM有哪些类加载机制?
倒置残差的理解
Ida dynamic debugging apk
QT uses ffmpeg4 to convert the qimage of ARGB to yuv422p
Mysql——》索引存储模型推演
三翼鸟两周年:羽翼渐丰,腾飞指日可待
H5 model trained by keras to tflite
PyTorch磨刀篇|argmax和argmin函数
C#/VB. Net to add text / image watermarks to PDF documents
【日常训练】66. 加一
人体姿态估计的热图变成坐标点的两种方案
并发编程系列之FutureTask源码学习笔记
Indicator trap: seven KPI mistakes that it leaders are prone to make
Copy ‘XXXX‘ to effectively final temp variable
Dark horse programmer - software testing - stage 06 2-linux and database-01-08 Chapter 1 - description of the content of the Linux operating system stage, description of the basic format and common fo
Mask wearing detection method based on yolov5
Mysql——》MyISAM存储引擎的索引
Delete AWS bound credit card account