Only the newest search matters
Requirement
A user types three queries — fx, then
fxdar 40 ms later, then fxdart after a
pause. Each search takes 150 ms, so the second query arrives
while the first search is still in flight: the first result must be
discarded, never shown. Print only the surviving results, then how
many searches started vs were delivered. The schedule is simulated in
the code; both versions must print the lines shown under
Expected output.
Expected output
7 results for "fxdar" 12 results for "fxdart" searches started: 3, results delivered: 2
Side by side
RxDart
FxDart
Why they differ
They don't. "Newer cancels older" is a statement about
subscriptions, and the fxEvents layer has
them: its switchMap maps
each query to an inner search stream and unsubscribes the previous
one the moment a newer query arrives, so a stale result has no
listener left to reach. Three searches start, two results survive,
and none of that logic appears in user code — in either panel. No
hand-rolled epoch counter, no completion bookkeeping.
fxdart absorbs the Rx approach for exactly this kind of
requirement: fxEvents is a thin wrapper chain over plain
Streams — never an extension, so it coexists with any
other stream library, rxdart included. RxDart's operator catalog
remains far larger; when the surviving results need real per-value
processing, cross into the typed pull chain with .pull().
For switchMap's defining use case the panels are
operator-for-operator equivalent: a tie.
Benchmark
No benchmark for this example. This operator is defined by elapsed time, not by work done: both sides wait out the same windows, so a bar would be measuring Dart's timers rather than either library. Shrinking the windows does not help either — how many values a burst produces would then depend on how fast the machine is, and the two sides could no longer be checked against an identical result. The examples built on timing operators are left unmeasured for that reason; the ones whose delays merely stand in for I/O are benchmarked with those delays set to zero.