0

It seems I need to do stuff on an ended stream, but not with an .onEnd(). Let's say I've got a list of URLs which I need to fetch per XHR and inject into my application. After that's done, I'd like to do continue doing something that depends on that data, so I really can't do this in parallel. I'm trying something like this right now:

var data = Bacon.fromArray(myArray).flatMap(function(url) {
    ... fetch per XHR and return ...
});

data.onValue(function() { ... do something with data ... });

Bacon.onValues(
    otherValue,
    data,
    function(value, data) {
         // I need to check whether data stream ended
         // if (data.isEnd()) obviously doesn't work
    }
)

Not only does this not work, but it also doesn't feel right. How do I do this properly?

EDIT: I've managed to hack something working together: data.mapEnd('we are done').filter(function(v) { return v === "we are done"; }), but I consider that a very dirty hack. Isn't there something more pure?

Nikolai Prokoschenko
  • 8,465
  • 11
  • 58
  • 97

1 Answers1

1

I'd store the state of the first activity (i.e. what URLs have been fetched) in a Property, and then trigger an Event when the property reaches a 'final' state.

Does the following code solve your problem?

var data = Bacon.fromArray(myArray).flatMap(fetchUrl)
var cumulativeResults = data.scan([], function(acc, result) { return acc.concat([result]) })
// a nicer implementation would ensure that every URL has indeed been fetched
var completed = cumulativeResults.filter(function(results) { return results.length == myArray.length });

completed.onValues(function(results) {
    // proceed
});