I have a stream where the events look something like this:
{
endOfSequence : false,
sequenceId: 12345,
data: [.....]
}
I need to terminate the sequence when endOfSequence === true
. I started with takeWhile
:
seq = stream.takeWhile( function(event){
return !event.endOfSeq;
});
but the problem is that I miss the last event.
I can obviously write code that accomplishes the same thing, for example:
function beforeEnd(event){
return !event.endOfSeq;
}
seq = stream.takeWhile(beforeEnd)
.merge(stream.skipWhile(beforeEnd).take(1));
But this is a bit ugly. Is there a better way?
You can write a custom handler with Bacon.withHandler that emits a
Bacon.End()
when you get theendOfSequence
.A working example can be found in this jsFiddle.