ngrx效应--结合两个API

问题描述 投票:0回答:1

我有下面的效果,使用以下方法更新对象的一部分 update api,然后我就可以通过 findById api,所以我用了 forkJoin 将这两个api结合起来,但我想让这个 findById 的api在1秒后被执行。update 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 }));
            })
        ))
);
typescript rxjs ngrx ngrx-store ngrx-effects
1个回答
0
投票

你需要使用一个 concattimer 为,。随着 concat 在开始下一个流之前,它先完成第一个流。所以它先进行update,然后等待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
  )),
);
© www.soinside.com 2019 - 2024. All rights reserved.