Overview
Collects values from the source Observable into an array and emits that array only when another Observable (theclosingNotifier) emits. After emitting the buffer, it immediately starts collecting values into a new buffer.
buffer is useful when you need to batch events based on some external signal, like user interactions or timing events.Type Signature
Parameters
An Observable (or Promise, Array, etc.) that signals when the current buffer should be emitted on the output Observable. Each time it emits, the current buffer is released and a new one begins.
Returns
A function that returns an Observable of arrays. Each array contains all values that were emitted by the source Observable since the last time the
closingNotifier emitted.Usage Examples
Basic Example: Buffer Interval Events on Click
Advanced Example: Buffer Based on Custom Events
Marble Diagram
Common Use Cases
- Event Batching: Collect multiple rapid events and process them together when a trigger occurs
- User Interaction Patterns: Buffer scroll events until the user stops scrolling (using debounced signal)
- Data Aggregation: Accumulate data points until a “flush” signal arrives
- Performance Optimization: Batch UI updates to reduce rendering overhead
Related Operators
- bufferCount - Buffer based on a specific number of values
- bufferTime - Buffer based on time intervals
- bufferToggle - Buffer with separate opening and closing signals
- bufferWhen - Buffer using a factory function for closing notifiers
- window - Like buffer, but emits Observables instead of arrays
Implementation Details
The operator maintains an internal array that accumulates values. When theclosingNotifier emits:
- The current buffer array is emitted to subscribers
- A new empty array is created to collect subsequent values
- The process repeats for each notification
