24 Jul 2024
1 min

bufferWhen – RxJS Reference

bufferWhen

bufferWhen collects values emitted from the source observable into cache without passing them down to an observer until the notifier observable emits (buffering). The buffer then sends the cached values as a group, resets and starts buffering again until the provided observable emits once more.

This operator is very similar to buffer, but once the buffer is flushed, it unsubscribes from the notifier observable and will re-subscribe once a new value arrives.

One interesting behavior of the bufferWhen operator is that it can send empty set of values to the observer if the notifier observable emits a value and the cache is empty.

The operator works in the following way:

  1. Subscribe to a source observable
  2. When a new value arrives from a source observable, put it in a cache
  3. If there’s no active subscription to a notifier observable, subscribe to it
  4. When a notifier observable emits a value, send all values from the cache to the observer, even if the cache is empty
  5. Unsubscribe from the notifier observable
  6. Once the source completes, send the complete notification to the observer
  7. If any of the source observables throws an error, send the error notification to the observer.

Usage

bufferWhen is often used when the batching technique is required. For example, sometimes you need to repeatedly perform a very expensive operation within a very small time frame, such as re-rendering a DOM tree on updates from a stream, batching can help collect updates and render them at once.

Here’s an example of using bufferWhen to emit array of most recent interval events on every click:

const clicks = fromEvent(document, 'click');
const intervalEvents = interval(1000);

const buffered = intervalEvents.pipe(bufferWhen(() => clicks));

buffered.subscribe(x => console.log(x));

Playground

Additional resources

See also

Frequently asked questions

How do I batch values with bufferWhen in RxJS?

Use bufferWhen by passing a factory function that returns a notifier observable, such as bufferWhen(() => clicks). The operator caches source values without passing them down and emits them as an array each time the notifier emits. For example, interval(1000).pipe(bufferWhen(() => fromEvent(document, 'click'))) emits the interval values collected since the previous click as one array on each click.

What is the difference between bufferWhen and buffer in RxJS?

The main difference is in how the notifier is handled: bufferWhen is very similar to buffer, but after each flush it unsubscribes from the notifier observable. It then re-subscribes only once a new source value arrives. Keep in mind that bufferWhen takes a function returning the notifier, as in bufferWhen(() => clicks).

Why does bufferWhen emit an empty array in RxJS?

bufferWhen emits an empty array when its notifier emits while the cache holds no source values. The operator sends whatever is cached on each notifier emission, even if nothing is there. If you need to skip these, filter the emitted arrays downstream by their length.

When should I use bufferWhen to batch expensive updates?

Use bufferWhen when you need to batch a stream of updates so an expensive operation runs once per batch rather than once per value. A typical case is re-rendering a DOM tree from a stream: values collect in the cache and are rendered together when the notifier fires. Choose it when the batch boundary should be driven by an event, such as a user click, rather than a fixed time window.

How does bufferWhen handle errors and completion in RxJS?

If any source observable throws, bufferWhen forwards the error notification to the observer. When the source completes, it sends the complete notification to the observer. Use a catch-all error handler on the subscription, since an error ends the stream. Typically you handle this in the subscriber's error callback, as buffered.subscribe({ next: ..., error: ... }).

Share this post

Sign up for our newsletter

Stay up-to-date with the trends and be a part of a thriving community.