当前位置:网站首页>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 .
边栏推荐
- MySQL stored procedure
- Mysql5.7 set password policy (etc. three-level password transformation)
- The leader of the cloud native theme group of beacon Committee has a long way to go!
- 【目标跟踪】|单目标跟踪指标
- 阿洛迷茫后的思考
- flink sql-client 使用 对照并熟悉官方文档
- 【MySQL】索引的分类
- 隐藏用户的创建和使用
- 效率提升 - 鼓捣个性化容器开发环境
- Intelligent computing architecture design of Internet
猜你喜欢
随机推荐
Mysql——》索引存储模型推演
黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,
Measurement of reference loop gain and phase margin
Relationship and difference between enterprise architecture and project management
搜狗微信APP逆向(二)so层
PyTorch磨刀篇|argmax和argmin函数
Appium automated testing foundation - Supplement: introduction to desired capabilities parameters
切面条 C语言
Gaussdb (DWS) active prevention and troubleshooting
【无标题】
Appium自动化测试基础 — 补充:Desired Capabilities参数介绍
从零开始学 MySQL —数据库和数据表操作
[jetcache] how to use jetcache
园区全光技术选型-中篇
redis配置文件中常用配置详解[通俗易懂]
Yolov5.5 call local camera
Recent public ancestor offline practice (tarjan)
[ecological partner] Kunpeng system engineer training
Rust language - Introduction to Xiaobai 05
rxjs Observable of 操作符的单步调试分析