// RxJS v6+import { interval } from'rxjs';import { bufferToggle } from'rxjs/operators';//emit value every secondconstsourceInterval=interval(1000);//start first buffer after 5s, and every 5s afterconststartInterval=interval(5000);//emit value after 3s, closing corresponding bufferconstclosingInterval= val => {console.log(`Value ${val} emitted, starting buffer! Closing in 3s!`);returninterval(3000);};//every 5s a new buffer will start, collecting emitted values for 3s then emitting buffered valuesconstbufferToggleInterval=sourceInterval.pipe(bufferToggle(startInterval, closingInterval));//log to console//ex. emitted buffers [4,5,6]...[9,10,11]constsubscribe=bufferToggleInterval.subscribe(val =>console.log('Emitted Buffer:', val));
Example 2: Toggle buffer on and off on mouse down/up