当前位置:网站首页>【Question】rxjs/operator takeWhile vs takeUntil
【Question】rxjs/operator takeWhile vs takeUntil
2022-06-10 11:15:00 【zsy_ one thousand nine hundred and ninety-one】
Question why
To be investigated , Welcome to the discussion ~
Problem description
To avoid memory leaks ,long lived observable Need to be unsubscribe, In general use takeWhile and takeUntil. But in the process of using, I found that sometimes takeWhile Not working properly , do not know why ??
The body code is as follows ,takeUntil(notifier) It works , But instead takeWhile(() => this.alive) Cannot print out successfully form statusChange complete.
import {
Component, Input, OnChanges, OnDestroy, OnInit } from '@angular/core';
import {
FormGroup } from '@angular/forms';
import {
Subject } from 'rxjs';
import {
debounceTime, takeUntil, takeWhile } from 'rxjs/operators';
@Component({
selector: 'dialog',
templateUrl: './dialog.component.html',
})
export class DialogComponent implements OnInit, OnChanges, OnDestroy {
@Input() innerForm?: FormGroup;
destory$ = new Subject();
constructor() {
}
ngOnInit(): void {
}
ngOnChanges(): void {
if (this.innerForm) {
this.innerForm.statusChanges.pipe(takeUntil(this.destory$)).subscribe((status) => {
console.log('form statusChange next', status);
}, (err) => {
console.log('form statusChange error', err);
}, () => {
console.log('form statusChange complete');
});
}
}
ngOnDestroy(): void {
// If there is no such sentence, just complete Words , It doesn't print form statusChange complete
this.destory$.next();
// There is nothing in this sentence , As long as there is the above sentence ,takeUntil Will let source observable complete. There are also articles that say complete Must call , Otherwise, there is a risk of memory leakage , do not know why ?? After the experiment, it will print form statusChange complete
this.destory$.complete();
}
}
takeWhile and takeUntil Introduce
takeWhile
The more surprising thing is , I always thought takeWhile(condition), as long as condition by true From the source observable emit Out value , But after experiments, it was found that , When condition fail after ,source observable I was block Live in the , Even in the back source observable Can meet the conditions , Not even emit A new value . The following figure illustrates takeWhile.
takeUntil
takeUntil(notifier) Used in source observable By another notifier observable Control of emit It's worth it , When notifier observable When the first value is issued ,source observable stop it emit The new value . Here's the picture notifier observable by timer(5000),5s after source observable Stopped. emit The new value .
The above picture is from the appendix reference article 【RxJS — take vs takeLast vs takeWhile vs takeUntil operators】, Thanks to the author Vinodhec
Reference resources
边栏推荐
- In commemoration of the 16th day of the first month, the total number of visits to the studio exceeded one million
- Yutai semiconductor rushes to the scientific innovation board: the annual revenue is 830million, and the actual controller is American
- 软件测试质量与保证大题
- 二叉树的前序中序后序递归遍历和非递归遍历(c语言版本)
- Carbon reduction in the construction industry is by no means a fresh idea experts suggest strengthening the transformation of rural buildings
- 常用颜色RGB、灰度值、取色值、透明度。
- Niuke Mianjing 02
- 剑指位运算
- In 2021, the revenue of China's electronic components will be the first, revealing the development path of precision and high quality of Tencent
- Digital commerce cloud furniture industry supplier evaluation management system: improve the core competitiveness of furniture enterprises and realize digital collaborative management
猜你喜欢

珠海高远电能科技有限公司30%股权转让,来自塔米狗分享

数商云商业服务业SaaS管理系统:实现高效业务协作,助力企业完善数字化转型之路

软件测试质量与保证大题

解析:稳定币不是「稳定的币」 其本质是一种产品

kubernetes常用命令-1-命令补全

cocoslua在vs2013的调试方法

In 2021, the revenue of China's electronic components will be the first, revealing the development path of precision and high quality of Tencent

音质出色的降噪旗舰,女毒必选,贝壳王子MO3体验

杰理之长按复位及高电平复位【篇】

Transfer of 30% equity of Zhuhai Gaoyuan Electric Energy Technology Co., Ltd., shared by tamigou
随机推荐
动态规划(mid)
Is there any difference between exception and error
Uniapp implements authorized login
87.(leaflet之家)leaflet军事标绘-直线箭头修改
PID光离子化检测器用于电厂压缩空气含油量的在线监测
音质出色的降噪旗舰,女毒必选,贝壳王子MO3体验
PV operation daily question - orange apple question (Preliminary Edition)
MySQL数据类型
解析:稳定币不是「稳定的币」 其本质是一种产品
杰理之BLE OTA 升级需要关闭不必要的外设【篇】
香槐路的香槐花,匆匆四年无归期。
AttributeError: module ‘gym.envs.box2d‘ has no attribute ‘LunarLander‘ 解决办法
Why is your next computer a computer? Explore different remote operations
Switch the Taobao image of NPM
string类及学习使用文档
Fundamentals of software testing
LocalDateTime与String日期互相转换
远程解决win10上keyboard和chrome不work的两例问题(by quqi99)
Recursive traversal and non recursive traversal of binary tree (C language version)
杰理之BLE功耗异常【篇】