我是RX的新手.这是我想要解决的问题的简单模型.它看起来很简单,但我很难找到合适的操作符(或以其他方式操作流)来解决它.
所以我们说我们有两个流.一个人经常发光; 另一个远不那么.我们希望每当第二个observable发出一个值时,取出该点另一个可观察者发出的最新值,然后用它做一些事情.
非工作示例:
let stream1 = Rx.Observable
.interval(100);
let stream2 = Rx.Observable
.interval(2000)
.combineLatest(stream1, (stream2Value, stream1Value) => stream1Value)
.do((stream1Value) => console.log('value:', stream1Value));
stream2.subscribe();
Run Code Online (Sandbox Code Playgroud)
上面代码片段的问题在于它会等到stream2的第一个发出值然后开始以stream1的频率发出事件流.我想要的是获得一个以stream2的速率触发事件的流,但是会在stream2触发时发出stream1发出的最新值.听起来好像我需要stream1成为一个行为主题,以便我可以在stream2触发时访问它的最后一个值...但也许有一个更简单的解决方案?
你可以withLatestFrom这样做:
let stream1 = Rx.Observable
.interval(100);
let stream2 = Rx.Observable
.interval(2000)
.withLatestFrom(stream1, (stream2Value, stream1Value) => stream1Value)
.do((stream1Value) => console.log("value:", stream1Value));
stream2.subscribe();Run Code Online (Sandbox Code Playgroud)
<script src="https://npmcdn.com/@reactivex/rxjs@5.0.3/dist/global/Rx.min.js"></script>Run Code Online (Sandbox Code Playgroud)
此外,根据您的使用情况,您可能会发现auditTime操作员很有用.
| 归档时间: |
|
| 查看次数: |
2808 次 |
| 最近记录: |