- RxJS Tutorial
- RxJS - Home
- RxJS - Overview
- RxJS - Environment Setup
- RxJS - Latest Updates
- RxJS - Observables
- RxJS - Operators
- RxJS - Working with Subscription
- RxJS - Working with Subjects
- RxJS - Working with Scheduler
- RxJS - Working with RxJS & Angular
- RxJS - Working with RxJS & ReactJS
- RxJS Useful Resources
- RxJS - Quick Guide
- RxJS - Useful Resources
- RxJS - Discussion
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
Advertisements