【发布时间】:2022-01-17 00:44:58
【问题描述】:
我正在尝试使用重试逻辑来在满足条件时进行服务调用。
我有一个处于重试块中的待处理上传对象(应用程序离线)列表。用户可以取消上传,因此我需要逃避重试。这是代码
createPointOfInterest$ = this.actions$.pipe(
ofType(mapActions.MapActionTypes.CreatePointOfInterest),
switchMap((action: mapActions.CreatePointOfInterest) =>
this.mapService.createPointOfInterest(action.pointOfInterest).pipe(
mergeMap((data: any) => {
// Create Point of Interest and associate the MarkerId with it
const pointOfInterest: PointOfInterest = {
id: data.Id,
markerId: data.MarkerId,
sessionId: data.SessionId,
type: data.Type,
status: data.Status,
severity: data.Severity,
approved: data.Approved,
latitude: data.Latitude,
longitude: data.Longitude,
data: data.Data,
correlationId: data.CorrelationId,
userName: data.UserName,
originatedDateTime: data.OriginatedDateTime,
createdDateTime: data.CreatedDateTime,
modifiedDateTime: data.ModifiedDateTime,
chatMessageId: action.pointOfInterest.chatMessageId,
chatMessage: { ...action.pointOfInterest.chatMessage },
uploadStatus: PointOfInterestUploadStatus.Uploaded
};
return [new mapActions.CreatePointOfInterestSuccess(pointOfInterest),
new mapActions.RemovePointOfInterestToPendingUpload(pointOfInterest)];
}),
retryWhen(error$ =>
error$.pipe(
delay(10000),
tap((errorStatus: any) => {
console.log(errorStatus);
console.log('Retrying CreatePointOfInterest...');
})
)
),
catchError((error: HttpErrorResponse) => {
const errorMessage = this.handleError('Error Creating Point of Interest', error, false);
return of(new mapActions.CreatePointOfInterestFailure('CreatePointOfInterestError: ' + errorMessage));
})
))
);
这只是在没有错误时不断重试。我希望可能会设置一个条件,以便仅在满足条件时调用服务
createPointOfInterest$ = this.actions$.pipe(
ofType(mapActions.MapActionTypes.CreatePointOfInterest),
switchMap((action: mapActions.CreatePointOfInterest) => {
if(true){
this.mapService.createPointOfInterest(action.pointOfInterest).pipe(
mergeMap((data: any) => {
// Create Point of Interest and associate the MarkerId with it
const pointOfInterest: PointOfInterest = {
id: data.Id,
markerId: data.MarkerId,
sessionId: data.SessionId,
type: data.Type,
status: data.Status,
severity: data.Severity,
approved: data.Approved,
latitude: data.Latitude,
longitude: data.Longitude,
data: data.Data,
correlationId: data.CorrelationId,
userName: data.UserName,
originatedDateTime: data.OriginatedDateTime,
createdDateTime: data.CreatedDateTime,
modifiedDateTime: data.ModifiedDateTime,
chatMessageId: action.pointOfInterest.chatMessageId,
chatMessage: { ...action.pointOfInterest.chatMessage },
uploadStatus: PointOfInterestUploadStatus.Uploaded
};
return [new mapActions.CreatePointOfInterestSuccess(pointOfInterest),
new mapActions.RemovePointOfInterestToPendingUpload(pointOfInterest)];
}),
retryWhen(error$ =>
error$.pipe(
delay(10000),
tap((errorStatus: any) => {
console.log(errorStatus);
console.log('Retrying CreatePointOfInterest...');
})
)
),
catchError((error: HttpErrorResponse) => {
const errorMessage = this.handleError('Error Creating Point of Interest', error, false);
return of(new mapActions.CreatePointOfInterestFailure('CreatePointOfInterestError: ' + errorMessage));
})
);
}
})
);
但是 switchmap 显示一个错误,指出 Void 不能分配给类型参数
最好的方法是什么?
【问题讨论】: