How to reset a RXJS scan operator based on another Observable

后端 未结 3 856
无人共我
无人共我 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:17

    I think you could achieve what you want just by restructuring your chain (I'm omitting tap calls that trigger loading for simplicity):

    search$.pipe(
      switchMap(searchterm =>
        concat(
          userService.getUsers(0, searchterm),
          offset$.pipe(concatMap(offset => userService.getUsers(offset, searchterm)))),
        ).pipe(
          map(({ data }) => data.map((user) => ({
            label: user.name,
            value: user.id
          }))),
          scan((acc, curr) => [...acc, ...curr], []),
        ),
      ),
    );
    

    Every emission from search$ will create a new inner Observable with its own scan that will start with an empty accumulator.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题