如何解决如何重置扫描运算符或流?
message$ = new Subject();
method(type) {
return this.message$.pipe(
filter((element) => element.type === type),scan((acc,val) => [...acc,val],[])
)
}
method.subscribe()
我想通过另一种方法在扫描操作的一部分中重置我的流。
reset() {
// reset logic
}
如果我尝试使用 message$.next() 重置消息,它仍然不会重置 scun,而是使重置值成为流的一部分。
我怎么能做到这一点?
解决方法
一旦你理解了这个概念,就有一个简单的解决方案。您可以将扫描内部更新状态的逻辑移动到 higher order function,并使用 merge 运算符组合多个不同的状态交互。
const { Subject,merge } = rxjs;
const { scan,map } = rxjs.operators;
message$ = new Subject();
reset$ = new Subject();
add = (message) => (state) => [...state,message];
clear = () => (state) => [];
const result$ = merge(
// The function add is applied to message$ and the outer function is called with the message value
message$.pipe(map(add)),// The function clear is applied to reset$ and the outer function is called
reset$.pipe(map(clear))
).pipe(
// The inner function is now fn. In case of add its now: (state) => [...state,message]
// The function is called with the previous state and the new update is now the value of the scan.
scan((state,fn) => fn(state),[])
)
result$.subscribe(console.log)
message$.next("Hello")
message$.next("Goodbye")
reset$.next()
message$.next("Hello again!")
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.3/rxjs.umd.min.js"></script>
您可以通过 scan
将此解决方案用于每个状态操作。您编写的用于操作 scan-state
的函数不受限制。另一个例子可能是一个只删除最后一个值的函数:
// Subject to trigger
const deleteLastValue = new Subject();
// Function that manipulates the state
const deleteLast = () => (state) => state.slice(0,-1)
// Apply the function to the observable/subject.
deleteLastValue$.pipe(map(deleteLast))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。