bufferCount

signature: bufferCount(bufferSize: number, startBufferEvery: number = null): Observable

Collect emitted values until provided number is fulfilled, emit as array.

Ultimate RxJS

Examples

Example 1: Collect buffer and emit after specified number of values

( StackBlitz | jsBin | jsFiddle )

// RxJS v6+
import { interval } from 'rxjs';
import { bufferCount } from 'rxjs/operators';

//Create an observable that emits a value every second
const source = interval(1000);
//After three values are emitted, pass on as an array of buffered values
const bufferThree = source.pipe(bufferCount(3));
//Print values to console
//ex. output [0,1,2]...[3,4,5]
const subscribe = bufferThree.subscribe(val =>
  console.log('Buffered Values:', val)
);

Example 2: Overlapping buffers

( StackBlitz | jsBin | jsFiddle )

Example 3: Last n keyboard presses tracking

( StackBlitz )

Additional Resources


📁 Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/bufferCount.ts

Last updated