Buffers values from the source Observable based on opening and closing signals. When the openings Observable emits, a new buffer starts collecting values. For each opening, a closing Observable is created using closingSelector, and when it emits, that buffer is emitted and closed.
bufferToggle allows you to create multiple concurrent buffers with independent lifecycles, making it powerful for complex buffering scenarios.
A function that receives the value emitted by openings and returns an Observable (or other ObservableInput). When this returned Observable emits its first value, the associated buffer is emitted and closed.
A function that returns an Observable of arrays. Each array contains the values collected between an opening signal and its corresponding closing signal.
import { interval, bufferToggle, timer, map } from 'rxjs';const source = interval(100).pipe( map(i => ({ value: i, timestamp: Date.now() })));// Start a buffer every 500ms, each lasts for 1 secondconst openings = interval(500);const buffered = source.pipe( bufferToggle(openings, () => timer(1000)));buffered.subscribe(items => { console.log('Buffer emitted with', items.length, 'items'); console.log('Values:', items.map(i => i.value));});// At ~1.0s: buffer started at 0s closes (10 items)// At ~1.5s: buffer started at 0.5s closes (10 items)// At ~2.0s: buffer started at 1.0s closes (10 items)// Multiple buffers can be active simultaneously!