当前位置:网站首页>Single step debugging analysis of rxjs observable of operator
Single step debugging analysis of rxjs observable of operator
2022-07-01 22:48:00 【Wang Zixi】
Look at this simplest piece of code :
import {
Observable, of } from 'rxjs';
const observable = of(1, 2, 3);
observable.subscribe((message) => {
console.log(message);
});
Output :
Input 1,2,3 Treated as an array , Trigger fromArray Function call :
Because it doesn't exist scheduler call , So to enter subscribeToArray Branch :
subscribeToArray Is implemented in a subscribeToArray.ts
In the document , The namespace of this file is as follows :internal/util
Be careful , No 8 Yes for loop , Clearly in of Function calls do not execute , It is of Back to Observable By subscribe Execution only .subscribeToArray The logic of , A simple for loop , Call... In turn in the loop body subscriber Of next Method , Last call complete Method .
subscribeToArray Return function , Stored in Observable Constructor's _subscribe Within the properties .
And then for this of Back to Observable example , call subscribe Method .
of Observable Example of _subscribe Method , It points to just subscribeToArray Return function :
until Observable By subscribe, This function body can be executed . In the function body for Within the loop , Call one by one subscriber Of next Method :
subscriber Not created by application developers , It is rxjs Internal maintenance and use of the framework .subscriber There is an attribute destination, Point to Safesubscriber, This safesubscriber Of _next attribute , It refers to the callback function maintained by the application developer .
The execution sequence of these functions is shown in the figure below :
subscribe In addition to passing in a single callback function , And support error and complete Handle .
See the following example :
import {
Observable, of } from 'rxjs';
const observable = of(1, 2, 3);
observable.subscribe(
(message) => {
console.log(message);
},
() => {
},
() => {
console.log('complete');
}
);
complete Methods and next The execution logic of the method is similar to , The only difference is for Trigger after the execution of the loop body :
thus it can be seen ,of Created Observable yes cold Observable.
If it is a periodic transmission of data Observable, We can also use unsubscribe Unsubscribe from it . Look at the code below :
import {
interval } from 'rxjs';
const observable = interval(1000);
const subscription = observable.subscribe((x) => console.log(x));
setTimeout(() => {
subscription.unsubscribe();
}, 4500);
Output :
The Observable At output 4 Stop transmitting the value after an integer .
边栏推荐
- 基准环路增益与相位裕度的测量
- 阿洛迷茫后的思考
- Understanding of inverted residuals
- Using securecrtportable to remotely connect virtual machines
- Awoo's favorite problem (priority queue)
- 固定资产管理子系统报表分为什么大类,包括哪些科目
- 447 Bili Bili noodles warp 1
- Recent public ancestor (LCA) online practices
- 对象内存布局
- Two schemes of transforming the heat map of human posture estimation into coordinate points
猜你喜欢
Mysql——》Innodb存储引擎的索引
In the past 100 years, only 6 products have been approved, which is the "adjuvant" behind the vaccine competition
Mysql——》MyISAM存储引擎的索引
Chen Tianqi's machine learning compilation course (free)
Delete AWS bound credit card account
今日睡眠质量记录71分
Basic knowledge of ngnix
【无标题】
# CutefishOS系统~
【目标跟踪】|单目标跟踪指标
随机推荐
Mysql database detailed learning tutorial
内存导致的电脑游戏中显示hdmi无信号 从而死机的情况
Mixconv code
利用SecureCRTPortable远程连接虚拟机
好友新书发布,祝贺(送福利)
LC501. 二叉搜索树中的众数
对象内存布局
Indicator trap: seven KPI mistakes that it leaders are prone to make
效率提升 - 鼓捣个性化容器开发环境
MySQL5.7 设置密码策略(等保三级密码改造)
Yolov5.5 call local camera
【MySQL】索引的分类
Using securecrtportable to remotely connect virtual machines
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
Configure filter
Slope compensation
切面条 C语言
[literacy] deep / shallow, local / global features in machine learning image processing
Understanding of transactions in MySQL
Ffmpeg learning notes