前言

前几篇介绍过了

Creation Operators

Filtering Operators

Join Creation Operators

Error Handling Operators

Transformation Operators

Join Operators

Utility 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的更多相关文章

  1. [RxJS] Adding Conditional Logic with Filter

    Often you only want values to proceed through your stream if they meet certain criteria, just as if ...

  2. RxSwift 系列(八) -- Error Handing Operators

    前言 本篇文章我们将学习RxSwift中的错误处理,包括: catchErrorJustReturn catchError retry retry(_:) catchErrorJustReturn 遇 ...

  3. rxjs笔记(未完成)

    首先是 Observable 和promise的区别, 1返回值个数,Observable 可以返回0到无数个值. 2.Promise主动推送,控制着"值"何时被 "推送 ...

  4. ReactiveX Operators

    This documentation groups information about the various operators and examples of their usage into t ...

  5. Rxjava, RxAndroid, Retrofit 等库的使用

    RxJava的基本用法: 关于 unSubscribe() 的调用问题: There is no need to unsubscribe in onCompleted. Take a look at  ...

  6. ReactiveX 学习笔记(6)条件操作符

    Conditional and Boolean Operators 本文的主题为处理 Observable 的条件和布尔操作符. 这里的 Observable 实质上是可观察的数据流. RxJava操 ...

  7. RxSwift 之官方文档

    RxSwift 官方文档结构 Introduction: Subjects Transforming Observables Filtering Observables Combining Obser ...

  8. Rx = Observables(响应) + LINQ(声明式语言) + Schedulers(异步)

    Reactive = Observables(响应)+ Schedulers(异步). Extensions = LINQ(语言集成查询) LINQ: The Operators of Reactiv ...

  9. RxJava中的doOnSubscribe默认运行线程分析

    假设你对RxJava1.x还不是了解,能够參考以下文章. 1. RxJava使用介绍 [视频教程] 2. RxJava操作符   • Creating Observables(Observable的创 ...

  10. RxJava 2.x 理解-2

    操作符总结: http://reactivex.io/documentation/operators.html https://github.com/ReactiveX/RxJava/wiki Ope ...

随机推荐

  1. .NET 9 预览版6发布

    微软发布了 .NET 9 的第 6 个预览版,此版本包括对运行时.SDK..NET MAUI.ASP.NET Core 和 C# 的更新,预览版没有包含太多新的主要功能或特性,因为已接近 .NET 9 ...

  2. LabVIEW图标编辑器中的文本变得模糊

    问题详述 在LabVIEW图标编辑器中将文本添加到VI图标时,如果我将字体大小设置为小于10,文本会变得模糊.当字体大小设置为大于11时,文本会正常地显示,但是字体则变得太大而无法放入图标中. 真难看 ...

  3. 前端系列-HTML5新特性

      HTML5 引入了许多新特性和改进,其中包括但不限于: 语义化标签:新增了像 <header>.<footer>.<nav>.<article>.& ...

  4. 部分解决 | ocrmypdf对中文pdf进行ocr识别后存在多余空格

    1.问题 ocrmypdf安装采用的是在windows安装方法具体看 https://media.readthedocs.org/pdf/ocrmypdf/latest/ocrmypdf.pdf 由于 ...

  5. CF452C 题解

    洛谷链接&CF 链接 题目简述 有 \(m \times n\) 张牌,有 \(n\) 个种类,每个种类有 \(m\) 张,现在抽一张放回,再抽一张,求这张牌与第一张抽出的牌种类相同的概率. ...

  6. 玄机-第二章日志分析-mysql应急响应

    目录 前言 简介 应急开始 准备工作 日志分析 步骤 1 步骤 2 步骤 3 步骤 4 总结 补充mysql中的/var/log/mysql/erro.log 记录上传文件信息的原因 前言 这里应急需 ...

  7. LLM-01 大模型 本地部署运行 ChatGLM2-6B-INT4(6GB) 简单上手 环境配置 单机单卡多卡 2070Super8GBx2 打怪升级!

    搬迁说明 之前在 CSDN 上发文章,一直想着努力发一些好的文章出来!这篇文章在 2024-04-17 10:11:55 已在 CSDN 发布 写在前面 其他显卡环境也可以!但是最少要有8GB的显存, ...

  8. JavaScript小技巧~将伪数组转成数组的方法

    伪数组:具有数组结构但是五数组相关方法的类数组结构: 方式1:Array.from() 方式2:Array.prototype.slice.call(); 用方式1吧,好记简单

  9. 安卓网络通信之 ​HttpURLConnection​ 文件上传

    文件上传分为二步,第一步选择文件 代码思路是: chooseFile()​方法用于创建一个Intent对象,并设置Intent的Action为ACTION_GET_CONTENT,这表示获取内容,即选 ...

  10. pycham配置GitHub环境【一文了解window上GitHub的基本操作】

    基础用户设置[包含用户登录.密钥生成] 网络配置 外观->系统设置->https代理->检查连接 我这里测试网址是GitHub,连接成功即可后续操作[不成功别找我,我也不知道] gi ...