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 ...
随机推荐
- Task异步多线程
不废话,直接贴上要实现的效果和代码... [1]直接使用Lambda表达式是实现多线程: using System; using System.Collections.Generic; using S ...
- JDK工具包:jshell
JDK工具包:jshell 简介 使用 jshell 工具可以执行 Java 代码,从而立即获取结果. 您可以输入 Java 定义(变量.方法.类等等) 例如: int x = 8 或 Java 表达 ...
- Docker 基于Dockerfile创建镜像实践
需求描述 简单说,就是创建一个服务型的镜像,即运行基于该镜像创建的容器时,基于该容器自动开启一个服务.具体来说,是创建一个部署了nginx,uwsgi,python,django项目代码的镜像,运行基 ...
- Python 实时获取任务请求对应的Nginx日志
需求描述 项目需求测试过程中,需要向Nginx服务器发送一些用例请求,然后查看对应的Nginx日志,判断是否存在特征内容,来判断任务是否执行成功.为了提升效率,需要将这一过程实现自动化. 实践环境 P ...
- 题解:P10320 勇气(Courage)
P10320 勇气(Courage) 推导过程 本题是一道数学题,重点是如何推导出正确式子. 首先,先特判几个特殊点: 当 \(n>=2\) 且 \(x=2\) 时,是不存在解的,战斗力无论何时 ...
- 关于构建一个可视化+code系统的思路
思路是有参考UE的现有功能,加之前的逻辑. 大概分为三个模块: 底层, 即native层 ,这一层实际上分为三个部分: 1.GUI层的解析,2.数据存储 3.Code的解析 这三部分关键在于他们 ...
- docker 6.1测试
https://www.cnblogs.com/xiugeng/p/10193333.html#_label1 1.设置重启策略 [root@docker ~]# cat /etc/docker/da ...
- python获取引用对象的个数
python获取引用对象的个数 使用sys.getrefcount()来获取当前对象被引用了多少次,返回的结果比实际大1 import sys class A: pass a = A() #创建实例对 ...
- 【ECharts】02 饼图
饼状图: <!-- 为ECharts准备一个具备大小(宽高)的Dom --> <div id="main" style="width: 600px;he ...
- 使用python对Excel表格某个区域保存为图片
实际工作中,我们经常会把表格某个区域(如:A1:F5)或某个图形保存为图片,如何用python自动做到这一点?不知屏幕前的小伙伴有没有遇到过类似的需求,此刻脑海里有木有一丢丢思路. python操作e ...