0

如果没有点击同一个用户,我有一个效果会做一些事情,以避免不必要的请求,对吗?

这段代码可以工作,但是当它陷入已经点击的同一个用户再次点击的情况下,它不会向 Store 发出任何错误,因此不会产生任何下一个效果。

如果我的 takeWhile 对我的逻辑不满意,我该如何返回错误?

updatePartial$: Observable<Action> = createEffect(() =>
    this.action$.pipe(
        ofType(fromPeopleActions.UPDATE_MANY_PARTIAL),
        withLatestFrom(this.selectorsService.userFriendClicked),
        takeWhile(([action, userFriendClicked]) => action.updates.id !== userFriendClicked.id),
        switchMap(([action, userFriendClicked]) => {
            console.log('oi');
            const peoples: Update<IPeople>[] = [];
            const https: Observable<Update<IPeople>>[] = [];

            peoples.push(action.updates);
            peoples.push({
                ...userFriendClicked,
                changes: {
                    isClicked: false,
                },
            });

            peoples.forEach((people) => https.push(this.backendService.updatePartial(people)));

            return forkJoin(https).pipe(
                map(() => fromPeopleActions.UPDATE_MANY_PARTIAL_SUCCESS({ updates: peoples })),
                catchError((error) => of(fromPeopleActions.UPDATE_MANY_PARTIAL_FAIL({ error: this.requestHandlerService.getError(error) })))
            );
        })
    )
);
4

1 回答 1

0

从 RxJS 6.4.0 开始,takeWhile采用一个可选参数,该参数inclusive将发出第一个未通过谓词的项目。

但是,在您的情况下,您需要抛出一个错误,而不是最后一个失败的项目,以便它被catchError操作员捕获。一种解决方法是在相同条件下使用concatMapbeforetakeWhile并在失败时抛出错误。尝试以下

updatePartial$: Observable<Action> = createEffect(() =>
  this.action$.pipe(
    ofType(fromPeopleActions.UPDATE_MANY_PARTIAL),
    withLatestFrom(this.selectorsService.userFriendClicked),
    concatMap(([action, userFriendClicked]) => {
      if (action.updates.id !== userFriendClicked.id) {
        return of([action, userFriendClicked]);
      }
      return throwError('Same user clicked');
    }),
    takeWhile(([action, userFriendClicked]) => action.updates.id !== userFriendClicked.id),
    .
    .
于 2020-05-02T07:58:14.627 回答