2013-10-22 21 views
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。 這些任何方法可以避免這種懶惰評估的巨大漏洞嗎?

回答

0
var downE = clickDownE(guiElement).mapE(function(){ 
return mouseMoveE(guiElement); 
}); 
var upE = clickUpE(guiElement).mapE(function(){ 
return zeroE(); 
}; 
var coordinatesE = mergeE(downE, upE).switchE(); 

這沒有很好的記錄,但是如果你給switchE一個zeroE它將清理所有東西並停止。如果你給它其他事件,它會清理並輸出新的事件與每個evaulation。