Typescript 使用combineAll替换switchMap和map?
我试着用Typescript 使用combineAll替换switchMap和map?,typescript,rxjs,observable,angular2-services,side-effects,Typescript,Rxjs,Observable,Angular2 Services,Side Effects,我试着用combineAll来组合所有的东西,我尝试了不同的组合,但没能成功。 但我通过使用所有这些开关映射/映射进行这种难看的嵌套,成功地获得了一些工作代码。 如何使用combineAll获得相同的结果 @Effect() SelectItem$: Observable<Action> = this.actions$ .ofType(supernode.ActionTypes.SET_SELECTED_ITEM) .debounceTime(100) .
combineAll
来组合所有的东西,我尝试了不同的组合,但没能成功。
但我通过使用所有这些开关映射/映射进行这种难看的嵌套,成功地获得了一些工作代码。
如何使用combineAll
获得相同的结果
@Effect()
SelectItem$: Observable<Action> = this.actions$
.ofType(supernode.ActionTypes.SET_SELECTED_ITEM)
.debounceTime(100)
.switchMap(() => {
return this.itemService.isEditable().switchMap(
edit => this.itemService.isSharable().switchMap(
share => this.itemService.isListable().map(
list => new item.SetShareStatus({
listable: list, editable: edit, sharable: share
})
)
)
).take(1);
})
.catch(this.handleError);
@Effect()
SelectItem$:Observable=this.actions$
.of类型(supernode.ActionTypes.SET_SELECTED_项)
.debounceTime(100)
.switchMap(()=>{
返回此.itemService.isEditable().switchMap(
edit=>this.itemService.isSharable().switchMap(
share=>this.itemService.isListable().map(
列表=>new item.SetShareStatus({
可列表:列表,可编辑:编辑,可共享:共享
})
)
)
).采取(1)项措施;
})
.接住(这个.把手错误);
我建议使用CombineTest
操作符:
...
.switchMap(() =>
Observable.combineLatest(
this.itemService.isEditable(),
this.itemService.isSharable(),
this.itemService.isListable(),
(editable, sharable, listable) =>
new SetShareStatus({ editable, sharable, listable })
)
.take(1)
)
combineAll
应应用于高阶可观察对象,即可观察对象的可观察对象,因此如果您真的想使用它,则需要首先从可观察对象中创建高阶可观察对象,例如:
Observable.of(
this.itemService.isEditable(),
this.itemService.isSharable(),
this.itemService.isListable()
)
.combineAll((editable, sharable, listable) =>
new SetShareStatus({ editable, sharable, listable }))
我不确定您调用的所有函数都应该做什么,但在我看来,您不需要使用
combineAll
或switchMap
。我认为使用zip()
或forkJoin()
会更好:
我认为
combineLatest()
不是一个好的选择,因为当它的任何源观测值发出时,它就会发出,我认为这是你不想要的。你试过combineLatest()
?
.switchMap(() => Observable.forkJoin(
this.itemService.isEditable()
this.itemService.isSharable()
this.itemService.isListable()
))
.map(results => new SetShareStatus({
listable: results[2], editable: results[1], sharable: results[1]
}))
})