问题
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