How to enable process only once after receiving something from another Bus

81 views Asked by At

I need idea how to achieve something with BaconJS. I need to have something like this solution (it is just an example) but I want to call processBus only once after button is clicked. So I need to call

console.log((new Date()).getTime());

Only when the button is clicked and there is event into globalBus. Here is my code:

var globalBus = new Bacon.Bus();
var processBus = new Bacon.Bus(); 
function startProcess() {
    globalBus.push(new Bacon.Next());
}

processBus.skipUntil(globalBus).onValue(function() {
    console.log((new Date()).getTime());
});

setInterval(function() {
    processBus.push(new Bacon.Next());
}, 1000);

and here is the example jsfiddle:

http://jsfiddle.net/Tjdp5/13/

I mean only once but every time after button is clicked.

This is example of what I want to achieve but without BaconJS http://jsfiddle.net/Tjdp5/14/

1

There are 1 answers

4
raimohanska On BEST ANSWER

If I understood your intention correctly, you're looking for a "task queue" mechanism. Here's my suggestion.

var taskE = Bacon.fromEvent(button1, "click")
   // use flatMap to force strict evaluation
  .flatMap(function() { return new Date() })
  .doLog('task created:')

var processE = Bacon.fromEvent(button2, "click")

taskE
  // for each new task, take one "process next task" click
  // use flatMapConcat to queue tasks
  .flatMapConcat(function(task) { return processE.take(1).map(task) })
  .onValue(function(task) {
    console.log('task ', task, 'processed at', new Date());
  });

Working example here: http://jsfiddle.net/52mb7b36/