RxJs Branching If Else Logic

前提是你 提交于 2019-12-31 04:32:08

问题


onSave() {
  // event handler 
  const save$ = combineLatest(this.configData.data, this.layerService.layerData)
    .pipe(
      filter(([configData]) => !_.isEmpty(configData)),
      take(1),
      tap(_ => this.loadingService.showLoading()),
      map(data => this.createSaveConfig(data)),
      flatMap(dataJSON => this.saveService.save(dataJSON)),
    ).share();

  const saveAndReload$ = save$.pipe(
    filter(_ => !this.savedId),
    pluck('savedId'),
    flatMap(savedId => this.saveService.getData(savedId)),
    tap(reportData => this.retrievedReport = reportData),
    pluck('savedId'),
    tap(savedId => {
      this.savedId = savedId;
      this.location.replaceState(`/notes/${this.savedId}`);
    })
  );

  save$.merge(saveAndReload$).subscribe(_ => {
    this.loadingService.hideLoading();
  }, err => this.errorService.error(err));

}

On initial save saveId doesn't exist and in that case specific operation is performed which is changing the url with saveId. And during subsequent saveId would be present so, in that case no need to change the url. The problem with above is that during first save subscribe method is called twice.

来源:https://stackoverflow.com/questions/52412091/rxjs-branching-if-else-logic

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