0

I have two observable that I am listening too with the merge operator when one of them is fire, or both I need to execute the handler only once, How can I do this with rx?

var source1 = Rx.Observable.interval(1000);

var source2 = Rx.Observable.interval(1000);

var source = Rx.Observable.merge(
    source1,
    source2)
    .subscribe(() => console.log('This needs to run only once and not kill the stream'))
ng2user
  • 1,997
  • 6
  • 22
  • 30

1 Answers1

0

You can use selector function of multicast

Optional selector function that can use the multicasted source stream as many times as needed, without causing multiple subscriptions to the source stream. Subscribers to the given source will receive all notifications of the source from the time of the subscription forward.

http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-multicast

 function factory() {
   return new Rx.Subject();
 }

 source.multicast(factory, function(shared) {
   return shared.take(1).do(x=>console.log(x)).concat(shared);
 })
 .subscribe();
Julia Passynkova
  • 17,256
  • 6
  • 33
  • 32