I send request - get an array of data. In order to manipulate that data I need to flatten it, so I can use it as a stream of entities not stream of array of entities but, then on side-effect I want those entities appear in the UI at once, not one by one, so it updates UI only ones at a time.
Let's say I have a code like this:
// this generates a sequence of objects - getCasesStream sends an ajax request,
// whenever dateRange changes
casesStm = dateRangeStm.flatMapLatest(getCasesStream)
casesStm.subscribe((x)=> { console.log(x) })
function getCasesStream(dateRange) {
return getCases(dateRange.startDate, dateRange.endDate)
// api every time returns an array,
// but it's difficult to work with array of elements, ergo the flattening
.flatMap((x) => x)
.filter((x) => _.isNotEmpty(x.value))
.map((caseDoc) => _.assign(caseDoc.value, {
key: caseDoc.id
}));
}
This works just fine, emits one value at a time. Now what I want is to emit at most 10 values, if there are less than 10 - emit whatever left.
I thought I could solve that by doing this:
casesStm
.windowWithCount(10)
.flatMap((x)=> x.toArray())
But then this only works if getCasesStream
(after filtering) returns at least 10 items, if it has less than that - I won't even see them.
How can I buffer elements effectively here? again:
- api sends us an array
- to filter and add additional props to each element it's better to flatten that array (or maybe not?)
- at the end I need to buffer (don't want to force browser to redraw each time new element comes)
Maybe I should use generic window
that returns length of elements in getCasesStream
but that function doesn't take any arguments, how can I get the length? I tried using windowWithTimeOrCount
- that keeps emitting empty buffer every interval, even when there's no elements.