passing concatMap result in to next concatMap in rxjs

扶醉桌前 提交于 2019-12-11 15:45:30

问题


You can see I have a method , which is working fine as I expected

loadTemplates(configUrl: any, templateId: string): Observable<any> {
    this.getConfiguration(configUrl)
      .pipe(

        concatMap(configResponse =>
          this.getTemplate(
            CONFIGURATION_URL +
              "templates/" +
              configResponse.TemplateSettings.RootTemplate.Path,
            configResponse
          )
        ),
        concatMap(rootTemplate =>
          this.templateEngine.getAllChildTemplates(rootTemplate)       
        ),
        concatMap(childTemplates=>this.templateEngine.createTemplateToRender(childTemplates,""))
      )
      .subscribe(finalresponse=> {

        debugger;
      });

  }
}

Execution

  1. getTheConfiguration() => basedontheconfigresponse=>
  2. calling getTemplate(basedontheconfigresponse)=>basedntheTemplateresponse=>
  3. calling getAllChildTemplates(basedntheTemplateresponse )=> basedonthechildtemplateresponse=>
  4. calling createTemplate(basedonthechildtemplateresponse,"")

in the 4 bullet point I passing an empty parameter to the createTemplate method , actually I have to pass basedntheTemplateresponse(in code rootTemplate). Right now I am doing a tweak by setting up when execute getAllChildTemplates

getAllChildTemplates(parentTemplate: string) {
    this.currentrootTemplate = parentTemplate;
    var services = this.getChildTemplateServiceInstance(parentTemplate);
    return forkJoin(services);
  }

is there any right way to pass the rootTemplate from the pipe itself to the next concatMap ? I am not sure is this is the right approach.


回答1:


You can just map the result from the inner Observable into an array of the current response and the previous one:

concatMap(rootTemplate =>
  this.templateEngine.getAllChildTemplates(rootTemplate).pipe(
    map(childTemplates => [rootTemplate, childTemplates])
  )
),
concatMap(([rootTemplate, childTemplates]) => 
  this.templateEngine.createTemplateToRender(childTemplates, rootTemplate)
)


来源:https://stackoverflow.com/questions/52953654/passing-concatmap-result-in-to-next-concatmap-in-rxjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!