2 回答
TA贡献1943条经验 获得超7个赞
我认为你也可以调度一个getRawDataSuccess动作(执行 1 个 api 调用)
getRawData$ = createEffect(() =>
this.actions$.pipe(
ofType(ESchedulesActions.getRawData),
mergeMap(() =>
this.apiCallsService.getRawData().pipe(
map(data => ({ type: ESchedulesActions.GetRawDataSuccess, payload: data })),
catchError(err => ({ type: ESchedulesActions.GetRawDataError, payload: err }))
)
)
)
);
然后为每个治疗创造一个效果,倾听getRawDataSuccess行动:
getSchedulesByDate$ = createEffect(() =>
this.actions$.pipe(
ofType(ESchedulesActions.getRawDataSuccess),
map((action) => {
return {
type: ESchedulesActions.GetSchedulesByDateSuccess,
payload: action.payload.schedulesByDate,
}
})
)
);
getDirectionsByDate$ = createEffect(() =>
this.actions$.pipe(
ofType(ESchedulesActions.getRawDataSuccess),
map((action) => {
return {
type: ESchedulesActions.GetDirectionsByDateSuccess,
payload: action.payload.directionsByDate,
}
})
)
);
这将是更清洁的 IMO,理论上也将并行运行。
TA贡献1744条经验 获得超4个赞
仅使用一种效果从 API 获取原始数据并将其放入您的商店,然后创建两个不同的选择器来应用您的 groupByDirections 和 groupByDate 逻辑。
或者提取 groupByDirections 和 groupByDate 逻辑来实现。在你的效果中制作一个管道,应用两种逻辑并以相同的效果分派两个动作
更新:
如果你想执行两个动作试试这个:
loadSchedulings$ = createEffect(() =>
this.actions$.pipe(
ofType(ESchedulesActions.getRawData),
mergeMap(action => this.apiCallsService.getRawData()),
map(rawApiData => {
const groupByDate = {}; // do your logic with rawApiData
const groupByDirections = {}; // do your logic with rawApiData
return { groupByDate, groupByDirections };
}),
mergeMap(groupedData => [
{
type: ESchedulesActions.GetDirectionsByDateSuccess,
payload: groupedData.groupByDirections,
},
{
type: ESchedulesActions.GetSchedulesByDateSuccess,
payload: groupedData.groupByDate,
},
]),
),
);
添加回答
举报