Home > Back-end >  Get the value of obserable inside obserable rxjs
Get the value of obserable inside obserable rxjs

Time:07-02

I have below Observable

  config$: Observable<QueryBuilderConfig>;

this.config$.pipe(map(item => {
  return {
    fieldGroups: [
      { id: CustomAttributeEntityType.Risk, label: this.l('Risk') },
      { id: CustomAttributeEntityType.RiskAssessment, label: this.l('Risk Assessment') }
    ],
    fields: this.metricCreateConfigureStore.queryBuilderFields$, // Get the value from obserable
    allowEmptyRules: true,
    allowEmptyRulesets: false
  }
}));

In the field attribute, I want to get the value from another observable, something like below

 fields: this.metricCreateConfigureStore.queryBuilderFields$

queryBuilderFields$ return Observable as below

readonly queryBuilderFields$: Observable<{ [key: string]: QueryBuilderFieldsDto }> = this.select(state => state.queryBuilderFields);

How can I get the value, without subscribe over there??

CodePudding user response:

Depends on what you exactly mean by "value from other observable": two streams emitting independently, and you want to catch values from both? That's

combineLatest([stream1$, stream2$])
    .subscribe(([val1$, val2$]) => //...

Or use value from one stream to create another one? That's switchMap (or maybe one of its siblings, like concatMap, mergeMap etc.):

stream1$.pipe(
    tap(/* maybe some side effects here */),
    switchMap(value1 => generateStream2(value1))
).subscribe(value2 => //...

CodePudding user response:

That would be something like


this.metricCreateConfigureStore.queryBuilderFields$.pipe(
   switchMap((fields) => this.config$.pipe(
      map((item) => {
         return { ... use fields and item }
      })
   ))
)


And also, I would not use this.l() function call inside the stream handlind, but put a parameter to be used, so pure function.

  • Related