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 ...
随机推荐
- [oeasy]python0091_仙童公司_八叛逆_intel_8080_altair8800_牛郎星
编码进化 个人电脑 计算机 通过电话网络 进行连接 极客 利用技术 做一些有趣的尝试 极客文化 是 认真研究技术的 文化 计算机 不再是 高校和研究机构高墙里面的 神秘事物 而是 生活中常见的 家用电 ...
- 题解:AT_abc359_e [ABC359E] Water Tank
背景 中考结束了,但是暑假只有一天,这就是我现在能在机房里面写题解的原因-- 分析 这道题就是个单调栈. 题目上问你第一滴水流到每个位置的时间.我们考虑,答案其实就是比当前木板高且距离当前木板最近的那 ...
- ElementUI Dialog 结合Vue实现对话框body“二分”布局
Dialog 结合Vue实现对话框body"二分"布局 需求描述 如下图, 把对话框body内容部分,分成上下两部分,其中上部分高度根据窗口大小动态调整,如果内容过多,则出现滚动条 ...
- python的基本认识
python的基本认识 初识python: python是一种跨平台的.开源的.免费的.解释型的高级编程语言: python的应用领域十分广泛.如web编程.图像处理.黑客编程.网络爬虫和科学计算等: ...
- 基于EasyTcp4Net开发一个功能较为完善的去持久化聊天软件
之前自己写了一篇介绍TCP的一些常用的功能介绍和特征,并且用代码做了示例,最终开发了一个EasyTcp4Net的TCP工具库,其最大的特色就是使用了微软提供的高性能库中的一些数据结构来处理TCP数据. ...
- 1、Springboot2简介
在学习 SpringBoot 之前,建议先具备 SpringMVC(控制层).Spring(业务层)和 Mybatis(持久层)的相关知识 1.1.概述 1.1.1.Spring的缺点 Spring ...
- 【WSDL】WebService描述语言的实践
问题的产生: 上班写了一个改接口的需求,其中涉及了一个WSDL这么一个概念 WSDL是个啥???? 翻了翻项目,里面就是个文件,以wsdl为后缀名 内容结构和XML相似,或者直接说是XML文件也可以 ...
- 解决Python使用matplotlib绘图时出现的中文乱码问题
原文地址: https://blog.csdn.net/qq_33254766/article/details/120304721 全文略,详细见原文. 解决方法: # 设置字体的属性 # plt.r ...
- 大模型时代该用什么样的显卡 —— 实验室新进两块A800显卡
具体如图: (这两个显卡是专为实验室的大模型方向提供的) 关于A800显卡的性能参数: (上图源自:https://www.zhihu.com/question/618932114/answer/32 ...
- mpi4py和cupy的联合应用(anaconda环境):GPU-aware MPI + Python GPU arrays
Demo代码: from mpi4py import MPI import cupy as cp comm = MPI.COMM_WORLD size = comm.Get_size() rank = ...