AJ-
AJ-

Reputation: 1783

How to combine 2 observables and emit values based on specific conditions in Angular 8

I have 2 observables that I want to combine, and use their combined version inside a combineLatest with other observables.

I'm not sure how to combine them correctly for my Use Case.

First of all let's call them ObsA and ObsB, and their combined version ObsCombined.

this.api.ObsA(), 
this.api.ObsB(), 
this.api.ObsCombined()

I need that their combined version only EMITS values following these rules:

So basically, ObsA is the most important, and the special thing is, when it emits Null, I want to make the other Obs reset and emit Null as well.

If ObsB emits a value, and ObsA last emitted was not Null, then emit this new value and the previous one from ObsA

Upvotes: 0

Views: 123

Answers (2)

PanterP
PanterP

Reputation: 83

You can try with RxJs forkJoin

const obA = this.api.ObsA();
const obB = this.api.ObsB();

forkJoin([obA, obB]).subscribe(res=>{
  console.log(res[0]);
  console.log(res[1]);
});

Upvotes: 1

Antoniossss
Antoniossss

Reputation: 32517

maybe (not checked)

merge(
 obsA.pipe(tap((v)=>{if(v==null) b.next(null)}),
 obsB
).subscribe();

Upvotes: 0

Related Questions