当我执行商店调度时,结果发生错误(500)。我想要的是,一旦发生错误,这将阻止操作进一步传播。 理想情况下,该操作会停止,从而就像从未执行过该操作一样。甚至商店也不应改变。有办法吗?
我试图使结果返回一个空的可观察值,因为我读到这导致已完成的可观察值完成并且其订阅者被取消订阅-这是可观察合约的一部分。
但是,我无法返回可观察到的空白。请指教。我已经在评论中保留了所有尝试的内容。
效果
@Effect() getArea$: Observable<Action> = this.actions
.ofType(areaActionTypes.AREA_FETCH)
.map((action: areaActions.FetchAreaAction) => action.payload)
.mergeMap((areaPayload: IAreaPayload) =>
this.areaService.getArea(areaPayload)
.map(area => {
return new areaActions.FetchAreaSuccessAction(area);
})
.catch((err: any) => {
// original way I had it which does NOT prevent the
// observable from completing and the handling of this action continues
return of(new areaActions.FetchAreaFailAction({ error: err.message }));
// NONE OF THESE BELOW WORK. CODE DOESN'T EVEN RUN BECAUSE
// THE EFFECT IS EXPECTING A TYPE Observable<Action>
// return Observable.empty();
// return Observable.of(Observable.empty());
// return of(new areaActions.FetchAreaFailAction(Observable.empty()));
})
);
只是尝试一种不同的方法,我也尝试在“效果”中这样做
.catch((err: any) => {
return ErrorObservable.create({type: areaActions.FetchAreaFailAction, payload: err});
然后在操作中
export class FetchAreaFailAction {
readonly type = AREA_FETCH_FAIL;
constructor(public payload?: any) { }
}
然后在减速器中
switch (action.type) {
case areaActionTypes.AREA_FETCH_FAIL:
return Object.assign({}, state, {
area: null,
loading: false,
loaded: false
});
但是这将继续处理所有操作,甚至将这些空数据放入我的商店中,这正是我试图阻止的事情。