waitAll, zip & friends
Combining many streams into one: wait for all of them, pair them up by index, play them in sequence, or let them race.
Lecture
FxEvents.waitAll(sources) is
Future.wait for streams. It emits exactly
one event — a list holding each source's
last value, in source order — once every source has closed,
and then closes itself. That is the dashboard case: three panels load
independently, and the screen renders when the slowest one is in.
A source that closes without ever emitting means there is no complete
result to report, so nothing is emitted at all.
FxEvents.zip pairs sources by index:
every source's 1st event together, then every source's 2nd, and so
on. Whichever source runs ahead is buffered until the slowest catches
up, and the result closes as soon as a closed source runs out of
buffer — no further pair can ever be formed. zipWith is
the two-source form, and unlike the list-based static it can pair
different types.
It is worth holding zip and
combineLatest side by
side, because they are the two halves of "combine two streams" and
people reach for the wrong one constantly. zip pairs by
position: the 3rd of A always meets the 3rd of B, however
long that takes. combineLatest pairs by time: any
event re-emits with whatever the other side happens to hold right
now, so one source can appear in many outputs and another in none.
combineLatestAll is its N-ary form.
The rest are sequencing rather than combining.
FxEvents.concat plays each source through to completion
before starting the next — followedBy is its two-source
form, named after Dart's own Iterable.followedBy.
mergeWith and raceWith are the instance
forms of FxEvents.merge and
FxEvents.race. fxdart events
layer, after Rx's forkJoin, zip,
combineLatestList and concat.
Demo 1 · Waiting for every panel
Demo 2 · By position, or by time
Try it yourself
Exercise: sequencing — concat, followedBy, raceWith, mergeWith.
combineLatest — the by-time pairing, and the one you usually want for UI state ·
race — first source to speak wins, the others are cancelled ·
zip — the pull-layer original, pairing Iterables by index