RxJS - Transformation Operator bufferWhen



This operator will give the values in the array form, it takes in one argument as a function that will decide when to close, emit and reset the buffer.

Syntax

bufferWhen(closing_func: Observable): Observable

Parameters

closing_func − A function that returns an Observable indicating buffer closure.

Return value

An observable will be returned, that will have an array of buffered values.

Example

Here is a working example of bufferWhen −

import { fromEvent, interval} from 'rxjs';
import { bufferWhen } from 'rxjs/operators';

let btn = document.getElementById("btnclick");
let btn_clicks = fromEvent(btn, 'click');
let buffered_array = btn_clicks.pipe(bufferWhen(() => interval(5000)));
buffered_array.subscribe(arr => console.log(arr));

For bufferWhen we have given a function that executes at an interval of 5 seconds. So, after every 5 seconds, it will output all the clicks recorded and will get reset and start again.

Output

bufferWhen Operator
Advertisements