I'm trying to understand bacon.js and FRP so tried to make a simple drag and drop example, but I'm having trouble with the lazy evaluation of one piece of code. When I add a .log()
into the stream it seems to look and act fine, but if I take it out, it doesn't update. Here is what I'm doing:
// UI streams
block_mousedown = block_el.asEventStream('mousedown').map(xyFromEvent);
global_mousemove = html.asEventStream('mousemove').map(xyFromEvent);
global_mouseup = html.asEventStream('mouseup');
// Composites
isDragging = block_mousedown.merge(global_mouseup.map(0));
mouseDragging = Bacon.combineAsArray(isDragging, global_mousemove)
.filter(function(v){ return notZero(v[0]) })
mouseDeltaFromClick = mouseDragging
.map(getDelta)
// Block offset when it was clicked on
block_pos_at_mousedown = block_mousedown
.map( function(a,b){ return block_el.offset();})
.map(function(e){ return [e.left, e.top]; })
// If I remove this log(), it doesn't evaluate
.log();
// merge mouse delta with block position when clicked
mouseDeltaAndBlockPos = mouseDeltaFromClick
.combine(block_pos_at_mousedown, '.concat')
.onValue( function(e){
block_el.css({
top : e[3]+e[1]+"px",
left : e[2]+e[0]+"px"
});
});
And here is a jsFiddle of it
I'm thinking I might be going about this all wrong, is this even the right approach? I want to pass through the position of the block when it was clicked which should update on mousedown
but not be updated along with the mousemove
.