Bacon.js restart buffering when another EventStream fires

I am trying to create a simple drawing application using Bacon.js. It should let you draw a line by selecting start and end points with mouse clicks like this.

points = paper.asEventStream('click')
  .map((e) -> x: e.pageX, y: e.pageY)
  .bufferWithCount(2)

      

So far so good. It should now allow deselecting the start point by pressing Esc.

cancel = doc.asEventStream('keypress')
  .map((e) -> e.keyCode)
  .filter((key) -> key is 27)
  .map(true)

      

What is the Bakon way to restart buffering?
UPD: The implementation that I still have looks awful.

points
  .merge(cancel)
  .buffer(undefined, (buffer) ->
    values = buffer.values
    if values.some((val) -> val is true) then values.length = 0
    else if values.length is 2 then buffer.flush())  

      

+3


source to share


1 answer


You can start canceling buffering:

var filteredPairs = cancel.startWith(true).flatMapLatest(function() {
  return points.bufferWithCount(2)
})

      



See jsFiddle

+1


source







All Articles