RxJS 系列 – Conditional and Boolean Operators
前言
前几篇介绍过了
这篇继续介绍 Conditional and Boolean Operators
参考
Docs – Conditional and Boolean Operators

defaultIfEmtpty
顾名思义, 如果一个流在 complete 前都没有任何发布, 那么至少发布一次 with default value.
const subject = new Subject<number>();
subject.pipe(defaultIfEmpty(5)).subscribe({
next: v => console.log('next', v),
complete: () => console.log('complete'),
});
subject.complete(); // 没有 next, 直接 complete
效果

every
every 和 JS array every 类似. 如果所有发布的值都符合条件那就发布 true, 不然就 false
const subject = new Subject<number>();
subject.pipe(every(v => v > 5)).subscribe(v => console.log('next', v));
subject.next(6);
subject.next(7);
subject.complete(); // console: true
注: 只有在 complete 后, RxJS 才能确保 every value match condition 哦.
但是只要其中一个 failure, 那不需要等 complete, 直接可以返回 false
const subject = new Subject<number>();
subject.pipe(every(v => v > 5)).subscribe({
next: v => console.log('next', v),
complete: () => console.log('complete'),
});
subject.next(1); // 直接 console: false, 同时也发布 complete
find
find 和 first 很像, 最大的区别是, first 要求一定要有匹配, 不然就报错.
find 则没有这个要求.
first
const subject = new Subject<number>();
subject.pipe(first(v => v === 5)).subscribe({
next: v => console.log('next', v),
error: e => console.log('error', e),
});
subject.next(1);
subject.complete();
效果

报错了, 因为 subject complete 前, 没有任何值符合 first 的匹配条件.
find
const subject = new Subject<number>();
subject.pipe(find(v => v === 5)).subscribe({
next: v => console.log('next', v),
error: e => console.log('error', e),
});
subject.complete();
效果

当 subject complete 后, 如果都没有匹配的 value, 那么至少会触发一次 next, value 是 undefined.
这就是 find 和 first 最大的区别了.
findIndex
findIndex 和 find 差不多, 只是说它返回的是 index (zero-based).
如果没有任何值匹配, complete 后至少会发布一次 next value 是 -1.
const subject = new Subject<number>();
subject.pipe(findIndex(v => v === 5)).subscribe({
next: v => console.log('next', v),
error: e => console.log('error', e),
});
subject.complete();
效果

isEmpty
当 stream complete 后, 如果没有发布过任何值, 那么至少会发布一次 next, value 是 true
const subject = new Subject<number>();
subject.pipe(isEmpty()).subscribe({
next: v => console.log('next', v),
complete: () => console.log('complete'),
});
subject.complete();
效果

如果 complete 前就已经发布值, 那么 isEmpty subscrube 会接收 false 并且触发 complete.
const subject = new Subject<number>();
subject.pipe(isEmpty()).subscribe({
next: v => console.log('next', v),
complete: () => console.log('complete'),
});
subject.next(5);
效果

一句话总结
defaultIfEmpty : 在 complete 后, 至少发布一个 next with default value
every : 在 complete 后, 过往发布的所有值都满足条件就发布 true. 发布中, 但凡其中一个值不符合条件, 直接发布 false 同时 complete 掉.
find : first match 不到会报错, find 不会, complete 后 match 不到就 next with undefined
findIndex : 和 find 一样, 但值是 index, 没有 matched 就值是 -1
isEmpty : complete 后没有任何发布就是 true, 但凡有一次发布就立马是 false 同时 complete 掉
RxJS 系列 – Conditional and Boolean Operators的更多相关文章
- [RxJS] Adding Conditional Logic with Filter
Often you only want values to proceed through your stream if they meet certain criteria, just as if ...
- RxSwift 系列(八) -- Error Handing Operators
前言 本篇文章我们将学习RxSwift中的错误处理,包括: catchErrorJustReturn catchError retry retry(_:) catchErrorJustReturn 遇 ...
- rxjs笔记(未完成)
首先是 Observable 和promise的区别, 1返回值个数,Observable 可以返回0到无数个值. 2.Promise主动推送,控制着"值"何时被 "推送 ...
- ReactiveX Operators
This documentation groups information about the various operators and examples of their usage into t ...
- Rxjava, RxAndroid, Retrofit 等库的使用
RxJava的基本用法: 关于 unSubscribe() 的调用问题: There is no need to unsubscribe in onCompleted. Take a look at ...
- ReactiveX 学习笔记(6)条件操作符
Conditional and Boolean Operators 本文的主题为处理 Observable 的条件和布尔操作符. 这里的 Observable 实质上是可观察的数据流. RxJava操 ...
- RxSwift 之官方文档
RxSwift 官方文档结构 Introduction: Subjects Transforming Observables Filtering Observables Combining Obser ...
- Rx = Observables(响应) + LINQ(声明式语言) + Schedulers(异步)
Reactive = Observables(响应)+ Schedulers(异步). Extensions = LINQ(语言集成查询) LINQ: The Operators of Reactiv ...
- RxJava中的doOnSubscribe默认运行线程分析
假设你对RxJava1.x还不是了解,能够參考以下文章. 1. RxJava使用介绍 [视频教程] 2. RxJava操作符 • Creating Observables(Observable的创 ...
- RxJava 2.x 理解-2
操作符总结: http://reactivex.io/documentation/operators.html https://github.com/ReactiveX/RxJava/wiki Ope ...
随机推荐
- 构筑开放式大数据架构,Apache Kyuubi和NDH荣登开源OSCAR
[点击了解更多网易大数据技术] 在9月16日召开的"2022 OSCAR开源产业大会"上,中国信息通信研究院发布了一系列开源研究成果和开源表彰,网易数帆发起的开源项目Apache ...
- [oeasy]python0100_wintel联盟_intel_微软_microsoft_msDOS_基尔代尔
wintel联盟 回忆上次内容 上次 了解了IBM的 背水一战 IBM 已经不在乎 软硬一体全自主的设计 了 而采用了 开放的架构 任何 硬件厂商和软件厂商 都可以来合作 以丧失 自主控制力的方式 获 ...
- oeasy教您玩转vim - 40 - # 复制粘贴
复制粘贴 回忆上节课内容 我们上次的内容是粘贴 小写p意味着在光标下面或者后面粘贴 大写P意味着在光标上面或者前面粘贴 p的意思是放上去,就是put 把什么放上去呢? 把 reg 中 " ...
- 我从 Python 潮流周刊提取了 800 个链接,精选文章、开源项目、播客视频集锦
你好,我是豌豆花下猫.前几天,我重新整理了 Python 潮流周刊的往期分享,推出了第 1 季的图文版电子书,受到了很多读者的一致好评. 但是,合集和电子书的篇幅很长,阅读起来要花不少时间.所以,为了 ...
- AX网相关图片(原创)
官网:AXA6.COM | The future is come. Copyright 2020 – 2023| AX网 Axa6.Com | All Rights Reserved
- Tomcat 线程池学习总结
前提 Tomcat 10.1.x Tomcat线程池介绍 Tomcat线程池,源于JAVA JDK自带线程池.由于JAVA JDK线程池策略,比较适合处理 CPU 密集型任务,但是对于 I/O 密集型 ...
- 句子成分&分类 被动
句子成分&分类 简单句的两个主要句子成分,分别是主语 + 谓语 谓语的核心是谓语动词 谓语 不等于 谓语动词 主语 谓语 [The rabbit] [ate a carrot] ate 为谓语 ...
- 【Vue】07 Webpack Part3 Loader
Loader是Webpack的核心概念: 除了JS文件以外我们还有CSS,图片,包括一些ES6规范的代码 或者是TypeScript各种前端类型的文件 但是最终必须统一转换成JS文件,Webpack本 ...
- ViT:拉开Trasnformer在图像领域正式挑战CNN的序幕 | ICLR 2021 —— An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
论文地址: https://arxiv.org/abs/2010.11929 Github地址: https://github.com/google-research/vision_transform ...
- 七牛云-存储区域代码:报错:"statusCode": 400,"error": incorrect region, please use up-cn-east-2.qiniup.com ——【图床】Typora 七牛云图床 配置文件
使用PicList对七牛云配置图床,报错信息: 2023-12-13 19:52:19 [PicList ERROR] { "method": "POST", ...