我正在使用 ngrx 并将 @Effect 连接到 LOAD_CONTRACT 操作,然后进行 3 次 HTTP 调用以获取数据。私有变量用于存储来自每个 GET 的数据,以便最后可以使用包含 3 个检索到的对象的有效负载调用 LoadContractSuccessAction。
我下面的代码运行良好,错误处理也运行良好......但我觉得可能有一种更简洁或更好的方式来构建事物。
我不知道是否所有的嵌套都是必要的,或者事情是否可以以某种方式变平。我也不确定使用 switchMap 是否是最好的运算符。
对ngrx最佳实践有更好了解的人可以评论如何改进/简化以下内容......?
private clientContractIds: IClientContractIds;
private contract: Contract;
private contractSummaryMonths: ContractSummaryMonth[];
private contractSummaryTotals: ContractSummaryTotals;
// Loads a contract and its summary months and totals.
@Effect()
loadContract$ = this.actions$.pipe(
ofType(ContractActionTypes.LOAD_CONTRACT),
map((action: IActionWithPayload<IClientContractIds>) => {
this.clientContractIds = {
client_id: action.payload.client_id,
contract_id: action.payload.contract_id
};
}),
// Get the contract.
switchMap(() => {
return this.contractService.getContract$(this.clientContractIds).pipe(
map(contract => (this.contract = contract)),
catchError(error => throwError(error)),
// Get the summary months.
switchMap(() => {
return this.contractService
.getContractSummaryMonths$(this.clientContractIds)
.pipe(
map(
contractSummaryMonths =>
(this.contractSummaryMonths = contractSummaryMonths)
),
catchError(error => throwError(error))
);
}),
// Get the summary totals.
switchMap(() => {
return this.contractService
.getContractSummaryTotals$(this.clientContractIds)
.pipe(
map(
contractSummaryTotals =>
(this.contractSummaryTotals = contractSummaryTotals)
),
catchError(error => throwError(error))
);
}),
// Call the success action with the payload objects.
switchMap(() => {
return of(
new LoadContractSuccessAction({
contract: this.contract,
contractSummaryMonths: this.contractSummaryMonths,
contractSummryTotals: this.contractSummaryTotals
})
);
})
);
}),
catchError(error => {
return of(new LoadContractFailAction(error));
})
);