我有一个组件,它onScrollEnd在呈现虚拟列表中的最后一个项目时触发一个事件。此事件将执行新的 API 请求以获取下一页并使用scan运算符将它们与先前的结果合并。
该组件还有一个触发onSearch事件的搜索字段。
如何在scan触发搜索事件时清除操作员之前的累积结果?或者我需要在这里重构我的逻辑吗?
const loading$ = new BehaviorSubject(false);
const offset$ = new BehaviorSubject(0);
const search$ = new BehaviorSubject(null);
const options$: Observable<any[]> = merge(offset$, search$).pipe(
// 1. Start the loading indicator.
tap(() => loading$.next(true)),
// 2. Fetch new items based on the offset.
switchMap(([offset, searchterm]) => userService.getUsers(offset, searchterm)),
// 3. Stop the loading indicator.
tap(() => loading$.next(false)),
// 4. Complete the Observable when there is no 'next' link.
takeWhile((response) => response.links.next), …Run Code Online (Sandbox Code Playgroud)