16

I can use Rx.Observable.combineLatest so that I will notify the changes whenever any observable changes. But how can I know which observable changes?

var s1 = someObservable1();
var s2 = someObservable2();
Rx.Observable.combineLatest(s1, s2).subscribe(function(){
    // How to know which Observable triggers combineLatest change

});
user1668903
  • 407
  • 5
  • 11

1 Answers1

11

RxJs provides no means to accomplish this. Nevertheless you can do it with additional state:

var trigger = "";

Rx.Observable
  .combineLatest(
    s1.do(function() { trigger = "s1"; }),
    s2.do(function() { trigger = "s2"; }))
  .subscribe(function(){
    // use trigger
  });
Sergey Sokolov
  • 2,709
  • 20
  • 31
  • 2
    looks so dirty, since we store state outside of the pipeline. Doesn't look good. But definitely works – denis631 Oct 02 '18 at 10:01
  • 4
    making a tuple (item, timestamp) and combining them together than taking the item which has greater timestamp as last event works as well. Having external variable doesn't seem like right approach – denis631 Oct 02 '18 at 10:52
  • 1
    I agree with @denis631. In fact, it seems that many RX implementation have a `Timestamp()` operator built in. https://reactivex.io/documentation/operators/timestamp.html#:~:text=RxJS%20timestamp,emitted%20by%20the%20source%20Observable. – freakTheMighty May 03 '22 at 14:06