I have two streams:
getPost
is afromPromise
stream that streams a single post. sample:author done
getPostsStream
streams a list of posts for a given author. sample:a1 done, a2 done
I have 3 authors, it's also a stream, sample: Bacon.fromArray(["a", "b", "c"])
.
I want to get posts for authors, by combining these streams:
I have two conditions:
- order: one post from each author, then next post for each author.
sample: a1--b1--c1--a2--b2--c2
- concurrency:
getPost
stream can't run in parallel, (that results in many http requests),sample: a1--b1--c1 should happen sequentially, it shouldn't burst.
Here's the code:
var getPost = function(author) {
return Bacon.fromPromise(new RSVP.Promise(function(resolve) {
setTimeout(function() { resolve(author + " done"); }, 1000);
}));
};
var getPostsStream = function(author) {
return Bacon.fromArray([1, 2, 3, 4]).flatMapConcat(function(v) {
return getPost(author + v);
});
};
var combinedFlatMap = Bacon.fromArray(["a", "b", "c"]).flatMap(function(v) {
return getPostsStream(v);
});
var combinedFlatMapConcat = Bacon.fromArray(["a", "b", "c"]).flatMapConcat(function(v) {
return getPostsStream(v);
});
//combinedFlatMap.log();
combinedFlatMapConcat.log();
I do flatMap
on author stream and return posts stream for that author. This results in:
a1 done
b1 done
c1 done
a2 done
b2 done
c2 done
But a1--b1--c1
comes in bursts so there is no concurrency limit of 1.
I do flatMapConcat
:
a1 done
a2 done
a3 done
a4 done
b1 done
b2 done
b3 done
b4 done
This works sequentially as I want, but this time it's out of order.
Edit:
I've played with streams, know how various combinators work. My final work is two streams getPostsStream(author)
and author
stream. I can't combine these, with correct order. Which is a1 b1 a2 b2
, so one post from each getPostsStream(author) and back
. Your example is wrong, try using my streams:
- author stream:
Bacon.fromArray(["a", "b", "c"])
. - postsStream :
getPostsStream(author)
from the above example.
What I need is, using these two streams produce the output:
a1 done `wait 1 sec`
b1 done `wait 1 sec`
c1 done `wait 1 sec`
a2 done `wait 1 sec`
b2 done `wait 1 sec`
c2 done `wait 1 sec`
Note that, letters a b c
represent authors and numbers 1 2 3
represent the posts.
How can I achieve the correct order with a concurrency limit of 1?