0
我想創建一個事件流,當用戶觸摸並移動他的手指時,觸發事件。事件應該包含用戶移動了多少的相對x和y值。下面的代碼確實是正確的:如何處理未使用的flapjax EventStreams
var extractTouchEvent = function (type) {
return extractEventE(document.body, type).mapE(function(e){
return {left:e.targetTouches[0].pageX, top:e.targetTouches[0].pageY};
});
};
var touchStartE = extractTouchEvent('touchstart');
var touchMoveE = extractTouchEvent('touchmove');
var draggedE = switchE(touchStartE.mapE(function(startValue){
return touchMoveE.mapE(function(moveValue){
return {left:moveValue.left-startValue.left, top:moveValue.top-startValue.top}
})
}));
但是,如果我們通過添加一些記錄進行修改:
var draggedE = switchE(touchStartE.mapE(function(startValue){
return touchMoveE.mapE(function(moveValue){
var a = {left:moveValue.left-startValue.left, top:moveValue.top- startValue.top};
console.log(a.left);
return a;
})
}));
我們將看到,所有過去的touchmove EventStreams仍然會評估每個touchmove事件儘管SwitchE的被丟棄方法,它只爲最終的事件選擇最新的EventStream。 這些任何方法可以避免這種懶惰評估的巨大漏洞嗎?