前言

前几篇介绍过了

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. 「Pygors跨平台GUI」1:Pygors跨平台GUI应用研究

    「Pygors系列」一句话导读: Python.Go.Rust.C程序跨平台GUI框架研究. 一.问题 Pygors是什么? Pygors是我自己创造的一个词,就是Python.Go.Rust.C四种 ...

  2. 解决方案 | tk.entry数字验证(输入框如何保证只能输入数字)

    from tkinter import * root = Tk() # 创建文本框 entry = Entry(root) entry.pack() # 设置文本框只能输入数字 entry.confi ...

  3. Figma 替代品 Excalidraw 安装和使用教程

    如今远程办公盛行,一个好用的在线白板工具对于团队协作至关重要.然而,市面上的大多数白板应用要么功能单一,要么操作复杂,难以满足用户的多样化需求.尤其是在进行头脑风暴.流程设计或产品原型绘制时,我们常常 ...

  4. 关于android的图像视图的基本了解

    最好直接复制进去而不是拖进去 图片直接导入最好用小写字母命名,数字与字母之间要用_,而且数字好像不可以连用 centerInside,fitCenter,center的区别: centerInside ...

  5. python统计班级学生

    python统计班级学生 如下场景: 假设我有一个学生类和一个班级类,想要实现的功能为:    执行班级人数增加的操作.获得班级的总人数:    学生类继承自班级类,每实例化一个学生,班级人数都能增加 ...

  6. NVIDIA中的cupti的作用及设置: CUDA profiling tools interface —— Could not load dynamic library 'libcupti.so.10.1' —— failed with error CUPTI_ERROR_INSUFFICIENT_PRIVILEGES

    NVIDIA官方给出的说明: 可以知道,这个组件的作用是对NVIDIA的CUDA进程进行性能分析的,通过对这个组件的调用可以实现对CUDA进程的性能监测. 在使用深度学习框架时有时需要对运行的代码的C ...

  7. pycuda学习过程中的一些发现,cuda函数的初始化要在cuda内存空间初始化之后,否则会报错

    参考: https://www.cnblogs.com/devilmaycry812839668/p/15348610.html 最近在看WarpDrive的代码,其中cuda上运行的代码是使用pyc ...

  8. 【转载】 实时调度论文中经常出现的术语 ties broken arbitrary的意思 —— 看伪代码时出现 ties broken arbitrary

    看伪代码时突然看到这样的一个Ps标注, ties broken arbitrary,  不明白是啥意思,后来看到下文:https://blog.csdn.net/kangkanglhb88008/ar ...

  9. Tensorflow1.14中placeholder.shape和tf.shape(placeholder)的区别

    最近在看TensorFlow的代码,还是1.14版本的TensorFlow的,代码难度确实比pytorch的难上不是多少倍,pytorch的代码看一遍基本能看懂个差不多,TensorFlow的代码看一 ...

  10. 面向分布式强化学习的经验回放框架(使用例子Demo)——Reverb: A Framework for Experience Replay

    相关前文: 面向分布式强化学习的经验回放框架--Reverb: A Framework for Experience Replay 论文题目: Reverb: A Framework for Expe ...