如何基于另一个 Observable 重置 RXJS 扫描操作符
How to reset a RXJS scan operator based on another Observable
我有一个组件在呈现虚拟列表中的最后一项时触发 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),
// 5. Map the response.
map(({ data }) =>
data.map((user) => ({
label: user.name,
value: user.id
}))
),
// 6. Accumulate the new options with the previous options.
scan((acc, curr) => {
// TODO: Dont merge on search$.next
return [...acc, ...curr]);
}
);
// Fetch next page
onScrollEnd: (offset: number) => offset$.next(offset);
// Fetch search results
onSearch: (term) => {
search$.next(term);
};
这是一个有趣的流。考虑一下,offset$ 和 search$ 实际上是 2 个独立的流,虽然具有不同的逻辑,因此应该在最后而不是开头合并。
此外,在我看来,搜索应该将偏移量重置为 0,但我在当前逻辑中看不到这一点。
所以这是我的想法:
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);
看看这是否有效或有意义。我可能缺少一些上下文。
我认为您可以通过重组您的链来实现您想要的(为简单起见,我省略了 tap
触发加载的调用):
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], []),
),
),
);
来自 search$
的每次发射都会创建一个新的内部 Observable,它有自己的 scan
,它将以一个空的累加器开始。
找到了可行的解决方案:我在 scan
运算符之前使用 withLatestFrom
检查当前偏移量,并在需要时根据此值重置累加器。
要操作 scan
的 state
,您可以编写 higher order functions that get the old state and the new update. Combine then with the merge 运算符。通过这种方式,您可以坚持使用干净的面向流的解决方案,而不会产生任何副作用。
const { Subject, merge } = rxjs;
const { scan, map } = rxjs.operators;
add$ = new Subject();
clear$ = new Subject();
add = (value) => (state) => [...state, value];
clear = () => (state) => [];
const result$ = merge(
add$.pipe(map(add)),
clear$.pipe(map(clear))
).pipe(
scan((state, innerFn) => innerFn(state), [])
)
result$.subscribe(result => console.log(...result))
add$.next(1)
add$.next(2)
clear$.next()
add$.next(3)
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.3/rxjs.umd.min.js"></script>
此方法可以轻松扩展 and/or 适用于 rxjs 中的其他 state
用例。
示例(删除最后一项)
removeLast$ = new Subject()
removeLast = () => (state) => state.slice(0, -1);
merge(
..
removeLast$.pipe(map(removeLast)),
..
)
我有一个组件在呈现虚拟列表中的最后一项时触发 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),
// 5. Map the response.
map(({ data }) =>
data.map((user) => ({
label: user.name,
value: user.id
}))
),
// 6. Accumulate the new options with the previous options.
scan((acc, curr) => {
// TODO: Dont merge on search$.next
return [...acc, ...curr]);
}
);
// Fetch next page
onScrollEnd: (offset: number) => offset$.next(offset);
// Fetch search results
onSearch: (term) => {
search$.next(term);
};
这是一个有趣的流。考虑一下,offset$ 和 search$ 实际上是 2 个独立的流,虽然具有不同的逻辑,因此应该在最后而不是开头合并。
此外,在我看来,搜索应该将偏移量重置为 0,但我在当前逻辑中看不到这一点。
所以这是我的想法:
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);
看看这是否有效或有意义。我可能缺少一些上下文。
我认为您可以通过重组您的链来实现您想要的(为简单起见,我省略了 tap
触发加载的调用):
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], []),
),
),
);
来自 search$
的每次发射都会创建一个新的内部 Observable,它有自己的 scan
,它将以一个空的累加器开始。
找到了可行的解决方案:我在 scan
运算符之前使用 withLatestFrom
检查当前偏移量,并在需要时根据此值重置累加器。
要操作 scan
的 state
,您可以编写 higher order functions that get the old state and the new update. Combine then with the merge 运算符。通过这种方式,您可以坚持使用干净的面向流的解决方案,而不会产生任何副作用。
const { Subject, merge } = rxjs;
const { scan, map } = rxjs.operators;
add$ = new Subject();
clear$ = new Subject();
add = (value) => (state) => [...state, value];
clear = () => (state) => [];
const result$ = merge(
add$.pipe(map(add)),
clear$.pipe(map(clear))
).pipe(
scan((state, innerFn) => innerFn(state), [])
)
result$.subscribe(result => console.log(...result))
add$.next(1)
add$.next(2)
clear$.next()
add$.next(3)
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.3/rxjs.umd.min.js"></script>
此方法可以轻松扩展 and/or 适用于 rxjs 中的其他 state
用例。
示例(删除最后一项)
removeLast$ = new Subject()
removeLast = () => (state) => state.slice(0, -1);
merge(
..
removeLast$.pipe(map(removeLast)),
..
)