【发布时间】:2020-01-25 04:07:24
【问题描述】:
我正在使用 ngrx 并且有一个场景,我需要同时调度 2 个动作。我的状态具有更新和更新的属性,如下所示。
//from reducer
const defaultCardState: CardState = {
ids: [],
entities: {},
loaded: false,
loading: false,
adding: false,
added: false,
updating: false,
updated: false,
deleting: false,
deleted: false
};
这些是我从组件中调度的操作
this.store.dispatch(fromCard.updateCard({id: id1, changes: {name: name1}}))
this.store.dispatch(fromCard.updateCard({id: id2, changes: {name: name2}}))
下面是我的动作、reducer和效果
//Update Card Actions
export const updateCard = createAction('[Cards] Update Card', props<{id: string, changes: any}>())
export const updateCardSuccess = createAction('[Cards] Update Card Success', props<{changes: any}>());
export const updateCardFail = createAction('[Cards] Update Card Fail')
//Reducer
on(fromCards.updateCard, (state) => ({...state, updating: true, updated: false})),
on(fromCards.updateCardSuccess, (state, action: any) => ({...cardAdapter.updateOne(action.changes, state), updated: true, updating: false})),
on(fromCards.updateCardFail, (state, action: any) => fromCards.updateCardFail),
//Update Card Effect
updateCard$: Observable<Action> = createEffect(() => this.actions$.pipe(
ofType(fromCardActions.updateCard),
map((action: any) => { return {id: action.id, changes: action.changes}}),
switchMap((action: any) => this.cardService.updateCard(action).pipe(
map((res) => (fromCardActions.updateCardSuccess({changes: action }))),
catchError(() => of(fromCardActions.updateCardFail))
))
))
依次调度这些操作以使更新和更新字段不冲突的最佳方式是什么?如果我只运行其中一个,它可以工作,但如果我像上图所示将它们一起发送,则只有一个完成。我看到两个动作都被分派了,但只有一个成功动作被分派。
【问题讨论】:
标签: angular typescript redux rxjs ngrx