【问题标题】:Angular NGRX Effect: Retry if condition is metAngular NGRX 效果:如果满足条件,请重试
【发布时间】: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 不能分配给类型参数

最好的方法是什么?

【问题讨论】:

    标签: angular ngrx


    【解决方案1】:

    我发现的最佳方法是使用 TakeWhile 并从中返回 true 或 false。如果返回值为 False,则转义 retryWhen

    retryWhen(error$ => {
                        return error$.pipe(
                            delay(10000),
                            tap((errorStatus: any) => {
                                console.log(errorStatus);
                            }),
                            takeWhile(value => {
                                let index;
                                this.mapStore.select(fromMap.getPendingUploadPointsOfInterest).subscribe(s => index = s.findIndex(a => a.chatMessageId === action.pointOfInterest.chatMessageId));
                                if (index >= 0) {
                                    console.log('Retrying SendPointOfInterestChatMessage...');
                                    return true;
                                } else {
                                    console.log('Canceling SendPointOfInterestChatMessage...');
                                    return false;
                                }
                            })
                        );
                    }),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 2018-02-21
      • 1970-01-01
      • 1970-01-01
      • 2018-08-24
      • 2019-07-19
      相关资源
      最近更新 更多