chunk, chunkOn & chunkEvery

Collect events into lists — by count, by a trigger, or by a clock — so a chatty stream becomes a few batched calls.

FxEvents<List<T>> FxEvents<T>.chunk(int count) // chain (events) FxEvents<List<T>> FxEvents<T>.chunkOn(Stream<void> trigger) FxEvents<List<T>> FxEvents<T>.chunkEvery(Duration window)

Lecture

Batching is the cheapest performance win a chatty stream has. A hundred analytics events are a hundred round trips one at a time and two round trips in batches of fifty; the work is identical, the cost is not. The pull layer batches by count with chunk, and that is all it can do — a pull pipeline has no clock, so "everything that happened in the last two seconds" is not a question it can ask.

The push side can. chunk(count) is the same fixed-size batching, with a short final batch flushed when the source closes so nothing is stranded. chunkEvery(window) batches by time instead: whatever arrived in the last window is emitted as one list. And chunkOn(trigger) hands the decision to a second stream — batch when the user scrolls, when the frame ends, when the connection comes back.

Both time-driven forms are silent on an empty window. A tick that finds nothing buffered emits nothing rather than an empty list, so downstream code never has to filter out batches that mean "nothing happened" — the same honesty rule sampleOn follows. Whatever is still buffered when the source closes is flushed before the close.

fxdart events layer, after Rx's bufferCount, buffer and bufferTime. The family keeps the pull layer's chunk as its root word — one name for one idea across both halves of the library — with the …On suffix for a trigger and …Every for a clock.

Demo 1 · Batching by the clock

Demo 2 · By count, and by trigger

Try it yourself

Exercise: a per-window summary instead of one report per click.

Related: chunk — the pull-layer original, batching by count over an Iterable · throttle — when you want one event per window rather than all of them · spaceBy — the other way to slow a burst: stretch it instead of grouping it