0

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/

Georgi Naumov
  • 4,160
  • 4
  • 40
  • 62
  • 1
    I'm trying to understand: do you want your side-effect to be run once per event in processBus, but only if there's been an event in globalBus before that? Like, you want to process whatever task globalBus contains when processBus emits an event... – raimohanska Aug 02 '17 at 07:27
  • Yes. I like just one events from processBus to be processed after there is a message in globalBus and this to happen again when there is a message in globalBus. – Georgi Naumov Aug 02 '17 at 07:45
  • The question is edited. I added example of what I want to achieve but without Bacon.JS – Georgi Naumov Aug 02 '17 at 08:50

1 Answers1

1

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/

raimohanska
  • 3,265
  • 17
  • 28
  • 1
    You could use zip instead of the flatMapConcat construct, but that would result to queuing the "process" clicks too. I understood your intention is to queue tasks only and ignoree "process" clicks when there are no tasks. – raimohanska Aug 02 '17 at 09:28
  • Thank you! Much appreciated! I'm sorry that I cannot vote up several times. I ended with this solution https://gist.github.com/gonaumov/e209b82ae2bccd0a4da4e6bdbac83e4b but can you suggest me why I need toProperty in this case? – Georgi Naumov Aug 02 '17 at 13:09
  • 1
    See https://github.com/baconjs/bacon.js/wiki/FAQ#why-isnt-my-subscriber-called also I'm afraid your implementation will miss all tasks in streamOne when there's nothing going on in streamTwo. I'm assuming you use streamOne for tasks there. I strongly suggest improving your naming conventions too :) – raimohanska Aug 15 '17 at 07:47
  • The real names of the variables are different because this is a production code. Currently the implementation is different but I'm glad that I have learned something. Thank you! – Georgi Naumov Aug 15 '17 at 08:06