[英]RXjs canceling debounce from source
I'm learning Rx so i dont get quiet well some of the mechanics on Rx, what i want to do i can do it easly with callbacks, but i want to understand Rx. 我正在学习Rx,所以我对Rx的一些技巧不太了解,我想做的事我可以通过回调轻松完成,但是我想了解Rx。
So what i want to do is, debounce a mouse over event 500ms and show a ui interface, and on mouse out hide that interface, so i have this code: 所以我想做的是,将鼠标移过事件500ms并显示一个ui界面,并在鼠标移出时隐藏该界面,所以我有以下代码:
var outStream = Rx.Observable.fromEventPattern(
function add (h) {
asset.events.onInputOut.add(function(){
h('out');
});
}
);
var overStream = Rx.Observable.fromEventPattern(
function add (h) {
asset.events.onInputOver.add(function(e){
h('over');
});
}
).debounce(500);
var source = Rx.Observable.merge(overStream, outStream);
source.subscribe(function (x) {
console.log(x);
});
What i want is something like this: 我想要的是这样的:
.
.mouse over fired after 500ms
.mouseout
.
.mouse over
.mouse out fired before 500ms trigger cancel mouse over
I need some directions, thanks in advance. 我需要一些指示,谢谢。
I'd use something like this: 我会用这样的东西:
var hoverStream = overStream.flatMapLatest(function() {
return Rx.Observable.timer(500).takeUntil(outStream).map(function() { return 'hover'; })
});
This says "map each overStream element into an observable that will yield a single element after 500ms, but stop listening if outStream yields an element before it arrives". 这表示“将每个overStream元素映射到一个可观察对象,它将在500ms之后产生一个元素,但是如果outStream在它到达之前产生一个元素,则停止侦听”。 For this to work properly you'll need to remove the debounce
from your overStream (and just keep it as a simple stream of all such events). 为了使此功能正常运行,您需要从您的OverStream中删除debounce
(并将其作为所有此类事件的简单流保存)。
You could then merge the values into whatever result stream you wanted. 然后,您可以将值合并到所需的任何结果流中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.