Focus
Focus

Reputation: 117

Rxjs5, distinct() is not working

This is the code:

if you inject the do() before the distinct() operator everything is working

just fine, but for some reasons distinct() print only the first object

_

Rx.Observable
  .interval(1000)
  .flatMap(_ => { // JSONP request
    return Rx.Observable.create(observer => {
      window.eqfeed_callback = res => {
        observer.next(res);
        observer.complete();
      };

      loadJSONP(QUAKE_URL);
    }).retry(3);
  })
  .flatMap(res => Rx.Observable.from(res.features))
  .map(quake => {
    return {
      lat: quake.geometry.coordinates[1],
      lng: quake.geometry.coordinates[0],
      size: quake.properties.mag * 10000,
      code: quake.properties.code
    };
  })
  .do(logToConsole) // DEBUG: all objects are logged to the console
  .distinct(quake => quake.code) // it only log the first object !
  .subscribe(logToConsole);

Upvotes: 3

Views: 1718

Answers (1)

Corim Bretinson
Corim Bretinson

Reputation: 66

Based on the function description provided by reactivex.io, my understanding is that the .distinct() operator should in fact work the way you have coded (whether this is a bug or not, I am unsure).

However, seeing as your code does not work (as I have tested it also), another option is to use .pluck() to pull out the value before calling .distinct(). In my testing, this will now work as expected (do not provide any arguments to the .distinct() call).

Example based on your code:

.do(logToConsole) // DEBUG: all objects are logged to the console
.pluck("code")
.distinct() // now works as expected
.subscribe(logToConsole);`

Upvotes: 3

Related Questions