Operator distinct() and its variants are an important type of Filtering operator. This lessons shows how they work and in what cases are they useful.

distinctUntilChanged():

var foo = Rx.Observable.interval(500).take(5)
.zip(Rx.Observable.of('a','b','a','a','b'), (x,y)=>y); /*
--a--b--a--a--b|
distinctUntilChanged
--a--b--a-----b|
*/ var result = foo.distinctUntilChanged(); result.subscribe(
function (x) { console.log('next ' + x); },
function (err) { console.log('error ' + err); },
function () { console.log('done'); },
);

distinct(comparFn, flushFn):

var foo = Rx.Observable.interval(500).take(5)
.zip(Rx.Observable.of('a','b','a','a','b'), (x,y)=>y); /*
--a--b--a--a--b|
distinct
--a--b---------|
*/ var result = foo.distinct(); result.subscribe(
function (x) { console.log('next ' + x); },
function (err) { console.log('error ' + err); },
function () { console.log('done'); },
); /*
"next a"
"next b"
"done"
*/

With CamperFn():

var foo = Rx.Observable.interval(500).take(5)
.zip(Rx.Observable.of('a','b','a','A','b'), (x,y)=>y); var comparFn = (x, y) => {
return x.toLowerCase() === y.toLowerCase();
} /*
--a--b--a--A--b|
distinct
--a--b---------|
*/ var result = foo.distinct(comparFn); result.subscribe(
function (x) { console.log('next ' + x); },
function (err) { console.log('error ' + err); },
function () { console.log('done'); },
); /*
"next a"
"next b"
"done"
*/

with FlusherFn:

var foo = Rx.Observable.interval(500).take(5)
.zip(Rx.Observable.of('a','b','a','A','b'), (x,y)=>y); var comparFn = (x, y) => {
return x.toLowerCase() === y.toLowerCase();
} var flushFn = Rx.Observable.interval(1100).take(1)
.concat(Rx.Observable.never()); /*
--a--b--a--A--b|
-------0--------
distinct(comparFn, flushFn)
--a--b--a-----b|
*/ var result = foo.distinct(comparFn, flushFn); result.subscribe(
function (x) { console.log('next ' + x); },
function (err) { console.log('error ' + err); },
function () { console.log('done'); },
); /*
"next a"
"next b"
"next a"
"next b"
"done"
*/

[RxJS] Filtering operators: distinct and distinctUntilChanged的更多相关文章

  1. [RxJS] Filtering operators: take, first, skip

    There are more operators in the filtering category besides filter(). This lesson will teach how take ...

  2. [RxJS] Filtering operators: takeLast, last

    Operators take(), skip(), and first() all refer to values emitted in the beginning of an Observable ...

  3. [RxJS] Filtering operators: throttle and throttleTime

    Debounce is known to be a rate-limiting operator, but it's not the only one. This lessons introduces ...

  4. [RxJS] Filtering operators: skipWhile and skipUntil

    After takeUntil() and takeWhile() function, let's have a look on skipWhile() and skilUntil() functio ...

  5. [RxJS] Filtering operators: takeUntil, takeWhile

    take(), takeLast(), first(), last(), those opreators all take number or no param. takeUtil and takeW ...

  6. Rxjs常用operators

    本文使用的是angular6内置的rxjs,版本号为6.3.3 concat 通过顺序地发出多个 Observables 的值将它们连接起来,一个接一个的. 参数: 名称 类型 属性 描述 other ...

  7. [RxJS] Transformation operators: debounce and debounceTime

    Debounce and debounceTime operators are similar to delayWhen and delay, with the difference that the ...

  8. [RxJS] Transformation operators: delay and delayWhen

    This lessons teaches about delay and delayWhen: simple operators that time shift. delay(number | dat ...

  9. [RxJS] Creation operators: interval and timer

    It is quite common to need an Observable that ticks periodically, for instance every second or every ...

随机推荐

  1. java基础部分

    1.java的基本数据类型及所占的字节 boolen  8位  1个字节 byte 8位 1个字节 char 16位 2个字节 short 16位 2个字节 int 32位 4个字 float 32位 ...

  2. yarn源代码

    Modules-------YARN consists of multiple modules. The modules are listed below as per the directory s ...

  3. 支付宝集成SDK 报错

    1.打开Demo中的错误 这是路径错误导致 解决办法:在Build Settings 中找到 Library Search Paths ,去掉其中的 /// 2.自己集成支付宝SDK时的错误 这个也是 ...

  4. 自动生成makefile的脚本

    如果需要测试某一个特性,写了一个test.cpp 某天又增加了一个utils.cpp,依此类推,测试文件越来越多 每次测试时都要手动维护一个makefile实在是不明智的 于是萌生了用脚本自动维护的念 ...

  5. Web 研发模式演变

    前不久徐飞写了一篇很好的文章:Web 应用的组件化开发.本文尝试从历史发展角度,说说各种研发模式的优劣. 一.简单明快的早期时代 可称之为 Web 1.0 时代,非常适合创业型小项目,不分前后端,经常 ...

  6. 【Maven】解决maven打jar包报错 source 1.3 中不支持注释 (请使用 -sour

    问题:maven在进行打包时,报 '请使用-source 5 或者更高版本以启用XX'的信息并导致打包失败. 原因:maven默认的编译插件的java版本较低,导致其不支持例如泛型,注解等用法. 解决 ...

  7. SecureCRT 颜色

    默认的情况下,SecureCRT 是没有颜色方案的. 也就是说:用vim,你是看不到色彩显示效果,用ll 文件和文件夹也不会有颜色区别.  那如何支持颜色显示呢?方法如下:  www.2cto.com ...

  8. 使用lombok

    Lombok是一种JavaArchive(JAR)文件,可用来消除Java代码的冗长.通过在开发环境中实现Lombok,开发人 员可以节省构建诸如hashCode()和equals()这样的方法以及以 ...

  9. 3.2 java中堆栈(stack)和堆(heap)(还在问静态变量放哪里,局部变量放哪里,静态区在哪里.....进来)

    (1)内存分配的策略 按照编译原理的观点,程序运行时的内存分配有三种策略,分别是静态的,栈式的,和堆式的. 静态存储分配是指在编译时就能确定每个数据目标在运行时刻的存储空间需求,因而在编 译时就可以给 ...

  10. UI设计师的 Android 备忘录

    Images and themes Nine-patch Colors Holo themes Naming conventions Naming conventions for drawables ...