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 ...
随机推荐
- 网易数帆实时数据湖 Arctic 的探索和实践
作者 | 蔡芳芳 采访嘉宾 | 马进 网易数帆平台开发专家 数据中台也要从离线为主走向实时化,湖仓一体是第一步. 数据从离线到实时是当前一个很大的趋势,但要建设实时数据.应用实时数据还面临两个难题.首 ...
- 如何在 Vue 和 JavaScript 中截取视频任意帧图片
如何在 Vue 和 JavaScript 中截取视频任意帧图片 大家好!今天我们来聊聊如何在 Vue 和 JavaScript 中截取视频的任意一帧图片.这个功能在很多场景下都非常有用,比如视频编辑. ...
- lvs的nat和dr模式混合用
机器部署信息 lvs : 10.0.0.200 vip 10.0.0.19 外网IP , 172.168.1.19 内网IP dr rs: 10.0.0.200 vip 10.0.0.18 rip ...
- JavaScript小技巧~将伪数组转成数组的方法
伪数组:具有数组结构但是五数组相关方法的类数组结构: 方式1:Array.from() 方式2:Array.prototype.slice.call(); 用方式1吧,好记简单
- 题解:P9777 [HUSTFC 2023] Fujisaki 讨厌数学
令 \(f(n)=x^{n}+x^{-n}\). 可以发现 \(f(n)f(m)=x^{n-m}+x^{m-n}+x^{n+m}+x^{-n-m}=f(n-m)+f(m+n)\). 若 \(m=1\) ...
- 2023/4/22 SCRUM个人博客
1.我昨天的任务 学习如何使用QTdesign,并完善UI 2.遇到了什么困难 在QTable上无法理解前后端互通·的问题 3.我今天的任务 学习Qt知识QTableWidgetItem完善Pyqt5 ...
- 快速将headers转字典
使用Headers插件完成快捷操作 在pycharm的Preferences-Plugins-Marketplace下搜索Headers install安装.apply应用,ok确定 接下来只要复制相 ...
- Jmeter函数助手6-time
time函数用于获取不同格式的当前时间(年月日时分秒). Format string for SimpleDateFormat (optional):时间格式,填入如yyyyMMdd-HHmmss.d ...
- 11、SpringMVC之文件下载和上传
创建名为spring_mvc_file的新module,过程参考9.1节和9.5节 11.1.文件下载 11.1.1.创建图片目录并放置图片 11.1.2.页面请求示例 <a th:href=& ...
- 使用 Alba 对 AspnetCore项目进行测试
前言 在AspnetCore生态系统中,我们测试项目一般使用Microsoft.AspNetCore.TestHost的TestServer 到.NET6后提供的Microsoft.AspNetCor ...