Search code examples
angularreturnconcatenationngrxstore

How to determine what type is returned by concatwith angular store


Hi have 2 observables ,

  1. this.resolveActifsFromStore() // return Observable
  2. this.store.select(fromActifSelector.DoSelector.selectAllActifs) // return Observable<IActifModel[]>

I need to execute by order, so I use the concatWith like this

actifs$!: Observable<IActifModel[]>;

ngOnInit(){
    this.resolveActifsFromStore()
    .pipe(        
        concatWith(this.store.select(fromActifSelector.DoSelector.selectAllActifs)),
    )        
    .subscribe((val)=>{ 
              //error first return is boolean , the second is  IActifModel[]
              //HERE IS I DON´T KNOW HOW TO DO 
              if(val) { 
                        actifs$ = of(val);
                      } 
}

The problem is the val will have the return boolean, and also IActifModel[]. How can I identify the type of each return, because I want to do this, if the first observables is true then I want to assign the value of the second observable to actifs$


Solution

  • You could use concatMap to wait for the first observable to complete and then proceed to the second one:

    this.resolveActifsFromStore().pipe(
      withLatestFrom(this.store.select(fromActifSelector.DoSelector.selectAllActifs),
      concatMap(([myBoolean, myIActifModel]: [boolean, IActifModel[]]) => {
        //....
        return of({
           theBoolean: myBoolean,
           theOtherOne: myIActifModel
        });
      })
    ).subscribe(console.log);
    

    Or based on your post, you could also use forkJoin and see when the value is true do some logic with it...

    const total$ = forkJoin({
      theBoolean: this.resolveActifsFromStore(),
      theOtherOne: this.store.select(fromActifSelector.DoSelector.selectAllActifs)
    });
    
    total$.subscribe(({theBoolean, theOtherOne}) => {
       if (theBoolean) {
         //... do some logic 
       }
    });