Operator distinct () and its variants is an important type of Filtering Operator. This lessons shows how they work and in what cases is they useful.
Distinctuntilchanged ():
var foo = Rx.Observable.interval. 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):
varFoo = Rx.Observable.interval ($). Take (5). zip (Rx.Observable.of (' A ', ' B ', ' A ', ' a ', ' B '), (x, y) =y);/*--a--b--a--a--b| Distinct--a--b---------|*/varresult =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 ():
varFoo = Rx.Observable.interval ($). Take (5). zip (Rx.Observable.of (' A ', ' B ', ' A ', ' a ', ' B '), (x, y) =y);varCOMPARFN = (x, y) = = { returnX.tolowercase () = = =y.tolowercase ();}/*--a--b--a--a--b| Distinct--a--b---------|*/varresult =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:
varFoo = Rx.Observable.interval ($). Take (5). zip (Rx.Observable.of (' A ', ' B ', ' A ', ' a ', ' B '), (x, y) =y);varCOMPARFN = (x, y) = = { returnX.tolowercase () = = =y.tolowercase ();}varFLUSHFN = Rx.Observable.interval (1100). Take (1). Concat (Rx.Observable.never ());/*--a--b--a--a--b|
-------0--------Distinct (COMPARFN, FLUSHFN)--a--b--a-----b|*/varresult =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