rxjsrxjs-observables

Rxjs combining bufferCount and bufferTime


Using Rxjs, I'm looking for a way to create an observable that

After a certain time interval, it emits what has been accumulated till then.

I looked into bufferCount and bufferTime. However, I am unable to understand how to combine both behaviors such that it guarantees at least one to be fulfilled. so that all events are processed.


Solution

  • The bufferTime operator allows you to specify an optional max buffer size. So you can do something like this:

    bufferTime(2000, null, 5) // <-- buffer emits @ 2000ms OR when 5 items collected