3

I'm attempting to create a stack of AJAX responses in BaconJS. That processes them in a first in first out fashion, but each 'out' event should wait for user input.

This is where I'm at now: Live JSBin

var pages = Bacon.fromArray([1,2,3])
var next = $("#next").asEventStream('click').map(true);

pages.flatMapConcat(asyncFunction).zip(next).log("responses")

function asyncFunction(page) {
  // Simulating something like an AJAX request
  return Bacon.later(1000 + (Math.random() * 3000), "Page "+ page)
}

Currently this synchronously outputs an event from the pages EventStream each time that #next is clicked, which the behavior I want.


However, I am unable to figure out how to push more values to the pages EventStream. I have attempted to replace the pages EventStream with a Bus, and pushing values like this (which doesn't work).

var pages = new Bacon.Bus()
pages.push("value")

How do I push more values to an EventStream?

  • Why not just use the parameter object pattern? – Darth Egregious Oct 28 '15 at 16:45
  • Can you give an example of how that will help solve my problem? –  Oct 28 '15 at 16:47
  • Where are the values that you want to push coming from? – Bergi Oct 28 '15 at 17:54
  • 1
    Well you can't "push" to an EventStrean unless it's a bus. You can, however, gather events from different sources into the same stream using `merge` for example. But as Bergi asked, where do your "more values" come from. Can you add them to your example? – raimohanska Oct 29 '15 at 07:38

1 Answers1

0

I know this is an OLD post, but bus would work. Just push the number (you had an array of numbers before) into it:

var pages = new Bacon.Bus();
// Bacon.fromArray([1,2,3])
var next = $("#next").asEventStream('click').map(true);

pages.flatMapConcat(asyncFunction).zip(next).log("responses")

function asyncFunction(page) {
  // Simulating something like an AJAX request
  return Bacon.later(1000 + (Math.random() * 3000), "Page "+ page)
}


pages.push(1);
pages.push(2);
pages.push(3);

I cloned your jsbin and changed it to use bus

As mentioned previously, you could stream the source of the page values using something like fromEvent or from fromBinder

Bless Yahu
  • 1,331
  • 1
  • 12
  • 25