How to group items into groups of size n in RxJs 5?

298 views Asked by At

I need help to group items on infinite stream without data loss.

Groups' size must be not bigger than n elements. If during some period of time no items came, then current group's size maybe less than n.

Trying this code:

Observable.create(observer => {
    setTimeout(() => observer.next([1]),            90)
    setTimeout(() => observer.next([2, 3]),        120)
    setTimeout(() => observer.next([4, 5, 6]),     160)
    setTimeout(() => observer.next([7, 8, 9, 10]), 310)
    setTimeout(() => observer.next([11]),          360)
})
.flatMap(xs => xs)
.bufferTime(100, 100, 3) // group every 100 ms or by 3, whatever comes first
.takeUntil(Observable.timer(590))
.subscribe(x => console.log(x))

it gives following result:

[ 1 ]
[ 2, 3, 4 ]
[]
[ 7, 8, 9 ]
[ 11 ]
[]

But I need:

[ 1 ]
[ 2, 3, 4 ]
[ 5, 6 ]
[]
[ 7, 8, 9 ]
[ 10, 11 ]
[]

Any help appreciated

1

There are 1 answers

0
Ali On

Thanks to this answer I answer myself. To achieve my goal I need to call like this

.bufferTime(100, null, 3)