当前位置:网站首页>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 MHA high availability configuration and failover
- [untitled]
- There is no signal in HDMI in computer games caused by memory, so it crashes
- Appium automated testing foundation - Supplement: introduction to desired capabilities parameters
- Delete AWS bound credit card account
- C#/VB. Net to add text / image watermarks to PDF documents
- Fiori 应用通过 Adaptation Project 的增强方式分享
- H5 model trained by keras to tflite
- Yyds dry goods inventory # solve the real problem of famous enterprises: egg twisting machine
- 【日常训练】66. 加一
猜你喜欢

Appium automated testing foundation - Supplement: introduction to desired capabilities parameters

Slope compensation

H5 model trained by keras to tflite

Relationship and difference between enterprise architecture and project management

Delete AWS bound credit card account

Redis configuration and optimization

447 Bili Bili noodles warp 1

SAP GUI 里的收藏夹事务码管理工具

The fixed assets management subsystem reports are divided into what categories and which accounts are included

The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
随机推荐
Understanding of indexes in MySQL
MySQL中对于事务的理解
RestTemplate 远程调用工具类
[literacy] deep / shallow, local / global features in machine learning image processing
awoo‘s Favorite Problem(优先队列)
Why must digital transformation strategies include continuous testing?
QT 使用FFmpeg4将argb的Qimage转换成YUV422P
内存导致的电脑游戏中显示hdmi无信号 从而死机的情况
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
20220701
pytorch训练自己网络后可视化特征图谱的代码
Easyexcel complex data export
MySQL view exercise
Wechat open platform scanning code login [easy to understand]
Pytorch sharpening chapter | argmax and argmin functions
Compensation des créneaux horaires
CIO's discussion and Analysis on the definition of high-performance it team
YOLOv5.5 调用本地摄像头
【QT小作】封装一个简单的线程管理类
[C language] detailed explanation of malloc function [easy to understand]