Jasem Al-Yafie
Jasem Al-Yafie

Reputation: 83

Canceling observable when certain observable emit data rxjs

I have three source of data :

let insightsFromDB$;// Return observable of insight object
let insightsFromAPI$;// Return observable of insight object
let insightsFromWebsockets; // Return observable of insight object
     otherObservable$
       .merge(insightsFromDB$, insightsFromAPI$) // Return observable of insight object 
      .map(insight=>insight.likes)

I want to add the insightsFromWebsockets$ to the otherObservable$ in such a way that if it's emits a value, it cancels the insight object coming from the merge operator and emit the object coming from insightsFromWebsockets$ to the map operator.

For more clarity, I have added the following marble diagram that summarize my code: enter image description here

Upvotes: 0

Views: 304

Answers (1)

Mark van Straten
Mark van Straten

Reputation: 9425

So if i understand your requirements correctly:

  • you want to receive values from streams x and z (merge)
  • once z emits you no longer are interested in values of x (takeUntil)

Example implementation:

    var x = Rx.Observable.interval(300);
    var y = Rx.Observable.timer(1500, 500).mapTo('y');
    
    x.takeUntil(y)
      .merge(y)
      .take(8).subscribe(console.log)
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.12/Rx.js"></script>

Upvotes: 2

Related Questions