0

I want to create an event stream which fires events whenever a user has touched and moved his finger by some pixels. Event should contain the relative x and y value by how much the user has moved. The following code does it correctly:

            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}
                })
            }));

But if we modify it by adding some logging:

    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;
                        })
                    }));

We will see that all past touchmove EventStreams still evaluates on each touchmove event despite being dropped by switchE method, which selects only the latest EventStream for the resulting Event. Is these any method to avoid this huge leak of lazyness evaluation?

triggeray
  • 1
  • 2

1 Answers1

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

This is not well documented, but if you give switchE a zeroE it will clean everything up and stop. If you give it other events, it will clean up and output the new events with each evaulation.

Jay Shepherd
  • 395
  • 3
  • 6