How to reset a RXJS scan operator based on another Observable

后端 未结 3 883
无人共我
无人共我 2021-01-18 03:49

I have a component which triggers an onScrollEnd event when the last item in a virtual list is rendered. This event will do a new API request to fetch the next

3条回答
  •  庸人自扰
    2021-01-18 04:06

    This is an interesting stream. Thinking about it, offset$ and search$ are really 2 separate streams, though, with different logic, and so should be merged at the very end and not the beginning.

    Also, it seems to me that searching should reset the offset to 0, and I don't see that in the current logic.

    So here's my idea:

    const offsettedOptions$ = offset$.pipe(
        tap(() => loading$.next(true)),    
        withLatestFrom(search$),
        concatMap(([offset, searchterm]) => userService.getUsers(offset, searchterm)),
        tap(() => loading$.next(false)),
        map(({ data }) =>
        data.map((user) => ({
          label: user.name,
          value: user.id
        })),
        scan((acc, curr) => [...acc, ...curr])
    );
    
    const searchedOptions$ = search$.pipe(
        tap(() => loading$.next(true)),
        concatMap(searchTerm => userService.getUsers(0, searchterm)),
        tap(() => loading$.next(false)),
        map(({ data }) =>
        data.map((user) => ({
          label: user.name,
          value: user.id
        })),
    );
    
    const options$ = merge(offsettedOptions, searchedOptions);
    

    See if that works or would make sense. I may be missing some context.

提交回复
热议问题