Esta página ainda não foi traduzida, por isso é exibida em inglês. Ajude a traduzir

mergeMap, concatMap & exhaustMap

Three more answers to "an event arrived while the last one is still running": run them all, run them in order, or ignore the new one.

FxEvents<R> FxEvents<T>.mergeMap<R>(Stream<R> Function(T) f, {int? concurrent}) FxEvents<R> FxEvents<T>.concatMap<R>(Stream<R> Function(T) f) FxEvents<R> FxEvents<T>.exhaustMap<R>(Stream<R> Function(T) f)

Lecture

Mapping an event to an inner stream — a request, an upload, a query — raises one question that a pull pipeline never has to answer: what happens when the next event arrives before the last inner stream has finished? There are exactly four sensible policies, and picking the wrong one is where most reactive bugs live. switchMap is the last-wins answer; these three are the other three.

mergeMap(f) runs every inner stream at once and interleaves their output in arrival order. Use it when every result matters and none supersedes another — uploading three files, fanning out to three services. With concurrent: n at most n run at a time and the rest wait in a queue, which is how you keep a fan-out from opening two hundred sockets.

concatMap(f) runs them strictly in order, each to completion before the next begins. Nothing overlaps and nothing is dropped, so a slow inner stream backs the whole chain up — that is the point when order is the correctness condition, as in "apply these edits in sequence".

exhaustMap(f) keeps the first and ignores the rest: while an inner stream is running, incoming events are dropped outright — not queued, not cancelled. This is the double-submit guard. A second tap on a button whose request is still in flight does nothing at all, which is exactly what you want when the request is POST /orders.

fxdart events layer, after Rx's flatMap, flatMap(maxConcurrent: 1) and exhaustMap. The first is called mergeMap here because flatMap already means iterable-flattening on the pull side.

Demo 1 · mergeMap — everything at once

Demo 2 · exhaustMap — the double-submit guard

Try it yourself

Exercise: concatMap's ordering, and a bounded fan-out.

Related: switchMap — the fourth policy: newest wins, the rest are cancelled · mapConcurrent — pull-side bounded fan-out, where results stay in order · debounce — often the better fix: stop the extra events before they become inner streams