代码之家  ›  专栏  ›  技术社区  ›  Aymen Kanzari

ngrx效应-结合两种API

  •  0
  • Aymen Kanzari  · 技术社区  · 5 年前

    使用以下命令更新对象的一部分具有以下效果 update api,然后我通过以下方式获取整个对象 findById api,所以我用了 forkJoin 将这两个api结合起来,但我想要 findById api将在1秒后执行 更新 api,所以我用了 delay(1000) ,但它不起作用

    @Effect()
    updateGeographicScope$ = this.actions$.pipe(
        ofType<conventionsActions.PatchGeographicScope>(conventionsActions.ConventionActionTypes.PATCH_GEOGRAPHIC_SCOPE),
        map(action => action.payload),
        exhaustMap(geographicScope => forkJoin(this.apiConvention.update(geographicScope),
            this.apiConvention.findById (geographicScope.externalId).pipe(delay(1000))).pipe(
                map(([first, convention]) => new conventionsActions.PatchSuccess({
                    id: convention.externalId,
                    changes: convention
                })),
                catchError(err => {
                    console.error(err.message);
                    return of(new conventionsActions.Failure({ concern: 'PATCH', error: err }));
                })
            ))
    );
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   satanTime    5 年前

    你需要使用 concat timer 为了这个。与 海螺 它是在开始下一个流之前完成的第一个流。因此,它会进行更新,然后等待1秒,然后生成findById。

    @Effect()
    updateGeographicScope$ = this.actions$.pipe(
        ofType<conventionsActions.PatchGeographicScope>(conventionsActions.ConventionActionTypes.PATCH_GEOGRAPHIC_SCOPE),
        map(action => action.payload),
        mergeMap(geographicScope => concat(
          this.apiConvention.update(geographicScope).pipe(switchMapTo(EMPTY)), // makes a request
          timer(1000).pipe(switchMapTo(EMPTY)), // waits 1 sec
          this.apiConvention.findById(geographicScope.externalId), // makes a request
        )),
        map(convention => new conventionsActions.PatchSuccess({
          id: convention.externalId,
          changes: convention
        })),
        catchError(err => {
          console.error(err.message);
          return of(new conventionsActions.Failure({ concern: 'PATCH', error: err }));
        }),
        repeat(), // make active after a failure
      )),
    );