Learn RxJS
Search…
bufferWhen

signature: bufferWhen(closingSelector: function): Observable

Collect all values until closing selector emits, emit buffered values.

Examples

Example 1: Emit buffer based on interval
1
// RxJS v6+
2
import { interval } from 'rxjs';
3
import { bufferWhen } from 'rxjs/operators';
4
5
//emit value every 1 second
6
const oneSecondInterval = interval(1000);
7
//return an observable that emits value every 5 seconds
8
const fiveSecondInterval = () => interval(5000);
9
//every five seconds, emit buffered values
10
const bufferWhenExample = oneSecondInterval.pipe(
11
bufferWhen(fiveSecondInterval)
12
);
13
//log values
14
//ex. output: [0,1,2,3]...[4,5,6,7,8]
15
const subscribe = bufferWhenExample.subscribe(val =>
16
console.log('Emitted Buffer: ', val)
17
);
Copied!

Additional Resources