仅在一组事件之后发射? [英] Emitting only after set of events?
本文介绍了仅在一组事件之后发射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试执行以下操作 - B 独立于 A 发出,但我想在 A 之后发生 B 时发出(按该顺序).
I'm trying to do something like the following - B is being emitted independently of A, but I want to emit when there is a B that happens after an A (in that order).
----A--------A-->
B--B--B--B----B->
------B-------B->
谢谢!
推荐答案
如果你有一个 hot observable 并且你想从 B
发出值,那么你可能想要将 switchMap
与 take(1)
结合使用:(基本上只有最后 3 行是相关的,上半部分只是模拟一些数据流)
In case you have a hot observable and you want to emit the values from B
, then you might want to use a switchMap
in combination with a take(1)
: (basically just the 3 last lines are relevant, the upper part is just mocking some data-stream)
// Mocking A and B
const streamA$ = Rx.Observable
.interval(2500)
.do(() => console.log("Emitting on A => TAKE NEXT B!!!"))
.share();
const streamB$ = Rx.Observable
.interval(1200)
.do(data => console.log("Emitting on B: " + data))
.publish();
streamB$.connect();
// \End of mocking
streamA$
.switchMap(() => streamB$.take(1))
.subscribe(data => console.info("Took value: " + data));
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>
如果你有冷观察,你可以使用window
结合map
:
If you have cold observables you could use window
in combination with map
:
// Mocking A and B
const streamA$ = Rx.Observable
.interval(4000)
.do(() => console.log("Emitting on A => TAKE NEXT B!!!"));
const streamB$ = Rx.Observable
.interval(900)
.do(data => console.log("Emitting on B: " + data));
// \End of mocking
streamB$.window(streamA$)
.skip(1) // skip the first window, since this will be emitted before A emitted the first time
.mergeMap(win => win.take(1)) // each window should have at most 1 emission
.subscribe(data => console.info("Took value: " + data));
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>
这篇关于仅在一组事件之后发射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文